标签:
public class EmailHelper { #region 私有字段 private static bool IsOutlook = false;//是否启用邮件发送功能 private static bool IsTestEmail = true;//是否测试账号 private static string MailServer = "";//邮件服务器 private static string MailAccount = "";//邮件账号 private static string MailPassword = "";//邮件账号密码 private static string Mail_suffix = "";//邮件后缀 #endregion #region 构造函数 public EmailHelper(string IS_OUTLOOK,string IS_ENVIRONMENT,string MAIL_SENDER,string MAIL_ACCOUNT,string MAIL_PASSWORD,string MAIL_SUFFIX) { IsOutlook = (IS_OUTLOOK == "1") ? true : false; IsTestEmail = (IS_ENVIRONMENT == "0") ?true : false; MailServer = MAIL_SENDER; MailAccount = MAIL_ACCOUNT; MailPassword = MAIL_PASSWORD; Mail_suffix = MAIL_SUFFIX; } #endregion #region 邮件发送方法 /// <summary> /// 邮件发送功能 /// </summary> /// <param name="m_strRecipientMail">收件人地址如: pcitzs,请勿加入@GNPJVC.COM.CN, 多个帐号之间请使用 , 或 ; 号隔开</param> /// <param name="m_strCC">抄送人地址如: pcitzs,请勿加入@GNPJVC.COM.CN, 多个帐号之间请使用 , 或 ; 号隔开</param> /// <param name="m_strarrayMailAttachmentList">附件完整地址</param> /// <param name="m_strMailSubject">主题</param> /// <param name="m_strMailBody">内容</param> /// <returns></returns> public bool SendMail(string m_strRecipientMail, string m_strCC, string m_strarrayMailAttachmentList, string m_strMailSubject, string m_strMailBody) { if (!IsOutlook) { return true; } string m_strMailServerName = MailServer; string adSuffix = Mail_suffix; // "@gnpjvc.com.cn"; string m_strSenderMail = MailAccount + adSuffix; string m_lMailUsername = MailAccount; string m_lMailUserPassword = MailPassword; if (IsTestEmail) { m_strMailSubject = "[测试邮件]" + m_strMailSubject; m_strMailBody = "这是一封测试邮件,抱歉占用您宝贵的时间,此邮件可直接删除,谢谢。<br>" + m_strMailBody; } //接收者列表 string[] ArrsToUser, ArrsCCUser; ArrsToUser = m_strRecipientMail.Replace(‘;‘, ‘,‘).Split(‘,‘); m_strRecipientMail = ""; for (int i = 0; i < ArrsToUser.Length; i++) { if (i == ArrsToUser.Length-1) { m_strRecipientMail += ArrsToUser[i] + adSuffix; } else { m_strRecipientMail += ArrsToUser[i] + adSuffix + ","; } } //抄送人列表 ArrsCCUser = m_strCC.Replace(‘;‘, ‘,‘).Split(‘,‘); m_strCC = ""; for (int i = 0; i < ArrsCCUser.Length; i++) { if (ArrsCCUser[i] != "") { if (i == ArrsCCUser.Length - 1) { m_strCC += ArrsCCUser[i] + adSuffix; } else { m_strCC += ArrsCCUser[i] + adSuffix + ","; } } } System.Net.Mail.MailMessage aMessage; System.Net.Mail.SmtpClient Client = new SmtpClient(); if (m_strSenderMail == "" || m_strSenderMail == null) { return false; } if (m_strRecipientMail == "" || m_strRecipientMail == null) { return false; } //Create a new message try { aMessage = new MailMessage(m_strSenderMail, m_strRecipientMail); if (m_strCC != "") { aMessage.CC.Add(m_strCC); } if (m_strMailSubject != null) aMessage.Subject = m_strMailSubject; aMessage.BodyEncoding = System.Text.Encoding.UTF8; aMessage.IsBodyHtml = true; if (m_strMailBody != null) aMessage.Body = m_strMailBody; //add the attachment if (m_strarrayMailAttachmentList != "") { aMessage.Attachments.Add(new System.Net.Mail.Attachment(m_strarrayMailAttachmentList)); } //create a smtpMail Client.Host = m_strMailServerName; Client.UseDefaultCredentials = false; Client.Credentials = new System.Net.NetworkCredential(m_lMailUsername, m_lMailUserPassword, "domain"); } catch (Exception err) { return false; } // send email try { Client.Send(aMessage); return true; } catch (Exception err) { return false; } } #endregion }
标签:
原文地址:http://www.cnblogs.com/kejin123/p/5325082.html