Example of. Net sending mail

Time:2019-12-9

Preface

This article mainly introduced to you about. Net to send mail related content, share it for your reference and study, the next few words, let’s have a look at the detailed introduction.

Be careful:You need to find “POP3 / SMTP service” and start it, and then generate the authorization code, which is the password for login below.

As for what POP3 / SMTP service is, you can refer to this article for details: / / www.jb51.net/softjc/42323.html

You can refer to this article for opening method of / / www.jb51.net/diannaojichu/520949.html

Example code:

/// <summary>
 ///Send mail
 /// </summary>
 ///< param name = "to" > recipients (separated by) < / param >
 ///< param name = "title" > Title < / param >
 ///< param name = "content" > content < / param >
 ///< param name = "CC" > CC < / param >
 /// <returns></returns>
 public string sendEmail(string to, string title, string content, string cc = "")
 {
  try
  {
   System.Net.Mail.MailMessage myMail = new System.Net.Mail.MailMessage();
   Mymail. From = new system. Net. Mail. MailAddress ("XXX @ QQ. Com", "XX notification", system. Text. Encoding. Utf8); // sender address, sender name, code
   string[] tos = to.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
   for (int i = 0; i < tos.Length; i++)
   {
    myMail.To.Add(new System.Net.Mail.MailAddress(tos[i]));
   }
   string[] ccs = cc.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
   for (int i = 0; i < ccs.Length; i++)
   {
    myMail.CC.Add(new System.Net.Mail.MailAddress(ccs[i]));
   }
   myMail.Subject = title;
   myMail.SubjectEncoding = Encoding.UTF8;
   myMail.Body = content;
   myMail.BodyEncoding = Encoding.UTF8;
   myMail.IsBodyHtml = true;
   System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
   smtp.Host = "smtp.qq.com";   smtp.EnableSsl = true;
   smtp.UseDefaultCredentials = false;
   smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
   smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
   smtp.Send(myMail);
   return "";
  }
  catch (Exception ee)
  {
   return ee.ToString();
  }
 }

summary

The above is the whole content of this article. I hope that the content of this article has a certain reference learning value for everyone’s study or work. If you have any questions, you can leave a message and exchange. Thank you for your support for developepaar.