【问题标题】:Sending an email with SendGrid API from ASP.Net Framework 3.5从 ASP.Net Framework 3.5 使用 SendGrid API 发送电子邮件
【发布时间】:2017-10-29 22:08:10
【问题描述】:

我正在尝试使用带有 API 密钥的 SendGrid API 发送电子邮件。

问题是:我必须从旧的 .net 应用程序中执行此操作,其 asp.net 框架版本是 3.5(并且不能更改框架版本)

我找不到有关如何实现它的有用信息。我发现的唯一代码使用SendGrid csharp libraries,这些不支持asp.net framework 3.5

这是我找到 here 的代码示例,但我无法从我的 .net 3.5 网络应用程序中完成这项工作:

// using SendGrid's C# Library - https://github.com/sendgrid/sendgrid-csharp
using System.Net.Http;
using System.Net.Mail;

var myMessage = new SendGrid.SendGridMessage();
myMessage.AddTo("test@sendgrid.com");
myMessage.From = new MailAddress("you@youremail.com", "First Last");
myMessage.Subject = "Sending with SendGrid is Fun";
myMessage.Text = "and easy to do anywhere, even with C#";

var transportWeb = new SendGrid.Web("SENDGRID_APIKEY");
transportWeb.DeliverAsync(myMessage);
// NOTE: If you're developing a Console Application, use the following so that the API call has time to complete
// transportWeb.DeliverAsync(myMessage).Wait();

有什么想法吗?

【问题讨论】:

    标签: c# asp.net sendgrid sendgrid-api-v3 sendgrid-api-v2


    【解决方案1】:

    您有几个选择。 SendGrid API 使用 HttpClient 类发出请求,这需要 .NET 4+ 依赖项。

    1. 您可以尝试使用 RestSharp 实现自己的实现,这与 .NET 3.5 兼容,并且 SendGrid API 使用您可以实现的接口。它只需要从github上的源代码进行调整。

      1. 使用 .Net SMTP 类发送电子邮件并按照 SendGrid 文档中的指南进行配置。

      2. 通过另一个运行 .NET 4+ 的 WebAPI 代理请求,并通过实现您自己的 API 来简化进行这些调用所需的内容。使用 WebClient 类或 RestSharp 之类的东西进行调用。

    ** 废话,选项 1 比看起来更难 ** ISendGridClient 是一个有点奇怪的例子。它使用接口内部的实现来获取一些参数。看起来 2 和 3 是不错的选择!

    【讨论】:

    • 谢谢。对于选项#2,我相信您指的是this documentation,对吧?如果是这种情况,是否可以使用此方法发送,但使用 API 密钥而不是提供用户名和密码?
    猜你喜欢
    • 1970-01-01
    • 2016-11-30
    • 2019-06-14
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    相关资源
    最近更新 更多