码迷,mamicode.com
首页 > Windows程序 > 详细

C# 发送邮件通知

时间:2020-07-23 16:34:07      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:from   pac   span   contex   pwd   aci   fail   alt   sys   

web.config
   <!-- 配置发邮件:邮件访问登录地址-->
    <add key="HttpsUrl" value="http://localhost:3334/Frame/Login.aspx" />
    <add key="IsSendMail" value="1" />
  <!--qq邮箱-->
    <add key="MailUser" value="xxcc@qq.com" />
    <add key="MailName" value="邮件通知" />
    <add key="MailHost" value="smtp.qq.com" />
    <add key="HostPort" value="25" />
 <!-- qq邮箱码-->
     <add key="MailPwd" value="测试自己邮箱密码" /> 
    
    
技术图片

 

  公共方法 ConfigHelper

public class ConfigHelper { /// <summary> /// 根据Key取Value值 /// </summary> /// <param name="key"></param> public static string GetValue(string key) { return ConfigurationManager.AppSettings[key].ToString().Trim(); } /// <summary> /// 根据Key修改Value /// </summary> /// <param name="key">要修改的Key</param> /// <param name="value">要修改为的值</param> public static void SetValue(string key, string value) { System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument(); xDoc.Load(HttpContext.Current.Server.MapPath("/XmlConfig/Config.xml")); System.Xml.XmlNode xNode; System.Xml.XmlElement xElem1; System.Xml.XmlElement xElem2; xNode = xDoc.SelectSingleNode("//appSettings"); xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key=‘" + key + "‘]"); if (xElem1 != null) xElem1.SetAttribute("value", value); else { xElem2 = xDoc.CreateElement("add"); xElem2.SetAttribute("key", key); xElem2.SetAttribute("value", value); xNode.AppendChild(xElem2); } xDoc.Save(HttpContext.Current.Server.MapPath("/XmlConfig/Config.xml")); } /// <summary> /// 修改web.config的配置值 /// </summary> /// <param name="key"></param> /// <param name="value"></param> public static void SetWebConfigValue(string key, string value) { Configuration objConfig = WebConfigurationManager.OpenWebConfiguration("~"); AppSettingsSection objAppSettings = (AppSettingsSection)objConfig.GetSection("appSettings"); if (objAppSettings != null) { //删除name,然后添加新值 objAppSettings.Settings.Remove(key); objAppSettings.Settings.Add(key, value); //保存配置文件 objConfig.Save(); } } }
   发送邮件
///
<summary> /// 发送邮件 /// </summary> public class SMTPManager { public static string MailUser = ConfigHelper.GetValue("MailUser"); public static string MailName = ConfigHelper.GetValue("MailName"); public static string MailHost = ConfigHelper.GetValue("MailHost"); public static string MailPwd = ConfigHelper.GetValue("MailPwd"); public static string IsSendMail = ConfigHelper.GetValue("IsSendMail"); public static string HostPort = ConfigHelper.GetValue("HostPort"); public static string EnableSsl = ConfigHelper.GetValue("EnableSsl"); /// <summary> /// 发送邮件 /// </summary> /// <param name=" sender">发送人、支持发送多个人每个地址用 ; 号隔开</param> /// <param name="Name">名称</param> /// <param name="Describe">内容</param> /// <param name="File_Path">附件</param> /// <returns></returns> public static bool MailSending(string Sender, string Name, string Describe, string File_Path) { if(IsSendMail !="1") { return false; } try { MailAddress from = new MailAddress(MailUser, MailName); //邮件的发件人 MailMessage mail = new MailMessage(); //设置邮件图片 AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(Describe, null, "text/html"); LinkedResource lrImage = new LinkedResource(AppDomain.CurrentDomain.BaseDirectory + "/Themes/Images/"+ConfigHelper.GetValue("EmailPhoto"), "image/gif"); lrImage.ContentId = "webimg"; //此处的ContentId 对应 htmlBodyContent 内容中的 cid: ,如果设置不正确,请不会显示图片 htmlBody.LinkedResources.Add(lrImage); mail.AlternateViews.Add(htmlBody); //设置邮件的标题 mail.Subject = Name;//任务名称 //设置邮件的发件人 //Pass:如果不想显示自己的邮箱地址,这里可以填符合mail格式的任意名称,真正发mail的用户不在这里设定,这个仅仅只做显示用 mail.From = from; //设置邮件的收件人 string address = ""; string displayName = ""; /**/ /* 这里这样写是因为可能发给多个联系人,每个地址用 ; 号隔开 一般从地址簿中直接选择联系人的时候格式都会是 :用户名1 < mail1 >; 用户名2 < mail 2>; 因此就有了下面一段逻辑不太好的代码 如果永远都只需要发给一个收件人那么就简单了 mail.To.Add("收件人mail"); */ string[] mailNames = (Sender + ";").Split(;); foreach (string name in mailNames) { if (name != string.Empty) { if (name.IndexOf(<) > 0) { displayName = name.Substring(0, name.IndexOf(<)); address = name.Substring(name.IndexOf(<) + 1).Replace(>, ); } else { displayName = string.Empty; address = name.Substring(name.IndexOf(<) + 1).Replace(>, ); } mail.To.Add(new MailAddress(address, displayName)); } } //设置邮件的抄送收件人 //这个就简单多了,如果不想快点下岗重要文件还是CC一份给领导比较好 //mail.CC.Add(new MailAddress("Manage@hotmail.com", "尊敬的领导"); //设置邮件的内容 mail.Body = FormatDescribe(Describe); //设置邮件的格式 mail.BodyEncoding = System.Text.Encoding.UTF8; mail.IsBodyHtml = true; //设置邮件的发送级别 mail.Priority = MailPriority.Normal; //设置邮件的附件,将在客户端选择的附件先上传到服务器保存一个,然后加入到mail中 if (File_Path != "") { mail.Attachments.Add(new Attachment(File_Path)); mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess; } SmtpClient client = new SmtpClient(); //设置用于 SMTP 事务的主机的名称,填IP地址也可以了 client.Host = MailHost; //设置用于 SMTP 事务的端口,默认的是 25 client.Port = int.Parse(HostPort); client.EnableSsl = EnableSsl == "1" ? true : false; client.UseDefaultCredentials = false; //这里才是真正的邮箱登陆名和密码, 用户名为 MailUser 密码是 MailPwd string strMailPwd = MailPwd; client.Credentials = new System.Net.NetworkCredential(MailUser, strMailPwd.Trim()); client.DeliveryMethod = SmtpDeliveryMethod.Network; ////如果发送失败,SMTP 服务器将发送 失败邮件 mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; //都定义完了,正式发送了 client.Send(mail); return true; } catch (Exception ex) { throw new Exception(ex.Message); } } /// <summary> /// 格式化邮件内容 /// </summary> /// <param name="Describe"></param> /// <returns></returns> public static string FormatDescribe(string Describe) { StringBuilder sb = new StringBuilder(); sb.Append("<table cellSpacing=0 cellPadding=0 width=100% border=1>"); sb.Append("<tr>"); sb.Append("<td bgColor=#ffffff colSpan=4 height=25>【管理系统】</td>"); sb.Append("</tr>"); sb.Append("<TR>"); sb.Append("<TD align=right width=9% bgColor=#eef7ff height=25>主 题</TD>"); sb.Append("<TD id=title bgColor=#ffffff colSpan=3><FONT face=宋体>"); sb.Append("管理邮件通知"); sb.Append("</FONT></TD>"); sb.Append("</TR>"); sb.Append("<TR>"); sb.Append("<TD align=right width=9% bgColor=#eef7ff height=250>内容</TD>"); sb.Append("<TD id=title bgColor=#ffffff colSpan=3><FONT face=宋体>"); sb.Append(Describe); sb.Append("</FONT></TD>"); sb.Append("</TR>"); sb.Append("</table>"); return sb.ToString(); } /// <summary> /// 发送电子邮件 /// </summary> /// <param name="smtpserver">SMTP服务器</param> /// <param name="userName">登录帐号</param> /// <param name="pwd">登录密码</param> /// <param name="nickName">发件人昵称</param> /// <param name="strfrom">发件人</param> /// <param name="strto">收件人</param> /// <param name="subj">主题</param> /// <param name="bodys">内容</param> public static void SendMail(string smtpserver, string userName, string pwd, string nickName, string strfrom, string strto, string subj, string bodys) { try { SmtpClient _smtpClient = new SmtpClient(); _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式 _smtpClient.Host = smtpserver;//指定SMTP服务器 _smtpClient.Credentials = new System.Net.NetworkCredential(userName, pwd);//用户名和密码 //MailMessage _mailMessage = new MailMessage(strfrom, strto); MailAddress _from = new MailAddress(strfrom, nickName); MailAddress _to = new MailAddress(strto); MailMessage _mailMessage = new MailMessage(_from, _to); _mailMessage.Subject = subj;//主题 _mailMessage.Body = bodys;//内容 _mailMessage.BodyEncoding = System.Text.Encoding.Default;//正文编码 _mailMessage.IsBodyHtml = true;//设置为HTML格式 _mailMessage.Priority = MailPriority.Normal;//优先级 _smtpClient.Send(_mailMessage); } catch (Exception ex) { throw new Exception(ex.Message); } } }

 

C# 发送邮件通知

标签:from   pac   span   contex   pwd   aci   fail   alt   sys   

原文地址:https://www.cnblogs.com/fhrfhr/p/13365432.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!