使用Sendgrid发送邮件 – SendEmailAsync

huangapple go评论70阅读模式
英文:

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&#39;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[&quot;SMTPUserName&quot;];
    string sgPassword = ConfigurationManager.AppSettings[&quot;SMTPPassword&quot;];


    /* CREATE THE MAIL MESSAGE
     * ===================================================*/
    var myMessage = new SendGridMessage();
    
    var plainTextContent =&quot;&quot; ;
    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[&quot;FromAddress&quot;], ConfigurationManager.AppSettings[&quot;FromName&quot;]);
    var subject = email.EmailSubject;
    var to = new EmailAddress(email.EmailAddress, email.FirstName + &quot; &quot; + 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

  1. 首先将其设置为异步。从某个版本开始出现问题。所以开始将是这样的:
public async Task testEmail()
{
    ...
    var result = await client.SendEmailAsync(msg);
}
  1. 如果它不起作用,请检查您发送邮件的目标。例如,发送到 Yahoo 等可能不起作用。

  2. 您是否在某个公司参与这个项目?严格的网络安全规则可能会阻止它...然后您就会被列入“禁止列表”直到您请求排除。

英文:

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);
}
  1. If it will not work check where you are sending mail. E.g. to Yahoo etc. it could not work.
  2. 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".

huangapple
  • 本文由 发表于 2023年6月8日 14:16:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定