我正在try 根据这https://www.ryadel.com/en/asp-net-core-send-email-messages-sendgrid-api/个教程创建一个控制器
除了控制器之外,我已经添加了所有内容.控制器中的代码是
public class SendGridController : BaseApiController
{
private readonly IEmailSender _emailSender;
public SendGridController(IEmailSender emailSender)
{
_emailSender = emailSender;
}
[HttpPost]
[ProducesResponseType(typeof(string), (int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(string), (int)HttpStatusCode.BadRequest)]
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
[ProducesResponseType(typeof(string), (int)HttpStatusCode.InternalServerError)]
public async Task<ActionResult> SendEmail() {
await _emailSender.SendEmailAsync("test@mail.com", "subject", "something");
return Ok(await _emailSender.SendEmailAsync("test@mail.com", "subject", "something"));
}
}
但我发现以下错误
Argument 1: cannot convert from 'void' to 'object'
我希望能够看到来自发送网格的响应,如果它已经发送或没有.
以下是SendGridEmailSender类:
public class SendGridEmailSender : IEmailSender
{
public SendGridEmailSender(
IOptions<SendGridEmailSenderOptions> options
)
{
this.Options = options.Value;
}
public SendGridEmailSenderOptions Options { get; set; }
public async Task SendEmailAsync(
string email,
string subject,
string message)
{
await Execute(Options.ApiKey, subject, message, email);
}
private async Task<Response> Execute(
string apiKey,
string subject,
string message,
string email)
{
var client = new SendGridClient(apiKey);
var msg = new SendGridMessage()
{
From = new EmailAddress(Options.SenderEmail, Options.SenderName),
Subject = subject,
PlainTextContent = message,
HtmlContent = message
};
msg.AddTo(new EmailAddress(email));
// disable tracking settings
// ref.: https://sendgrid.com/docs/User_Guide/Settings/tracking.html
msg.SetClickTracking(false, false);
msg.SetOpenTracking(false);
msg.SetGoogleAnalytics(false);
msg.SetSubscriptionTracking(false);
return await client.SendEmailAsync(msg);
}
}