.net中发送邮件方法有很多,如MailMessage,SmtpMail等下面我来给大家利用这些方法来实现在.net中邮件发送吧,希望此方法对各位同学会有所帮助
MailMessage 提供属性和方法来创建一个邮件消息对象。通常可以先构建好MailMessage对象,然后设置它的属性的方式来构建邮件程序。 常用的属性: From -- 发送邮件的地址 To -- 接受邮件的地址 Subject -- 邮件的标题 Priority -- 邮件的优先级(有效值为High,Low,Normal) Attachments -- 返回一个集合,代表附件 Bcc -- 密送地址 Cc -- 抄送地址 Body -- 获取或是设置电子邮件消息的内容 BodyFormat -- 获取或是设置MailFormat的枚举值,此值指定消息体邮件的格式(Html格式、Text格式) Bodyencoding -- 指定消息的编码方式编码(主要有Base64,UUencode) UrlContentBase :在HTML格式邮件中的URL编码方式 UrlContentLocation:邮件信息的优先级(High, Medium,Low) SmtpMail 负责发送邮件的SMTP协议,提供属性和方法通过使用windows 2000 CDOSYS 的消息组件的联合数据对象来发送邮件消息。 SmtpMail类用到的是Send方法,它的目的就是发送邮件,有两个重载方法。 1. SmtpMail.Send("发送邮件的地址","接受邮件的地址","邮件的标题","邮件消息的内容") 这个方法很简单,不适合发送带附件的邮件。 2. SmtpMail.Send(MailMessage) 此方法复杂、灵活,适合发送附件,而且可以设置MailMessage对象的各种属性值。 如果我们用ASP.NET写一个邮件发送的程序,那么首先应该如何得到SMTP。有两种方法:第一种方法调用目前知名的邮件服务提供商的SMTP,比如新浪、搜狐、网易的免费电子邮箱的SMTP;第二种方法是自己装一个SMTP虚拟服务器,这个在安装IIS时一起装上去的。 MailAttachment 与邮件附件有关的对象类,主要用来提供属性和方法来创建一个邮件附件对象。 构造函数 创建一个附件对象 MailAttachment objMailAttachment = new MailAttachment( "d:test。txt" );//发送邮件的附件 调用形式 代码如下: MailMessage objMailMessage= new MailMessage(); objMailMessage.Attachments.Add( objMailAttachment );//将附件附加到邮件消息对象中 封装的邮件发送类 代码如下: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Net; using System.Net.Mail; using System.Text; public class SendMail ...{ public SendMail() ...{ } private string _host; /**//// <summary> /// 服务器地址 /// </summary> public string Host ...{ get ...{ return _host; } set ...{ _host = value; } } private int _port; /**//// <summary> /// 服务器端口 /// </summary> public int Port ...{ get ...{ return _port; } set ...{ _port = value; } } private string _smtpUsername; /**//// <summary> /// 发送人邮箱用户名 /// </summary> public string SmtpUsername ...{ get ...{ return _smtpUsername; } set ...{ _smtpUsername = value; } } private string _sendemail; /**//// <summary> /// 发送人邮箱帐号(只接收加密串,发邮件时解密) /// </summary> public string SendEmail ...{ get ...{ return _sendemail; } set ...{ _sendemail = value; } } private string _replyToEmail; /**//// <summary> /// 回复人邮箱账号 /// </summary> public string ReplyToEmail ...{ get ...{ return _replyToEmail; } set ...{ _replyToEmail = value; } } private string _replyUserName; /**//// <summary> /// 回复人用户名 /// </summary> public string ReplyUserName ...{ get ...{ return _replyUserName; } set ...{ _replyUserName = value; } } private string _getemail; /**//// <summary> /// 收件人邮箱帐号 /// </summary> public string GetEmail ...{ get ...{ return _getemail; } set ...{ _getemail = value; } } private string _smtpPassword; /**//// <summary> /// 发送人邮箱密码(只接收加密串,发邮件时解密) /// </summary> public string SmtpPassword ...{ get ...{ return _smtpPassword; } set ...{ _smtpPassword = value; } } private string _content; /**//// <summary> /// 邮件内容 /// </summary> public string Content ...{ get ...{ return _content; } set ...{ _content = value; } } private string _title; /**//// <summary> /// 邮件标题 /// </summary> public string Title ...{ get ...{ return _title; } set ...{ _title = value; } } private string[] _cc = null; /**//// <summary> /// 抄送邮箱 /// </summary> public string[] cc ...{ get ...{ return _cc; } set ...{ _cc = value; } } private string[] _bcc = null; /**//// <summary> /// 密送邮箱 /// </summary> public string[] bcc ...{ get ...{ return _bcc; } set ...{ _bcc = value; } } /**//// <summary> ///发送邮件 /// </summary> /// <returns>返回是否成功</returns> public bool Send() ...{ try ...{ MailMessage objMailMessage; objMailMessage = new MailMessage(SendEmail, _getemail, _title, _content); if (!string.IsNullOrEmpty(_replyToEmail) && !string.IsNullOrEmpty(_replyUserName)) ...{ MailAddress reply = new MailAddress(_replyToEmail, _replyUserName); objMailMessage.ReplyToList.Add(reply); } objMailMessage.BodyEncoding = Encoding.GetEncoding(936); objMailMessage.IsBodyHtml = true; if (cc != null && cc.Length > 0) ...{ foreach (string ccAddress in cc) ...{ &n