zoukankan      html  css  js  c++  java
  • c# 邮件发送

     
    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(" ", "").Replace(" ", "").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; }
  • 相关阅读:
    【小白入门教程】3 分钟搞明白直播中拖动不准的问题
    如何在直播中解决黑屏、花屏、闪屏问题 | 直播疑难杂症排查
    骑兵变步兵?10 分钟搞明白如何在直播中去马赛克
    实现高性能纠删码引擎 | 纠删码技术详解(下)
    傅里叶分析之掐死教程(完整版)
    Python & 机器学习之项目实践
    LightGBM 调参方法(具体操作)
    模型调参:分步骤的提升模型的精度
    QQ的孤独
    python 机器学习中模型评估和调参
  • 原文地址:https://www.cnblogs.com/zxhome/p/9368925.html
Copyright © 2011-2022 走看看