zoukankan      html  css  js  c++  java
  • .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, "", "")

  • 相关阅读:
    用Python完成一个汇率转换器
    鸿蒙如何用JS开发智能手表App
    鸿蒙如何用JS开发智能手表App
    SAP Spartacus SplitViewComponent Migration 的一个具体例子
    SAP Spartacus B2B 页面 Popover Component 的条件显示逻辑
    SAP Spartacus 升级时关于 schematics 的更新
    SAP Spartacus B2B 页面 Disable 按钮的显示原理
    SAP Spartacus B2B 页面 Disable Confirmation 对话框的显示原理
    通过 Feature Level 动态控制 SAP Spartacus 的页面显示
    SAP Commerce Cloud Build Manifest Components
  • 原文地址:https://www.cnblogs.com/Lvkang/p/9352937.html
Copyright © 2011-2022 走看看