.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>\n", "<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("[email protected]");
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, "", "")

猜你喜欢

转载自www.cnblogs.com/Lvkang/p/9352937.html