c# 邮件发送

2021-03-26 23:27

阅读:567

标签:exception   ase   smtps   ndt   send   string   efault   ==   return   

 
1     
2     "SmtpServer" value="mail.anroc.com.au" />
3     "SmtpDeaultEmail" value="Test@anroc.com.au" />
4     
5     "FromUser" value="" />
6     "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# 邮件发送

标签:exception   ase   smtps   ndt   send   string   efault   ==   return   

原文地址:https://www.cnblogs.com/zxhome/p/9368925.html


评论


亲,登录后才可以留言!