C#发送邮件

1.引用命名空间:

using System.Net.Mail;
using System.Text;

2.创建发送邮件的类

  1     public class Email
  2     {
  3         /// <summary>
  4         /// 发送者
  5         /// </summary>
  6         public string mailFrom { get; set; }
  7 
  8         /// <summary>
  9         /// 收件人
 10         /// </summary>
 11         public string[] mailToArray { get; set; }
 12 
 13         /// <summary>
 14         /// 抄送
 15         /// </summary>
 16         public string[] mailCcArray { get; set; }
 17 
 18         /// <summary>
 19         /// 标题
 20         /// </summary>
 21         public string mailSubject { get; set; }
 22 
 23         /// <summary>
 24         /// 正文
 25         /// </summary>
 26         public string mailBody { get; set; }
 27 
 28         /// <summary>
 29         /// 发件人密码
 30         /// </summary>
 31         public string mailPwd { get; set; }
 32 
 33         /// <summary>
 34         /// SMTP邮件服务器
 35         /// </summary>
 36         public string host { get; set; }
 37 
 38         /// <summary>
 39         /// 正文是否是html格式
 40         /// </summary>
 41         public bool isbodyHtml { get; set; }
 42 
 43         /// <summary>
 44         /// 附件
 45         /// </summary>
 46         public string[] attachmentsPath { get; set; }
 47 
 48         public bool Send()
 49         {
 50             //使用指定的邮件地址初始化MailAddress实例
 51             MailAddress maddr = new MailAddress(mailFrom);
 52             //初始化MailMessage实例
 53             MailMessage myMail = new MailMessage();
 54 
 55 
 56             //向收件人地址集合添加邮件地址
 57             if (mailToArray != null)
 58             {
 59                 for (int i = 0; i < mailToArray.Length; i++)
 60                 {
 61                     myMail.To.Add(mailToArray[i].ToString());
 62                 }
 63             }
 64 
 65             //向抄送收件人地址集合添加邮件地址
 66             if (mailCcArray != null)
 67             {
 68                 for (int i = 0; i < mailCcArray.Length; i++)
 69                 {
 70                     myMail.CC.Add(mailCcArray[i].ToString());
 71                 }
 72             }
 73             //发件人地址
 74             myMail.From = maddr;
 75 
 76             //电子邮件的标题
 77             myMail.Subject = mailSubject;
 78 
 79             //电子邮件的主题内容使用的编码
 80             myMail.SubjectEncoding = Encoding.UTF8;
 81 
 82             //电子邮件正文
 83             myMail.Body = mailBody;
 84 
 85             //电子邮件正文的编码
 86             myMail.BodyEncoding = Encoding.Default;
 87 
 88             myMail.Priority = MailPriority.High;
 89 
 90             myMail.IsBodyHtml = isbodyHtml;
 91 
 92             //在有附件的情况下添加附件
 93             try
 94             {
 95                 if (attachmentsPath != null && attachmentsPath.Length > 0)
 96                 {
 97                     Attachment attachFile = null;
 98                     foreach (string path in attachmentsPath)
 99                     {
100                         attachFile = new Attachment(path);
101                         myMail.Attachments.Add(attachFile);
102                     }
103                 }
104             }
105             catch (Exception err)
106             {
107                 throw new Exception("在添加附件时有错误:" + err);
108             }
109 
110             SmtpClient smtp = new SmtpClient();
111             //指定发件人的邮件地址和密码以验证发件人身份
112             smtp.EnableSsl = true;
113             smtp.UseDefaultCredentials = false;
114             smtp.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd);
115 
116             //设置SMTP邮件服务器
117             smtp.Host = host;
118 
119             try
120             {
121                 //将邮件发送到SMTP邮件服务器
122                 smtp.Send(myMail);
123                 return true;
124 
125             }
126             catch (System.Net.Mail.SmtpException ex)
127             {
128                 return false;
129             }
130 
131         }
132     }

3.使用发送邮件的类发送邮件

 1     Email email = new Email();
 2     email.mailFrom = "...@qq.com"; //"...@qq.com";"...@163.com"
 3     email.mailPwd = ""; //QQ邮箱要使用QQ授权码,而不是邮箱密码
 4     email.mailSubject = "这个是邮件主题";
 5     email.mailBody = "这是邮件内容";
 6     email.isbodyHtml = true;    //是否是HTML
 7     email.host = "smtp.163.com";//如果是QQ邮箱则:smtp:qq.com,以次类推
 8     email.mailToArray = new string[] { "...@qq.com" };//接收者邮件集合
 9     email.mailCcArray = new string[] { };//抄送者邮件集合
10     email.attachmentsPath = new string[] { "E:\\test.txt" };//附件
11 
12     if (email.Send())
13     {
14         Response.Write("<script type='text/javascript'>alert('发送成功!')</script>");
15 
16     }
17     else
18     {
19         Response.Write("<script type='text/javascript'>alert('发送失败!')</script>");
20     }

 

posted @ 2016-12-14 11:34  枫林余晖  阅读(280)  评论(0编辑  收藏  举报