📜  c# 发送电子邮件 - C# 代码示例

📅  最后修改于: 2022-03-11 14:48:57.583000             🧑  作者: Mango

代码示例1
using System.Net;
using System.Net.Mail;
using System.Net.Mime;

...
try
{

   SmtpClient mySmtpClient = new SmtpClient("my.smtp.exampleserver.net");

    // set smtp-client with basicAuthentication
    mySmtpClient.UseDefaultCredentials = false;
   System.Net.NetworkCredential basicAuthenticationInfo = new
      System.Net.NetworkCredential("username", "password");
   mySmtpClient.Credentials = basicAuthenticationInfo;

   // add from,to mailaddresses
   MailAddress from = new MailAddress("test@example.com", "TestFromName");
   MailAddress to = new MailAddress("test2@example.com", "TestToName");
   MailMessage myMail = new System.Net.Mail.MailMessage(from, to);

   // add ReplyTo
   MailAddress replyTo = new MailAddress("reply@example.com");
   myMail.ReplyToList.Add(replyTo);

   // set subject and encoding
   myMail.Subject = "Test message";
   myMail.SubjectEncoding = System.Text.Encoding.UTF8;

   // set body-message and encoding
   myMail.Body = "Test Mail
using HTML."; myMail.BodyEncoding = System.Text.Encoding.UTF8; // text or html myMail.IsBodyHtml = true; mySmtpClient.Send(myMail); } catch (SmtpException ex) { throw new ApplicationException ("SmtpException has occured: " + ex.Message); } catch (Exception ex) { throw ex; }