C# 发送邮件通知
标签:from pac span contex pwd aci fail alt sys
web.config
"HttpsUrl" value="http://localhost:3334/Frame/Login.aspx" />
"IsSendMail" value="1" />
"MailUser" value="xxcc@qq.com" />
"MailName" value="邮件通知" />
"MailHost" value="smtp.qq.com" />
"HostPort" value="25" />
"MailPwd" value="测试自己邮箱密码" />
公共方法 ConfigHelper
public class ConfigHelper
{
///
/// 根据Key取Value值
///
///
public static string GetValue(string key)
{
return ConfigurationManager.AppSettings[key].ToString().Trim();
}
///
/// 根据Key修改Value
///
/// 要修改的Key
/// 要修改为的值
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"));
}
///
/// 修改web.config的配置值
///
///
///
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();
}
}
}
发送邮件
///
/// 发送邮件
///
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");
///
/// 发送邮件
///
/// 发送人、支持发送多个人每个地址用 ; 号隔开
/// 名称
/// 内容
/// 附件
///
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 ; 用户名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);
}
}
///
/// 格式化邮件内容
///
///
///
public static string FormatDescribe(string Describe)
{
StringBuilder sb = new StringBuilder();
sb.Append("
");
sb.Append("
");
sb.Append("【管理系统】 |
");
sb.Append("
");
sb.Append("
");
sb.Append("主 题 |
");
sb.Append("");
sb.Append("管理邮件通知");
sb.Append(" |
");
sb.Append("
");
sb.Append("
");
sb.Append("内容 |
");
sb.Append("");
sb.Append(Describe);
sb.Append(" |
");
sb.Append("
");
sb.Append("
");
return sb.ToString();
}
///
/// 发送电子邮件
///
/// SMTP服务器
/// 登录帐号
/// 登录密码
/// 发件人昵称
/// 发件人
/// 收件人
/// 主题
/// 内容
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
文章来自:
搜素材网的
编程语言模块,转载请注明文章出处。
文章标题:
C# 发送邮件通知
文章链接:http://soscw.com/index.php/essay/38442.html
评论