zoukankan      html  css  js  c++  java
  • Send Email

     private string SendEmail(string mailTo, string body, ref int sendresult)
            {
                string errorEmailAddress = ""; 
                SmtpClient smtp = new SmtpClient(_SmtpServer);
                try
                {
    
                    MailMessage message = new MailMessage();
                    message.Priority = MailPriority.High;
                    message.From = new MailAddress(_From);
                    //message.To.Add(mailTo);
                    message.Subject = _Subject;
                    message.Body = body;
                    message.IsBodyHtml = true;
                    message.Bcc.Add(new MailAddress(_Bcc, "", Encoding.Default));
                    SetToMailAddress(mailTo, message, ref errorEmailAddress);
                    if (message.To.Count > 0)
                    {
                        smtp.Send(message);
                        sendresult = 1;
                    } 
                    smtp.Dispose();
                    return string.IsNullOrEmpty(errorEmailAddress) ? string.Empty : ("Error email address:"+errorEmailAddress);
                }
                catch (Exception e)
                {
                    smtp.Dispose(); 
                    return e.InnerException.Message + (string.IsNullOrEmpty(errorEmailAddress) ? "" : (" Error email address: " + errorEmailAddress)); 
                }
            }
            private static void SetToMailAddress(string toMailAddress, MailMessage message, ref string errorEmailAddress)
            {
                string[] addes = toMailAddress.Split(';').ToArray();
                foreach (string s in addes)
                {
                    if (Regex.IsMatch(s, @"w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*"))
                    {
                        message.To.Add(new MailAddress(s.Trim(), "", Encoding.Default));
                    }
                    else
                    { errorEmailAddress += s + ";"; }
                }
            } 
  • 相关阅读:
    几个数之和----数组刷题
    单调栈刷题
    腾讯金融科技凉经
    mysql刷题
    链表类题目常用方法
    阿里云一面凉经
    腾讯TEG一面凉经
    腾讯软件开发-后台开发实习生-一面凉经
    剑指 Offer 19. 正则表达式匹配
    剑指 Offer 20. 表示数值的字符串
  • 原文地址:https://www.cnblogs.com/muxueyuan/p/6868271.html
Copyright © 2011-2022 走看看