准备将一些项目迁移到 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", "mail@hantianwei.cn"));
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", , true);
client.Authenticate("mail@hantianwei.cn", "******"); 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", "mail@hantianwei.cn"));
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", , SecureSocketOptions.None).ConfigureAwait(false);
await client.AuthenticateAsync("mail@hantianwei.cn", "******");
await client.SendAsync(emailMessage).ConfigureAwait(false);
await client.DisconnectAsync(true).ConfigureAwait(false); }
} }
}

以上代码同步异步都没有问题

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

用ASP.NET Core 1.0中实现邮件发送功能的更多相关文章

  1. 用ASP.NET Core 1.0中实现邮件发送功能-阿里云邮件推送篇

    在上篇中用MailKit实现了Asp.net core 邮件发送功能,但一直未解决阿里云邮件推送问题,提交工单一开始的回复不尽如人意,比如您的网络问题,您的用户名密码不正确等,但继续沟通下阿里云客户还 ...

  2. 避免在ASP.NET Core 3.0中为启动类注入服务

    本篇是如何升级到ASP.NET Core 3.0系列文章的第二篇. Part 1 - 将.NET Standard 2.0类库转换为.NET Core 3.0类库 Part 2 - IHostingE ...

  3. ASP.NET Core 1.0 中的依赖项管理

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  4. 在ASP.NET Core 1.0中如何发送邮件

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:目前.NET Core 1.0中并没有提供SMTP相关的类库,那么要如何从ASP.NE ...

  5. ASP.NET Core 1.0 中使用 Swagger 生成文档

    github:https://github.com/domaindrivendev/Ahoy 之前文章有介绍在ASP.NET WebAPI 中使用Swagger生成文档,ASP.NET Core 1. ...

  6. 在ASP.NET Core 2.0中使用CookieAuthentication

    在ASP.NET Core中关于Security有两个容易混淆的概念一个是Authentication(认证),一个是Authorization(授权).而前者是确定用户是谁的过程,后者是围绕着他们允 ...

  7. 如何在ASP.NET Core 2.0中使用Razor页面

    如何在ASP.NET Core 2.0中使用Razor页面  DotNetCore2017-11-22 14:49 问题 如何在ASP.NET Core 2.0中使用Razor页面 解 创建一个空的项 ...

  8. ASP.NET Core 3.0中使用动态控制器路由

    原文:Dynamic controller routing in ASP.NET Core 3.0 作者:Filip W 译文:https://www.cnblogs.com/lwqlun/p/114 ...

  9. asp.net core 3.0 中使用 swagger

    asp.net core 3.0 中使用 swagger Intro 上次更新了 asp.net core 3.0 简单的记录了一下 swagger 的使用,那个项目的 api 比较简单,都是匿名接口 ...

随机推荐

  1. [转]深入理解mysqldump原理 --single-transaction --lock-all-tables --master-data

    本文转至:http://blog.csdn.net/cug_jiang126com/article/details/49824471 在mysqldump过程中,之前其实一直不是很理解为什么加了--s ...

  2. Git 常用几个操作

    获取 git clone git@github.com:XXX/learning.git 更新 git pull  添加 git add XXX 上传本地 git commit -m "ap ...

  3. 以太网客户端提示windows系统自带共享代理解决方法

    以太网客户端(Dr.COM)登陆出现windows系统自带共享代理,如下图: 系统win+R调出运行: 在服务中,找到Internet Connection Sharing (ICS): 右键属性,将 ...

  4. 采用动态代理方式调用WEB服务(转载+整理)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. Android View 如何绘制

    上文说道了Android如何测量,但是一个漂亮的控件我只知道您长到哪儿,这当然不行.只需要简单重写OnDraw方法,并在Canvas(画布)对象上调用那根五颜六色的画笔就能够画出这控件"性感 ...

  6. CentOS7 安装 mongodb

    https://docs.mongodb.com/manual/tutorial/install-mongodb-enterprise-on-red-hat/

  7. node-webkit教程(14)禁用缓存

    1.在开发者工具中禁用缓存 上面这张图,是在node-webkit 中 在开发工具中配置禁用缓存的选项. 使用这个选项可以有效的禁用所有页面缓存. 2. 在配置文件中,配置webkit 缓存禁用和启用 ...

  8. 使用VS2013在WIN8.1上运行gaclib的hello world

    首先:gaclib的官网是http://www.gaclib.net/ 需要了解更多信息的请自己去官网,我也是刚刚研究   第一步 下载gaclib的源码   这些文件是运行程序所必须的   第二步 ...

  9. c++如何遍历删除map/vector里面的元素

    新技能Get! 问题 对于c++里面的容器, 我们可以使用iterator进行方便的遍历. 但是当我们通过iterator对vector/map等进行修改时, 我们就要小心了, 因为操作往往会导致it ...

  10. create mystic by Django

    See the sumary below figure : We going to talk each step ...more detail