c# 郵件發送

來源:互聯網
上載者:User

標籤:exception   ase   smtps   ndt   send   string   efault   ==   return   

 
1     <!--SMTP server address-->2     <add key="SmtpServer" value="mail.anroc.com.au" />3     <add key="SmtpDeaultEmail" value="[email protected]" />4     <!--email address and password used to send mails-->5     <add key="FromUser" value="" />6     <add key="FromPwd" value="" />
   
 private static readonly string _smtpServer = ConfigurationManager.AppSettings["SmtpServer"]; private static readonly string _userAccount = ConfigurationManager.AppSettings["FromUser"]; private static readonly string _userPwd = ConfigurationManager.AppSettings["FromPwd"]; private static readonly string _toUser = ConfigurationManager.AppSettings["ToUser"]; private static readonly string _defaultEmailAddress = ConfigurationManager.AppSettings["SmtpDeaultEmail"];

public static Operate SendEmail(string title, string content, string toUserEmail = "", string filepath = "")        {            var result=new Operate();            try            {
          //郵件接收人 var sendTo = toUserEmail == "" ? _toUser : toUserEmail;
           var client = new SmtpClient(_smtpServer); client.Timeout = 60000; client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential(_userAccount, _userPwd); client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; var message = new MailMessage(); if (filepath != "") { var attach = new Attachment(filepath); attach.Name = Path.GetFileName(filepath); attach.NameEncoding = Encoding.GetEncoding("gb2312"); attach.TransferEncoding = System.Net.Mime.TransferEncoding.Base64; attach.ContentDisposition.Inline = true; attach.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline; message.Attachments.Add(attach); } message.SubjectEncoding = System.Text.Encoding.UTF8; message.BodyEncoding = System.Text.Encoding.UTF8; message.From = new System.Net.Mail.MailAddress(_defaultEmailAddress); message.To.Add(new System.Net.Mail.MailAddress(sendTo)); message.IsBodyHtml = true; message.Subject = title.Replace("\r", "").Replace("\n", "").Trim(); message.Body = content; client.SendMailAsync(message); result.Status = 1; } catch (Exception ex) { result.Status = -1; result.Message = ex.Message; Logger.WriteErrorLog(ex); } return result; }

 

c# 郵件發送

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.