华域联盟 .Net ASP.NET Core 1.0实现邮件发送功能

ASP.NET Core 1.0实现邮件发送功能

准备将一些项目迁移到 asp.net core 先从封装类库入手,在遇到邮件发送类时发现在 asp.net core 1.0中并示提供SMTP相关类库,于是网上一搜发现了MailKit 

好东西一定要试一下,何况是开源,下面是代码可实现SMTP邮件发送: 

using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;
using System.Threading.Tasks;

namespace ConsoleApp1
{
 public class MailHelper
 {
  public static void Send(string email, string subject, string message)
  {
   var emailMessage = new MimeMessage();
   emailMessage.From.Add(new MailboxAddress("tianwei blogs", "[email protected]"));
   emailMessage.To.Add(new MailboxAddress("mail", email));
   emailMessage.Subject = subject;
   emailMessage.Body = new TextPart("plain") { Text = message };

   using (var client = new SmtpClient())
   {
    client.Connect("smtp.hantianwei.cn", 465, true);
    client.Authenticate("[email protected]", "******");

    client.Send(emailMessage);
    client.Disconnect(true);

   }
  }

  public static async Task SendEmailAsync(string email, string subject, string message)
  {
   var emailMessage = new MimeMessage();

   emailMessage.From.Add(new MailboxAddress("tianwei blogs", "[email protected]"));
   emailMessage.To.Add(new MailboxAddress("mail", email));
   emailMessage.Subject = subject;
   emailMessage.Body = new TextPart("plain") { Text = message };

   using (var client = new SmtpClient())
   {
    await client.ConnectAsync("smtp.hantianwei.cn", 25, SecureSocketOptions.None).ConfigureAwait(false);
    await client.AuthenticateAsync("[email protected]", "******");
    await client.SendAsync(emailMessage).ConfigureAwait(false);
    await client.DisconnectAsync(true).ConfigureAwait(false);
    
   }
  }

 }
} 

以上代码同步异步都没有问题
 注:一般邮箱如腾讯企业邮、163等都可以发送成功,但阿里云邮件推送失败,如果有高手可实现阿里云推送邮件请告诉我一下,非常感谢!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持华域联盟。

您可能感兴趣的文章:

  • Asp.net发送邮件的两种方法小结
  • 在asp.NET 中使用SMTP发送邮件的实现代码
  • asp.net2.0实现邮件发送(测试成功)
  • 在ASP.NET2.0中通过Gmail发送邮件的代码
  • Asp.Net类库中发送电子邮件的代码
  • 用ASP.NET做的个性化的邮件发送系统
  • asp.net mvc发送邮件实例讲解
  • ASP.NET MVC 5之邮件服务器与客户端
  • asp.net基于windows服务实现定时发送邮件的方法
  • ASP.NET邮件发送system.Net.Mail案例
  • 创建基于ASP.NET的SMTP邮件服务的具体方法
  • asp.net实现的群发邮件功能详解

本文由 华域联盟 原创撰写:华域联盟 » ASP.NET Core 1.0实现邮件发送功能

转载请保留出处和原文链接:https://www.cnhackhy.com/69625.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部