zoukankan      html  css  js  c++  java
  • C# 发送邮件 System.Net.Mail

    C#要实现发送邮件功能很简单,微软已经帮我们搞定了大部分事情,话不多说,代码:

      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.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd);
    113 
    114 
    115                 //设置SMTP邮件服务器
    116                 smtp.Host = host;
    117                 smtp.EnableSsl = true;
    118                 try
    119                 {
    120                     //将邮件发送到SMTP邮件服务器
    121                     smtp.Send(myMail);
    122                     return true;
    123 
    124                 }
    125                 catch (System.Net.Mail.SmtpException)
    126                 {
    127                     return false;
    128                 }
    129 
    130             }
    131         }

    有上面这个类,直接调用Send访问就好了

     1 Email email = new Email();         
     2 email.mailFrom = 账号;         
     3 email.mailPwd = 密码;
     4 email.mailSubject = titile;//标题
     5 email.mailBody = content;//正文
     6 email.isbodyHtml = true;    //是否是HTML
     7 email.host = 邮箱host;//如果是QQ邮箱则:smtp:qq.com,依次类推
     8 email.mailToArray = new string[] { emailaddress };//接收者邮件集合
     9 email.mailCcArray = new string[] { };//抄送者邮件集合  
    10 email.Send();

    至此就完成邮件发送功能了,快试试吧

  • 相关阅读:
    fzuoj Problem 2177 ytaaa
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Capture the Flag
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Lunch Time
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Convert QWERTY to Dvorak
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest May Day Holiday
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Demacia of the Ancients
    zjuoj The 12th Zhejiang Provincial Collegiate Programming Contest Ace of Aces
    csuoj 1335: 高桥和低桥
  • 原文地址:https://www.cnblogs.com/JessieR/p/9015509.html
Copyright © 2011-2022 走看看