|
@@ -0,0 +1,103 @@
|
|
|
+/*-------------------------------------------------------------------------
|
|
|
+ * 功能描述:SmtpMailUtil
|
|
|
+ * 作者:xulisong
|
|
|
+ * 创建时间: 2019/3/20 14:55:01
|
|
|
+ * 版本号:v1.0
|
|
|
+ * -------------------------------------------------------------------------*/
|
|
|
+
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+using System.Net.Mail;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+
|
|
|
+namespace SAGA.DotNetUtils.Utilities
|
|
|
+{
|
|
|
+ public class SmtpMailOptions
|
|
|
+ {
|
|
|
+ public SmtpMailOptions()
|
|
|
+ {
|
|
|
+ Host = "smtp.163.com";
|
|
|
+ UserName = "sagaproblem@163.com";
|
|
|
+ Password = "saga123";
|
|
|
+ }
|
|
|
+ public string Host { get; set; }
|
|
|
+ public string UserName { get; set; }
|
|
|
+ public string Password { get; set; }
|
|
|
+ }
|
|
|
+ public class SmtpMailUtil
|
|
|
+ {
|
|
|
+ /// <summary>
|
|
|
+ /// 邮箱相关设置
|
|
|
+ /// </summary>
|
|
|
+ private static SmtpMailOptions MailOptions { get; set; }
|
|
|
+
|
|
|
+ static SmtpMailUtil()
|
|
|
+ {
|
|
|
+ MailOptions = new SmtpMailOptions();
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 是否已经初始化
|
|
|
+ /// </summary>
|
|
|
+ public static bool IsInited { get; private set; }
|
|
|
+ public static void InitSmtpMail(SmtpMailOptions options)
|
|
|
+ {
|
|
|
+ MailOptions = options;
|
|
|
+ IsInited = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static SmtpClient CreateSmtpClient()
|
|
|
+ {
|
|
|
+ #region 服务器相关描述
|
|
|
+ //使用163的SMTP服务器发送邮件
|
|
|
+ ////163的SMTP服务器需要用163邮箱的用户名和smtp授权码作认证,如果没有需要去163申请个,
|
|
|
+ ///这里需要注意,163似乎有规定发信人的邮箱地址必须是163的,而且发信人的邮箱用户名必须和上面SMTP服务器认证时的用户名相同
|
|
|
+ //因为上面用的用户名abc作SMTP服务器认证,所以这里发信人的邮箱地址也应该写为abc@163.com
|
|
|
+ #endregion
|
|
|
+ SmtpClient client = new SmtpClient();
|
|
|
+ client.Host = MailOptions.Host;
|
|
|
+ client.UseDefaultCredentials = true;
|
|
|
+ client.DeliveryMethod = SmtpDeliveryMethod.Network;
|
|
|
+ client.Credentials = new System.Net.NetworkCredential(MailOptions.UserName, MailOptions.Password);
|
|
|
+ return client;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void Send(SmtpClient client, MailMessage mainMessage)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ client.Send(mainMessage);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ string message = DateTime.Now + " ;Source:" + ex.Source + " ;HelpLink:" + ex.HelpLink +
|
|
|
+ " ;Message:" + ex.Message;
|
|
|
+ var name = System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName;
|
|
|
+ string temp =Path.Combine(Environment.GetEnvironmentVariable("TEMP"), name,"Mail.txt");
|
|
|
+ var dir = Directory.GetParent(temp);
|
|
|
+ if (!dir.Exists)
|
|
|
+ {
|
|
|
+ dir.Create();
|
|
|
+ }
|
|
|
+ File.AppendAllText(temp,message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public static void Send(string target, string subject, string bodyContent)
|
|
|
+ {
|
|
|
+ SmtpClient client = CreateSmtpClient();
|
|
|
+ MailMessage message = new MailMessage();
|
|
|
+ message.From = new MailAddress(MailOptions.UserName);
|
|
|
+ message.To.Add(target);//将邮件发送给Gmail
|
|
|
+
|
|
|
+ message.Subject = subject;
|
|
|
+ message.Body = bodyContent;
|
|
|
+ message.SubjectEncoding = Encoding.UTF8;
|
|
|
+ message.BodyEncoding = Encoding.UTF8;
|
|
|
+ message.Priority = MailPriority.High;
|
|
|
+ message.IsBodyHtml = true; //可以为html
|
|
|
+ Send(client, message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|