用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", "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中实现邮件发送功能的更多相关文章
- 用ASP.NET Core 1.0中实现邮件发送功能-阿里云邮件推送篇
在上篇中用MailKit实现了Asp.net core 邮件发送功能,但一直未解决阿里云邮件推送问题,提交工单一开始的回复不尽如人意,比如您的网络问题,您的用户名密码不正确等,但继续沟通下阿里云客户还 ...
- 避免在ASP.NET Core 3.0中为启动类注入服务
本篇是如何升级到ASP.NET Core 3.0系列文章的第二篇. Part 1 - 将.NET Standard 2.0类库转换为.NET Core 3.0类库 Part 2 - IHostingE ...
- ASP.NET Core 1.0 中的依赖项管理
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
- 在ASP.NET Core 1.0中如何发送邮件
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:目前.NET Core 1.0中并没有提供SMTP相关的类库,那么要如何从ASP.NE ...
- ASP.NET Core 1.0 中使用 Swagger 生成文档
github:https://github.com/domaindrivendev/Ahoy 之前文章有介绍在ASP.NET WebAPI 中使用Swagger生成文档,ASP.NET Core 1. ...
- 在ASP.NET Core 2.0中使用CookieAuthentication
在ASP.NET Core中关于Security有两个容易混淆的概念一个是Authentication(认证),一个是Authorization(授权).而前者是确定用户是谁的过程,后者是围绕着他们允 ...
- 如何在ASP.NET Core 2.0中使用Razor页面
如何在ASP.NET Core 2.0中使用Razor页面 DotNetCore2017-11-22 14:49 问题 如何在ASP.NET Core 2.0中使用Razor页面 解 创建一个空的项 ...
- ASP.NET Core 3.0中使用动态控制器路由
原文:Dynamic controller routing in ASP.NET Core 3.0 作者:Filip W 译文:https://www.cnblogs.com/lwqlun/p/114 ...
- asp.net core 3.0 中使用 swagger
asp.net core 3.0 中使用 swagger Intro 上次更新了 asp.net core 3.0 简单的记录了一下 swagger 的使用,那个项目的 api 比较简单,都是匿名接口 ...
随机推荐
- 冒泡排序优化JAVA
本文对传统的冒泡排序进行了一些优化,减少了循环次数. 时间复杂度 若文件的初始状态是正序的,一趟扫描即可完成排序.所需的关键字比较次数 C 和记录移动次数 M 均达到最小值: C(min)=n-1 , ...
- Please set registry key HKLM\Microsoft\.NET Framework\InstallRoot to point to the .NET Framework
安装.NET程序时会提示“Please set registry key HKLM\Microsoft\.NET Framework\InstallRoot to point to the .NET ...
- io多路复用,select,笔记
一下代码,是摘自大王的博客,(http://www.cnblogs.com/alex3714/)我自己有加了些注释. 1 2 3 #_*_coding:utf-8_*_ 4 5 __author__ ...
- ubuntu 挂载新硬盘
http://www.cnblogs.com/hnrainll/archive/2012/02/27/2369331.html
- SR触发器和JK触发器
SR触发器(电平触发器) 基本RS触发器的逻辑方程为:Q(n+1)=一S+RQ(n); 约束方程:R+S=1; 根据上述两个式子得到它的四种输入与输出的关系: 1.当R端有效(0),S端无效时(1), ...
- 【Leetcode】【Hard】Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...
- Asp.Net Web API 2 官网菜鸟学习系列导航[持续更新中]
详情请查看http://aehyok.com/Blog/Detail/67.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...
- 【Win10】解决 模拟器调试手机 错误-> 引导阶段... 无法找到指定路径......\2052\msdbgui.dll
去弄几天的Web服务,又弄了几天的CefSharp,都是给其它组的同学做了一下支持,终于又可以回来玩下Win10啦. 今天想试一下UWP在手机上的效果,就找了台WP手机开始升级,结果下载速度遥遥无期, ...
- EF Code First Migration总结
开启Migration 1. 通过 Tools->Nuget Package Manager->Package Manager Console 打开Package Manager Cons ...
- Redis教程(十):持久化详解
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/137.html 一.Redis提供了哪些持久化机制: 1). RDB持久化 ...