1 public static bool SendEmail(string sender, string mailSubject, string mailAddress, string mailBody, MailPriority mailPriority = MailPriority.Normal, Attachment mailAttachment = null)
2 {
3 //var manager = new EmailManager();
4 var sw = Stopwatch.StartNew();
5 bool isSendSuccess = false;
6
7 try
8 {
9 isSendSuccess = CommonSendMail(sender, mailSubject, mailAddress, mailBody, mailPriority, mailAttachment);
10 }
11 catch (Exception ex)
12 {
13 sw.Stop();
14 }
15
16 return isSendSuccess;
17 }
18
19 public static bool CommonSendMail(string sender, string mailSubject, string mailAddress, string mailBody, MailPriority mailPriority = MailPriority.Normal, Attachment mailAttachment = null)
20 {
21 bool isSendSuccess = false;
22 MailMessage mail = new MailMessage();
23 //local test
24 //mail.From = new MailAddress("Admin@xxx.com", sender);
25 //SmtpClient mySmtp = new SmtpClient("xxx.xxx.loc");
26
27 //official
28 mail.From = new MailAddress("Admin@xxx.com", sender);
29 SmtpClient mySmtp = new SmtpClient("xxx.xxx.com");
30
31 string language = string.Empty;
32 string sMail = string.Empty;
33 string sError = string.Empty;
34 const int retryNumber = 3;
35
36 mail.BodyEncoding = Encoding.UTF8;
37
38 #region 设置发件人
39 mail.To.Add(mailAddress);
40 #endregion
41
42 #region 设置邮件标题
43 //设置邮件标题
44 mail.Subject = mailSubject;
45 #endregion
46
47 #region 设置邮件内容
48 mail.IsBodyHtml = true;
49 mail.Body = mailBody;
50 #endregion
51
52 //设置电子邮件优先级
53 mail.Priority = MailPriority.High;
54
55 #region 设置附件
56 if (mailAttachment != null)
57 mail.Attachments.Add(mailAttachment);
58 #endregion
59
60 for (var i = 0; i < retryNumber; i++)
61 {
62 //发送邮件
63 try
64 {
65 mySmtp.Send(mail);
66 isSendSuccess = true;
67 break;
68 }
69 catch (Exception ex)
70 {
71 sError = ex.Message;
72 isSendSuccess = false;
73 }
74 //finally
75 //{
76 // //记日志
77 //
78 //}
79 }
80
81 return isSendSuccess;
82 }
83