• .NET发送邮件的方法


    整理一下,在.NET中发送邮件的一个方法,代码如下:

     1 public static string Net_Email(string strSendto, string strCC, string strBCC, string strMailContent, string strMailTitle, string[] strMailAttachMent, string strMailFormat, string strServiceTeam)
     2 {
     3     try
     4     {
     5         MailMessage mMail = new MailMessage();
     6         using (mMail)
     7         {
     8             strMailContent = strMailContent.Replace("<br>
    ", "<br>");
     9             string[] ListTo = strSendto.Split(';');
    10             for (int i = 0; i < ListTo.Length; i++)
    11             {
    12                 if (ListTo[i].Equals("")) continue;
    13                 mMail.To.Add(ListTo[i]);
    14             }
    15             string[] ListCC = strCC.Split(';');
    16             for (int i = 0; i < ListCC.Length; i++)
    17             {
    18                 if (ListCC[i].Equals("")) continue;
    19                 mMail.CC.Add(ListCC[i]);
    20             }
    21             string[] ListBCC = strBCC.Split(';');
    22             for (int i = 0; i < ListBCC.Length; i++)
    23             {
    24                 if (ListBCC[i].Equals("")) continue;
    25                 mMail.Bcc.Add(ListBCC[i]);
    26             }
    27             string[] strMailFrom = strServiceTeam.Split(';');
    28             mMail.From = strMailFrom[0].IndexOf("@", StringComparison.Ordinal) > 1 ? new MailAddress(strMailFrom[0]) : new MailAddress("xxxxx@xxx.com.cn");
    29             mMail.Body = strMailContent;
    30             mMail.Subject = strMailTitle;
    31             mMail.IsBodyHtml = strMailFormat == "";
    32             for (int i = 0; i < strMailAttachMent.Length; i++)
    33             {
    34                 string strFJ = strMailAttachMent[i] + "";
    35                 if (strFJ.Length > 0)
    36                 {
    37                     mMail.Attachments.Add(new Attachment(strFJ));
    38                 }
    39             }
    40             SmtpClient smtp = new SmtpClient();
    41             smtp.Host = "10.10.10.10";
    42             smtp.Credentials = new NetworkCredential("emailcn", "123");
    43             smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    44             smtp.Send(mMail);
    45             return "1";
    46             }
    47         }
    48     catch (Exception ex)
    49     {
    50         return ex.Message;
    51     }
    52 }        

     调用方式:

    组装邮件内容的时候,是调用一个html模板,将其中的标签内容进行替换

    var strFileCn = File.ReadFile(MapPath("order_model.html"), "utf-8");
    strFileCn = strFileCn.Replace("#orderid#", orders.orderid);
    strFileCn = strFileCn.Replace("#username#", orders.username);

    var att = new string[1];
    att[0] = "";

    Net_Email(strsendto, strcc, "", strFileCn, "商品订购单Order sheet.",att, "", "")

  • 相关阅读:
    github 代理加速
    centos系统语言设置为中文
    红帽 / CentOS安装Jenkins
    查看api有没有更新到位
    永久关闭Windows10或Windows11的系统自动更新
    api传文件连接超时
    docker日常使用
    开发者工具批量替换
    Linux常用工具安装
    office密钥
  • 原文地址:https://www.cnblogs.com/Lvkang/p/9352937.html
Copyright © 2020-2023  润新知