C#Mail帮助类
Posted seven77yixuan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#Mail帮助类相关的知识,希望对你有一定的参考价值。
public class EmailHelper { public static string SendWithSenderEmail(string OurEmail, string OurPs, string subject, string body, string attachment, string attachmentf, string attachments, string[] Mail_Image, string toes, string cc = null,string bcc=null) { try { string SYS_TEST_EMAIL_OffON = ConfigHelper.GetConfigString("SYS_TEST_EMAIL_OffON"); string SYS_IS_TESTEVN = ConfigHelper.GetConfigString("SYS_Is_TestEvn"); string TestEmailsWhite = ConfigHelper.GetConfigString("TestEmailsWhite"); string WebSite = ConfigHelper.GetConfigString("SMTP"); MailMessage message = new MailMessage(); SmtpClient client = new SmtpClient(WebSite, 25); //smtp address and port string SYS_EMAIL = ConfigHelper.GetConfigString("SYS_EMAIL"); message.From = new MailAddress(OurEmail);//the email address used to send email string[] whiteEmailList = TestEmailsWhite.Split(‘;‘); if (!string.IsNullOrEmpty(toes)) { string[] toList = toes.Split(‘;‘); ArrayList arrTo = new ArrayList(); foreach (string str in toList) { //过滤重复邮箱 if (!string.IsNullOrEmpty(str) && !arrTo.Contains(str.Trim())) { if (SYS_TEST_EMAIL_OffON == "ON") { // 如果测试开关开启, 仅在白名单中的邮件允许发送 foreach (var email in whiteEmailList) { if (email.Trim() == str.Trim()) { arrTo.Add(str.Trim()); message.To.Add(new MailAddress(str.Trim())); } } } else { arrTo.Add(str.Trim()); message.To.Add(new MailAddress(str.Trim())); } } } } else { return "发邮件发件人不许为空:" + "发件人:" + OurEmail + " \n\t" + "SMTP身份验证用户名" + OurEmail.Split(‘@‘)[0]; } if (!string.IsNullOrEmpty(cc)) { string[] ccList = cc.Split(‘;‘); ArrayList arrCC = new ArrayList(); foreach (string str in ccList) { //过滤重复邮箱 if (!string.IsNullOrEmpty(str) && !arrCC.Contains(str.Trim())) { if (SYS_TEST_EMAIL_OffON == "ON") { // 如果测试开关开启, 仅在白名单中的邮件允许发送 foreach (var email in whiteEmailList) { if (email.Trim() == str.Trim()) { arrCC.Add(str.Trim()); message.CC.Add(new MailAddress(str.Trim())); } } } else { arrCC.Add(str.Trim()); message.CC.Add(new MailAddress(str.Trim())); } } } } //密送 if (!string.IsNullOrEmpty(bcc)) { string[] bccList = bcc.Split(‘;‘); ArrayList arrBCC = new ArrayList(); foreach (string str in bccList) { //过滤重复邮箱 if (!string.IsNullOrEmpty(str) && !arrBCC.Contains(str.Trim())) { if (SYS_TEST_EMAIL_OffON == "ON") { // 如果测试开关开启, 仅在白名单中的邮件允许发送 foreach (var email in whiteEmailList) { if (email.Trim() == str.Trim()) { arrBCC.Add(str.Trim()); message.Bcc.Add(new MailAddress(str.Trim())); } } } else { arrBCC.Add(str.Trim()); message.Bcc.Add(new MailAddress(str.Trim())); } } } } message.CC.Add(new MailAddress(SYS_EMAIL)); message.Bcc.Add(new MailAddress(SYS_EMAIL)); if (SYS_IS_TESTEVN == "ON") { message.Subject = "测试环境-" + subject; } else { message.Subject = subject; } message.Body = body; message.IsBodyhtml = true; message.Priority = MailPriority.Normal; if (!string.IsNullOrEmpty(attachment)) { message.Attachments.Add(new Attachment(attachment)); } if (!string.IsNullOrEmpty(attachmentf)) { message.Attachments.Add(new Attachment(attachmentf)); } if (!string.IsNullOrEmpty(attachments)) { message.Attachments.Add(new Attachment(attachments)); } if (Mail_Image != null && Mail_Image.Length > 0) { for (int i = 0; i < Mail_Image.Length; i++) { if (!string.IsNullOrEmpty(Mail_Image[i])) { System.Net.Mail.Attachment Imageattachment = new System.Net.Mail.Attachment(Mail_Image[i]); message.Attachments.Add(Imageattachment); message.Body = message.Body.Replace("<$template_IMG" + (i + 1) + ">", "<img src=\"cid:" + Imageattachment.ContentId + "\"/>"); } else { message.Body = message.Body.Replace("<$template_IMG" + (i + 1) + ">", ""); } } } #region If the smtp doesn‘t require ssl, the code could removed. string userOfAD = OurEmail.Split(‘@‘)[0]; NetworkCredential c = new NetworkCredential { Password = OurPs, UserName = userOfAD }; client.Credentials = c; //client.EnableSsl = true; #endregion StringBuilder builder = new StringBuilder(); builder.AppendFormat("发邮件给 {0}, 标题:{1} , 当前时间:{2}", message.To.ToString(), message.Subject, DateTime.Now); client.Send(message); builder.Clear(); builder.AppendFormat("邮件发送成功!标题:{0}, 时间:{1}", message.Subject, DateTime.Now); } catch (Exception ex) { string strE = "发邮件内部错误:" + ex.InnerException + " \n\t" + "堆栈:" + ex.StackTrace + " \n\t" + "出错信息:" + ex.Message + " \n\t" + "错误源:" + ex.Source + " \n\t" + "发件人:" + OurEmail + " \n\t" + "SMTP身份验证用户名" + OurEmail.Split(‘@‘)[0]; //Log(文本) MyLogHelper.Info(System.Reflection.MethodBase.GetCurrentMethod().ToString() + strE.ToString()); return strE; } return null; } /// <summary> /// 用系统中 config 里面配置的邮箱 异步 发送邮件 /// </summary> /// <param name="Email"></param> /// <param name="email_Body"></param> /// <param name="Object"></param> /// <returns></returns> public static string Sys_SentEmail(string Email, string email_Body, string Object) { //发送邮件。 string SYS_EMAIL = ConfigHelper.GetConfigString("SYS_EMAIL"); string SYS_EMAILPASSWORD = ConfigHelper.GetConfigString("SYS_EMAILPASSWORD"); string SYS_SMTP = ConfigHelper.GetConfigString("SMTP"); //<!--如果打开测试邮箱开关,则所有发送的邮件都发给下面的系统邮箱--> //string SYS_TEST_EMAIL_OffON = ConfigHelper.GetConfigString("SYS_TEST_EMAIL_OffON"); //if (SYS_TEST_EMAIL_OffON == "ON") //{ // Email = SYS_EMAIL; //} string SyncDataErrorEmail = ConfigHelper.GetConfigString("Email_ForSyncError"); if (Email == "SyncDataError") { if (SyncDataErrorEmail == null || string.IsNullOrEmpty(SyncDataErrorEmail)) { SyncDataErrorEmail = "[email protected]"; } } string error = null; System.Threading.Thread t = new System.Threading.Thread(() => { error = EmailHelper.SendWithSenderEmail(SYS_EMAIL, SYS_EMAILPASSWORD, Object, email_Body, "", "", "", null, SyncDataErrorEmail, null); }); t.IsBackground = true; t.Start(); return error; } public static string User_SentEmail(string toEmail, string ccEmail,string bccEmail, string email_Body, string Object) { //发送邮件。 string SYS_EMAIL = ConfigHelper.GetConfigString("SYS_EMAIL"); string SYS_EMAILPASSWORD = ConfigHelper.GetConfigString("SYS_EMAILPASSWORD"); string SYS_SMTP = ConfigHelper.GetConfigString("SMTP"); string error = null; error = EmailHelper.SendWithSenderEmail(SYS_EMAIL, SYS_EMAILPASSWORD, Object, email_Body, "", "", "", null, toEmail, ccEmail,bccEmail); return error; } }
以上是关于C#Mail帮助类的主要内容,如果未能解决你的问题,请参考以下文章
在mail_admins()中发送EmailMultiAlternatives
如何通过单击片段内的线性布局从片段类开始新活动?下面是我的代码,但这不起作用
csharp C#代码片段 - 使类成为Singleton模式。 (C#4.0+)https://heiswayi.github.io/2016/simple-singleton-pattern-us