• .net Core1.0 邮件发送


      今天一天早,公司需要将之前的.net Core项目增加一个预处理机制,就是当程序出现异常后给我们的开发人员发送邮件,因为今天写些关于.netCore上发送邮件。

      根据查阅资料发现在目前的Core1.0还没有直接发送邮件的类库,因此使用MailKit包来进行邮件的发送!

      1.首先使用Nuget将 NETCore.MailKit 引用到程序中,然后调用该程序包进行发送,具体方法在下面!

     1    #region 发送邮件
     2         /// <summary>
     3         /// 发送邮件
     4         /// </summary>
     5         /// <param name="email"></param>
     6         /// <param name="subject"></param>
     7         /// <param name="message"></param>
     8         public static void SendEmail(string email, string subject, string message)
     9         {
    10             var emailMessage = new MimeMessage();
    11             emailMessage.From.Add(new MailboxAddress(emailAddressName, emailUserName));
    12             string[] emailArray = email.Split(',');
    13             foreach (var item in emailArray)
    14             {
    15                 emailMessage.To.Add(new MailboxAddress(item));
    16             }
    17             emailMessage.Subject = subject;
    18             emailMessage.Body = new TextPart("plain") { Text = message };
    19             using (var client = new SmtpClient())
    20             {
    21                 client.Connect(emailHost, 465, true);
    22                 client.Authenticate(emailUserName, emailPassword);
    23                 client.Send(emailMessage);
    24                 client.Disconnect(true);
    25             }
    26         }
    27         /// <summary>
    28         /// 异步发送邮件
    29         /// </summary>
    30         /// <param name="email"></param>
    31         /// <param name="subject"></param>
    32         /// <param name="message"></param>
    33         /// <returns></returns>
    34         public static async Task SendEmailAsync(string email, string subject, string message)
    35         {
    36             var emailMessage = new MimeMessage();
    37             emailMessage.From.Add(new MailboxAddress(emailAddressName, emailUserName));
    38             emailMessage.To.Add(new MailboxAddress("mail", email));
    39             emailMessage.Subject = subject;
    40             emailMessage.Body = new TextPart("plain") { Text = message };
    41 
    42             using (var client = new SmtpClient())
    43             {
    44                 await client.ConnectAsync(emailHost, 25, SecureSocketOptions.None).ConfigureAwait(false);
    45                 await client.AuthenticateAsync(emailUserName, emailPassword);
    46                 await client.SendAsync(emailMessage).ConfigureAwait(false);
    47                 await client.DisconnectAsync(true).ConfigureAwait(false);
    48             }
    49         } 

    以上就是调用MailKit 的发送方法,希望对大家有帮助!!如有问题请留言!!

          

    每一天都是崭新的,我们的目标有多远,我们就能走多远,坚持!!
  • 相关阅读:
    BERT 简介 P1_李宏毅
    GAN_P4 Learning from Unpaired Data_李宏毅
    GAN_P3_李宏毅
    Generation P1_李宏毅
    GAN_P2_Theory behind GAN_李宏毅
    李宏毅_Transformer p2
    李宏毅_Transformer p1
    强类型数据集 官方教程
    网页管理系统一
    读张子阳的用户验证自定义IPrincipal和IIdentity有感
  • 原文地址:https://www.cnblogs.com/xiao-feng/p/6475053.html
Copyright © 2020-2023  润新知