Send mail via SendGrid on Azure

What is SendGrid?

SendGrid is a cloud-based email service that provides transaction-based, reliable email delivery.

And it has the ability of scalability and real-time analysis. Common use cases are:

  • Auto-reply to users' emails
  • Send messages to users periodically
  • generate reports, etc.

 

Because the use of third-party mail services such as SendGrid is stable and reliable without the need to maintain the mail server, and for applications that send a small amount of mail, the number of free emails sent (25,000 per month) can already meet the demand. So more and more applications are beginning to use this mail service to send mail.

Let's start by creating a SendGrid account on Azure and introduce how to send emails through SendGrid.

Create a SendGrid account

Log in to the Azure management website and click "+" to create a SendGrid account. Type "SendGrid Email Delivery" in the search bar and hit enter:

Select "SendGrid Email Delivery" to enter the introduction interface:

In the introduction interface, you can read some basic information, of course, just click "Create" directly! At this point, enter the Create SendGrid details interface. We will not pay attention to the basic information of the interface. Please directly check the charging standard of F1 free in the Pricing tier. This is a genuine 25,000 free emails per month. Finally, click "Create" to complete the creation:

After the SendGrid account is created, let's check its Configurations information:

This information is needed when sending emails using SendGrid. First of all you will see that the username has been processed, we are using this processed username when accessing the SendGrid service. There is also the following SMTP SERVER, that is to say, when we create a SendGrid account, we can also send emails through the specified SMTP server in the traditional way.

Using SendGrid in a C# project

假如我们要在一个 asp.net 的应用中通过 SendGrid 发送邮件。那么在使用 SendGrid 服务之前,我们需要先在该项目中安装 SendGrid 提供的组件。请在 Visual Studio 的 Package Manager Console 中执行下面的命令:

> Install-Package Sendgrid -Version 6.3.4

注意,SendGrid 的 API 升级很快,我们这里使用的是 V2 版本的 API,所以要指定安装的版本。

发送测试邮件

万事俱备,让我们先发送一封普通的邮件试试:

// 请使用前面介绍的被 SendGrid 处理过的用户名。

string username = "xxxxx";

string password = "yyyyy";

var credentials = new NetworkCredential(username, password);

var transport = new Web(credentials);

 

var myMessage = new SendGridMessage();

myMessage.From = new MailAddress("fromaddress");

myMessage.AddTo("toaddress");

myMessage.Subject = "test";

myMessage.Html = "<p>Hello World!</p>";

 

// 通过异步方法发送邮件。

transport.DeliverAsync(myMessage);

代码很简单。需要注意的是,这段代码在你本地调试时,是无法发送邮件的。你需要把你的应用发布到 Azure 的 Web App 或是 Cloud Service 上才能成功发送邮件。

发送带附件的邮件

你可以使用 SendGridMessage.AddAttachment 方法,为你的邮件添加附件。这个方法有两个重载,分别是指定文件路径和文件流。如果你的程序运行在 Azure上,读取文件流的用例会多一些。

如果你还需要其它一些功能,请详细的阅读 SendGridMessage 类型的 API,它基本上能够满足你所有的需求。

已知问题

如果你有一个非常顽固的用户,他要求你发出的邮件内容必须是纯文本的形式 (主要是为了保持手动创建的各种换行和缩进),不能是 HTML 格式的,这将会是一个不小的挑战。因为 SendGrid 默认把邮件内容包成了 HTML 格式。不过我们还是有方法满足客户的,你可以尝试在每一行文本前面添加一个半角的空格,这也是 SendGrid 官方给出的方法。

其实,还有另外一种方式可以实现类似的效果。那就是把所有的文本内容放到 html 的<pre>元素中,代码也很简单:

message.Html = string.Format("<pre>{0}</pre>", System.Web.HttpUtility.HtmlEncode(message.Text));

总结

本文概要的介绍了在 Azure 上的 C# 应用中,如何使用 SendGrid 发送电子邮件。虽然代码不多,但描述了整个操作过程,希望对朋友们有所帮助。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326405671&siteId=291194637