发邮件的邮件体以及记log

Posted Riusmary

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了发邮件的邮件体以及记log相关的知识,希望对你有一定的参考价值。

 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         
View Code

 

以上是关于发邮件的邮件体以及记log的主要内容,如果未能解决你的问题,请参考以下文章

linux PHPMailer 发送邮件sendmail 发邮件失败问题

Log4j配置发邮件功能

httpclient发邮件

log4j SMTPAppender控制发送邮件间隔

Java经验杂谈(1.记PostFix邮件发送性能与有效发送问题)

logback发邮件配置