/// <summary>
/// 邮件实体
/// </summary>
public class EmailModel
{
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 编码
/// </summary>
public string Encode { get; set; }
/// <summary>
/// 服务器地址
/// </summary>
public string IP { get; set; }
/// <summary>
/// 账户
/// </summary>
public string Identity { get; set; }
/// <summary>
/// 密码
/// </summary>
public string Password { get; set; }
}
1 /// <summary>
2 /// 邮件服务缓存
3 /// </summary>
4 public class Email_Singleton
5 {
6 private static List<EmailModel> instance=null;
7 private static int index;
8
9 private Email_Singleton()
10 { }
11
12 public static EmailModel GetEmailModel()
13 {
14 EmailModel result = new EmailModel();
15 if (instance ==null)
16 {
17 if (instance == null)
18 {
19 XmlDocument xml = new XmlDocument();
20 string temp_url = System.AppDomain.CurrentDomain.BaseDirectory + "Emails.xml";
21 xml.Load(temp_url);
22 XmlNodeList xmlList = xml.SelectNodes("/Emails/Email");
23 instance = new List<EmailModel>();
24 if (xmlList != null && xmlList.Count >= 1)
25 {
26 EmailModel _temp;
27 foreach (XmlNode item in xmlList)
28 {
29 if (item.ChildNodes == null)
30 {
31 continue;
32 }
33
34 _temp = new EmailModel();
35 _temp.Name = item.Attributes["name"].Value;
36 _temp.Encode = item.Attributes["encode"].Value;
37 foreach (XmlNode item_ in item.ChildNodes)
38 {
39 switch (item_.Name)
40 {
41 case "ip":
42 _temp.IP = item_.InnerText;
43 break;
44 case "Identity":
45 _temp.Identity = item_.InnerText;
46 break;
47 case "Password":
48 _temp.Password = item_.InnerText;
49 break;
50 }
51 }
52
53 instance.Add(_temp);
54 }
55
56 if (instance.Count >= 1)
57 {
58 index = 0;
59 result = instance[index];
60 }
61 }
62 }
63 }
64 else
65 {
66 if (instance.Count>=1)
67 {
68 result = instance[index];
69 index++;
70 index = index >= instance.Count ? 0 : index;
71 }
72 }
73
74 ILog log = LogManager.GetLogger(PublicEnum.SystemLog.ErrorLog.ToString());
75 log.Info("发送邮箱:" + result.Identity + ",名称:" + result.Name);
76 return result;
77 }
78 }
1 /// <summary>
2 /// 发送邮件类
3 /// </summary>
4 public class Email
5 {
6 /// <summary>
7 /// 发送邮件
8 /// </summary>
9 /// <param name="userid">接收人邮件</param>
10 /// <param name="content">发送内容</param>
11 /// <param name="subject">发送主题,可为空,使用默认主题</param>
12 /// <param name="csuserEmailList">抄送人邮件列表</param>
13 public static void sendemail(string userEmail, string content, string subject,string csuserEmailList)
14 {
15 MailMessage mail = new MailMessage();
16 try
17 {
18 string dest;
19 dest = userEmail;
20 EmailModel mail_model= Email_Singleton.GetEmailModel();
21 if (mail_model == null)
22 {
23 return;
24 }
25
26 mail.From = new MailAddress(mail_model.Identity, mail_model.Name, Encoding.GetEncoding(mail_model.Encode));
27 mail.To.Add(new MailAddress(dest, userEmail, Encoding.GetEncoding(mail_model.Encode)));
28 if (!string.IsNullOrEmpty(csuserEmailList))//发送给抄送人
29 {
30 string[] sp_readers = csuserEmailList.Split(',');
31 string csuseremil = "";
32 foreach (string str in sp_readers)
33 {
34 if (!string.IsNullOrEmpty(str))
35 {
36 csuseremil = str;
37
38 mail.CC.Add(new MailAddress(csuseremil, str, System.Text.Encoding.GetEncoding(mail_model.Encode)));
39 }
40 }
41 }
42
43 mail.Subject = subject == "" ? "测试平台系统邮件" : subject.Trim();
44 mail.Body = content;
45 mail.IsBodyHtml = true;
46 mail.Priority = MailPriority.High;
47 mail.SubjectEncoding = Encoding.GetEncoding(mail_model.Encode);
48 mail.BodyEncoding = Encoding.GetEncoding(mail_model.Encode);
49 using (SmtpClient smtp = new SmtpClient(mail_model.IP))
50 {//邮箱ip地址
51 smtp.EnableSsl = true;
52 smtp.UseDefaultCredentials = false;
53 smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
54 smtp.Credentials = new System.Net.NetworkCredential(mail_model.Identity, mail_model.Password);
55 ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, error) =>
56 {//为错误:根据验证过程,远程证书无效
57 return true;
58 };
59 smtp.Send(mail);
60 ILog log = LogManager.GetLogger(PublicEnum.SystemLog.MessageLog.ToString());
61 log.Info("收件人" + userEmail + "的邮件发送成功;。服务邮箱:" + mail_model.Identity);
62 }
63 }
64 catch (Exception ex)
65 {
66 ILog log = LogManager.GetLogger(PublicEnum.SystemLog.ErrorLog.ToString());
67 log.Info("收件人" + userEmail + "的邮件发送失败。详细信息:" + ex.Message + ex.Source);
68 }
69 finally {
70 mail.Dispose();
71 }
72 }
73 }