惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
腾讯CDC
M
MIT News - Artificial intelligence
Y
Y Combinator Blog
L
LangChain Blog
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog
P
Proofpoint News Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
IT之家
IT之家
A
About on SuperTechFans
H
Help Net Security

博客园 - JustRun

Webpack中publicPath设置 忘记Mysql的root密码怎么办? Visual Studio 2015上安装Entity Framework Power Tools Ubuntu下安装中文输入法 Ubuntu如何选择更新源 Orchard中如何配置远端发布 .Net缓存管理框架CacheManager 全新的membership框架Asp.net Identity(2)——绕不过的Claims 全新的membership框架Asp.net Identity(1)——.Net membership的历史 泛型使用中,解决类型转换问题 使用Filter跟踪Asp.net MVC页面加载时间 下一代Asp.net开发规范OWIN(3)—— Middleware 下一代Asp.net开发规范OWIN(2)—— Katana介绍以及使用 下一代Asp.net开发规范OWIN(1)—— OWIN产生的背景以及简单介绍 对象映射工具AutoMapper介绍 AngularJs中,如何在render完成之后,执行Js脚本 .Net Collection的一些理解——记录一次向实习生的答疑 代替Reflection(反射)的一些方法 使用PhoneGap开启移动开发之旅
.Net中使用SendGrid Web Api发送邮件(附源码)
JustRun · 2014-10-18 · via 博客园 - JustRun

2014-10-18 11:03  JustRun  阅读(4934)  评论()    收藏  举报

SendGrid是一个第三方的解决邮件发送服务的提供商,在国外使用的比较普遍。国内类似的服务是SendCloud.
SendGrid提供的发送邮件方式主要是两种, 一种是SMTP API, 一种是Web Api. SMTP API是一种比较简单的方式,只要我们准备好Mail Message, 直接发送到SendGrid的邮件服务器就可以了,SendGrid的邮件服务器会帮我们投递。另外一种是Web Api的方式。

一般来说,很多三方的服务器提供商都会禁止链接外部25端口,这样你就没有办法连接SendGrid的SMTP服务器发送邮件了。在这种情况下,Web API就是一个很好的选择。SengGrid官方有较为详细的SMTP API Demo. Demo的地址是 https://github.com/sendgrid/sendgrid-csharp 由于没有Web API的Demo, 自己花时间自己写了一份,现在共享出来https://github.com/justrun1983/sendgrid-csharp-webapi

代码中使用了RestSharp, 一个非常方便在.Net中使用的访问Restful API的工具包。一个完整的发送邮件的代码如下, 包含cc, bcc和附件。

public class WebApiRestSharp
  {
      private const string ApiWebSite = "https://sendgrid.com";
      private const string ApiUrlAddress = "api/mail.send.json";

      public static void SendNormalHelloWorldEmail()
      {
          var client = new RestClient(ApiWebSite);
          var request = new RestRequest(ApiUrlAddress, Method.POST);
          request.AddParameter("api_user", Config.SendGridName);
          request.AddParameter("api_key", Config.SendGridPassword);
          request.AddParameter("to[]", Config.ToEmail);
          request.AddParameter("cc[]", Config.ToEmail);
          request.AddParameter("bcc[]", Config.ToEmail);
          request.AddParameter("subject", "Test");
          request.AddParameter("from", "test@test.me");
          request.AddParameter("text", "HelloWorld1");
          request.AddFile("files[2.txt]", @"C:\1.txt");

          // execute the request
          var response = client.Execute(request);
          var content = response.Content; // raw content as string
      }
  }

如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的推荐按钮。
如果,您希望更容易地发现我的新博客,不妨点击一下右下角的关注 Justrun
因为,我的写作热情也离不开您的肯定支持。

感谢您的阅读,这里还有你可能感兴趣的文章推荐博客文章