This article is a mirror article of machine translation, please click here to jump to the original article.

View: 20648|Reply: 0

[Source] c# SmtpClient sends the email source code

[Copy link]
Posted on 12/25/2015 12:46:03 PM | | |
  1. /// <summary>
  2.         /// 处审核后类的实例
  3.         /// </summary>
  4.         /// <param name="To">收件人地址集合</param> 根据个人需要是单个收件人或者多个
  5.         /// <param name="Body">邮件正文</param>
  6.         /// <param name="Title">邮件的主题</param>
  7.         public SendMail(List<string> To, string Body, string Title)
  8.         {
  9.             mailMessage = new MailMessage();
  10.             foreach (string str in To)
  11.             {
  12.                 mailMessage.To.Add(str);
  13.             }
  14.             List<sendmailinfoModel> list = new List<sendmailinfoModel>();
  15.             sendmailinfoProc sendMailinfoProc = new sendmailinfoProc();
  16.             list = sendMailinfoProc.GetList(null);
  17.             sendmailinfoModel sendinfo = list[0] as sendmailinfoModel;
  18.             if (sendinfo != null)
  19.             {
  20.                 mailMessage.From = new System.Net.Mail.MailAddress(sendinfo.Mailaddress, sendinfo.Mailname);
  21.                 //mailMessage.From = new System.Net.Mail.MailAddress("aaa@163.com");
  22.                 mailMessage.Subject = Title;
  23.                 mailMessage.Body = Body;
  24.                 mailMessage.IsBodyHtml = false;
  25.                 mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
  26.                 mailMessage.Priority = System.Net.Mail.MailPriority.Normal;
  27.                 this.password = sendinfo.Mailpassword;
  28.                 //this.password = "***";
  29.                 smtpserver = sendinfo.Smtpserver;
  30.             }
  31.         }

  32. /// <summary>
  33.         /// 发送邮件
  34.         /// </summary>
  35.         public SmtpStatusCode Send()
  36.         {
  37.             if (mailMessage != null)
  38.             {
  39.                 smtpClient = new SmtpClient();
  40.                 smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据
  41.                 smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
  42.                 smtpClient.Host = smtpserver;
  43.                 try
  44.                 {
  45.                     smtpClient.Send(mailMessage);
  46.                 }
  47.                 catch (SmtpException e)
  48.                 {
  49.                     return e.StatusCode;
  50.                 }
  51.                 return SmtpStatusCode.Ok;
  52.             }
  53.             return SmtpStatusCode.SyntaxError;
  54.         }
Copy code






Previous:Asp.Net 404 page setup
Next:c# AES encryption decryption algorithm
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com