英文:
SendEmail using Sendgrid - SendEmailAsync
问题
我有一个代码,之前在Framework从4.5升级到4.7之前是可以工作的。现在不再工作了。有没有任何建议,我怎样才能让它工作。
没有错误,但是
response:
Id = 207, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}" System.Threading.Tasks.Task<SendGrid.Response>
public static void SendMail(EmailBuilder email)
{
try
{
/* CREDENTIALS
* ===================================================*/
string sgUsername = ConfigurationManager.AppSettings["SMTPUserName"];
string sgPassword = ConfigurationManager.AppSettings["SMTPPassword"];
/* CREATE THE MAIL MESSAGE
* ===================================================*/
var myMessage = new SendGridMessage();
var plainTextContent = "";
if (email.Style == 2)
plainTextContent = email.Body;
if (email.Style == 1)
plainTextContent = email.Body;
var apiKey = sgPassword;
var client = new SendGridClient(apiKey);
var from = new EmailAddress(ConfigurationManager.AppSettings["FromAddress"], ConfigurationManager.AppSettings["FromName"]);
var subject = email.EmailSubject;
var to = new EmailAddress(email.EmailAddress, email.FirstName + " " + email.LastName);
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, plainTextContent);
var response = client.SendEmailAsync(msg);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
我试图将其更改为 `public async Task SendMail`,但是然后我在从另一个类调用它时遇到了困难。
对于这个,它是 `Mailer.SendMail(email)`。
<details>
<summary>英文:</summary>
I have code that was working until I ungraded Framework from 4.5 to 4.7. Now it doesn't work any more. Are there any suggestion how I can make it work.
There is no error but
response:
Id = 207, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}" System.Threading.Tasks.Task<SendGrid.Response>
public static void SendMail(EmailBuilder email)
{
try
{
/* CREDENTIALS
* ===================================================*/
string sgUsername = ConfigurationManager.AppSettings["SMTPUserName"];
string sgPassword = ConfigurationManager.AppSettings["SMTPPassword"];
/* CREATE THE MAIL MESSAGE
* ===================================================*/
var myMessage = new SendGridMessage();
var plainTextContent ="" ;
if (email.Style == 2)
plainTextContent = email.Body;
if (email.Style == 1)
plainTextContent = email.Body;
var apiKey = sgPassword;
var client = new SendGridClient(apiKey);
var from = new EmailAddress(ConfigurationManager.AppSettings["FromAddress"], ConfigurationManager.AppSettings["FromName"]);
var subject = email.EmailSubject;
var to = new EmailAddress(email.EmailAddress, email.FirstName + " " + email.LastName);
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, plainTextContent);
var response = client.SendEmailAsync(msg);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
I tried to change that to public async Task SendMail
but then I have difficulties in calling that from another class.
For this one it was Mailer.SendMail(email)
答案1
得分: 2
如您调用异步方法,如果不想将返回类型更改为 Task
client.SendEmailAsync(msg)
.GetAwaiter()
.GetResult();
无论如何,我建议您使用await
关键字并进行所有必要的重构以使您的代码工作。
英文:
As you are calling an async method, if you don't want to change the return type to Task<T>, you can use the following approach.
client.SendEmailAsync(msg)
.GetAwaiter()
.GetResult();
Anyway, I'd recommend that you use the await
word and do all the necessary refactorings to make your code work.
答案2
得分: 0
- 首先将其设置为异步。从某个版本开始出现问题。所以开始将是这样的:
public async Task testEmail()
{
...
var result = await client.SendEmailAsync(msg);
}
-
如果它不起作用,请检查您发送邮件的目标。例如,发送到 Yahoo 等可能不起作用。
-
您是否在某个公司参与这个项目?严格的网络安全规则可能会阻止它...然后您就会被列入“禁止列表”直到您请求排除。
英文:
Are 3 possibilities -
1)
At first make it async. From some version it was problem. So begin will be as
public async Task testEmail()
{ ...
var result = await client.SendEmailAsync(msg);
}
- If it will not work check where you are sending mail. E.g. to Yahoo etc. it could not work.
- Are you with this project at some corporate? Tough cyber security rules can block it ... aaand you are done until you ask for exclude from the "forbidden list".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论