我正在嘗試按照此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("[email protected]", "subject", "something");
return Ok(await _emailSender.SendEmailAsync("[email protected]", "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);
}
}
uj5u.com熱心網友回復:
您沒有回傳private您擁有的方法的結果:
await Execute(Options.ApiKey, subject, message, email);
所以,你需要回傳結果
public async Task SendEmailAsync(
string email,
string subject,
string message)
{
result = await Execute(Options.ApiKey, subject, message, email);
// do some checks with result
}
如果您需要檢查控制器代碼中的結果,這將更加棘手,因為 的簽名IEmailSender不提供通用任務物件,您需要手動轉換它(不推薦)。您可以簡單地假設方法完成后發送成功(因為在其他情況下您會得到例外):
public async Task<ActionResult> SendEmail() {
await _emailSender.SendEmailAsync("[email protected]", "subject", "something");
// email was sent, no exception
return Ok();
}
如果您需要該方法的回應,您可以使用 執行類似此答案的操作_emailSender.SendEmailAsync("[email protected]", "subject", "something"),而不使用await構造(仍然不能推薦這種方法):
/// <summary> /// Casts a <see cref="Task"/> to a <see cref="Task{TResult}"/>. /// This method will throw an <see cref="InvalidCastException"/> if the specified task /// returns a value which is not identity-convertible to <typeparamref name="T"/>. /// </summary> public static async Task<T> Cast<T>(this Task task) { if (task == null) throw new ArgumentNullException(nameof(task)); if (!task.GetType().IsGenericType || task.GetType().GetGenericTypeDefinition() != typeof(Task<>)) throw new ArgumentException("An argument of type 'System.Threading.Tasks.Task`1' was expected"); await task.ConfigureAwait(false); object result = task.GetType().GetProperty(nameof(Task<object>.Result)).GetValue(task); return (T)result; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/484850.html
