我正在尝试从SendGrid发送基本的测试电子邮件。我的应用程序已连接到azure服务器,并且上面有SendGrid资源。我在SENDGRID_APIKEY下的应用程序设置中有我的api密钥,并且我知道我的应用程序已正确连接到azure服务器,因为基本的todolist示例可以正常工作。但是,即使运行它时没有出现任何编译器错误,我似乎也无法通过它发送电子邮件。我决定在应用程序启动时对其进行测试,因此它与应用程序的问题无关。
我发送电子邮件的代码
using System;
using Microsoft.WindowsAzure.MobileServices;
using Xamarin.Forms;
using static TestApp.Views.Styles.ToDoItem;
using System.Net.Http;
using System.Net.Mail;
using SendGrid;
using SendGrid.Helpers.Mail;
namespace TestApp
{
public partial class App : Application
{
public static MobileServiceClient MobileService =
new MobileServiceClient(
"https://kidneyapp.azurewebsites.net"
);
public App()
{
InitializeComponent();
CurrentPlatform.Init();
TodoItem item = new TodoItem { Text = "Awesome item" };
MobileService.GetTable<TodoItem>().InsertAsync(item);
var apiKey = System.Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
var client = new SendGridClient(apiKey);
var msg = new SendGridMessage()
{
From = new EmailAddress("test@example.com", "DX Team"),
Subject = "Hello World from the SendGrid CSharp SDK!",
PlainTextContent = "Hello, Email!",
HtmlContent = "<strong>Hello, Email!</strong>"
};
msg.AddTo(new EmailAddress("test@example.com", "Test User"));
var response = await client.SendEmailAsync(msg);
}
}
}