zoukankan      html  css  js  c++  java
  • .net使用 SmtpClient 发邮件

    private static void SendEmail(string mailSubject, List<string> mailAddress, List<string> ccs, string mailContent, List<string> mailAttachment)
    {
    try
    {
    SmtpClient smtpClient = new SmtpClient();
    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
    smtpClient.Host = smtpServer; //指定SMTP服务器
    smtpClient.UseDefaultCredentials = true;

    //string userPassword = "&R$CEg=6";//登陆密码
    //smtpClient.UseDefaultCredentials = false;
    //smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, userPassword);//用户名和密码

    // 发送邮件设置
    MailMessage mailMessage = new MailMessage(); // 发送人和收件人
    MailAddress mf = new MailAddress(mailFrom, dispalyName);
    mailMessage.From = mf;

    foreach (string current in mailAddress)
    {
    if (!string.IsNullOrEmpty(current) && current.IndexOf('@') > 0)
    {
    mailMessage.To.Add(current);
    }
    }
    if (ccs != null)
    {
    foreach (string current2 in ccs)
    {
    if (!string.IsNullOrEmpty(current2) && current2.IndexOf('@') > 0)
    {
    mailMessage.CC.Add(current2);
    }
    }
    }
    mailMessage.Body = mailContent;
    mailMessage.BodyEncoding = Encoding.UTF8;
    mailMessage.IsBodyHtml = true;
    mailMessage.Subject = mailSubject;
    mailMessage.SubjectEncoding = Encoding.UTF8;
    mailMessage.Priority = MailPriority.High;
    if (mailAttachment != null && mailAttachment.Count > 0)
    {
    foreach (string current3 in mailAttachment)
    {
    if (File.Exists(current3))
    {
    Attachment item = new Attachment(current3);
    mailMessage.Attachments.Add(item);
    }
    }
    }
    smtpClient.Send(mailMessage);
    log.Info("send e-mail success!");
    }
    catch (Exception e)
    {
    log.Error(string.Format(" 内部错误:{0} 堆栈:{1} Message:{2} 来源:{3} ",
    e.InnerException==null?e.Message:e.InnerException.ToString(), e.StackTrace, e.Message, e.Source));
    //throw e;
    }
    }

  • 相关阅读:
    Go 语言简介(下)— 特性
    Array.length vs Array.prototype.length
    【转】javascript Object使用Array的方法
    【转】大话程序猿眼里的高并发架构
    【转】The magic behind array length property
    【转】Build Your own Simplified AngularJS in 200 Lines of JavaScript
    【转】在 2016 年做 PHP 开发是一种什么样的体验?(一)
    【转】大话程序猿眼里的高并发
    php通过token验证表单重复提交
    windows 杀进程软件
  • 原文地址:https://www.cnblogs.com/Depingblogs/p/14074338.html
Copyright © 2011-2022 走看看