码迷,mamicode.com
首页 > Windows程序 > 详细

c# 邮件发送

时间:2018-07-25 23:57:28      阅读:363      评论:0      收藏:0      [点我收藏+]

标签: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="Test@anroc.com.au" />
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# 邮件发送

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!