Can't send email through SendGrid

后端 未结 6 616
一个人的身影
一个人的身影 2021-01-15 15:58

I\'m following the example from SendGrid\'s site and as credentials I\'m pasting it what they gave me in the Azure portal. Still, I get this error message.

相关标签:
6条回答
  • 2021-01-15 16:30

    Ok, I think I have the answer. Above code is fine. On SendGrid you must approve the sending email address. Log into your SendGrid portal and attend to the below:

    1. Setting -> Sender Authentication
    2. Under "Single Sender Verification" add the email address that you want
    3. Click authorize link in inbox and your bob's nephew.

    Hope the above helps

    0 讨论(0)
  • 2021-01-15 16:34

    I was able to get a SendGrid email send to happen successfully from a Console application using the SendGrid v3 C# API.

    I first needed to import the SendGrid C# library via NuGet using the Package Manager Console in Visual Studio:

    PM> Install-Package SendGrid
    

    Then, my code:

    using SendGrid;
    using SendGrid.Helpers.Mail;
    using System.Threading.Tasks;
    
    // ...
    
    public static async Task SendEmailViaSendGrid(string to, string from, string subject, string body)
    {
        // Generated from https://app.sendgrid.com/settings/api_keys
        // TODO: Store this somewhere secure -- not in version control.
        string apiKey = "YOUR_PRIVATE_SENDGRID_API_KEY_GOES_HERE";
    
        dynamic sendGridClient = new SendGridAPIClient(apiKey);
    
        Email fromEmail = new Email(from);
        Email toEmail = new Email(to);
        Content content = new Content("text/plain", body);
        Mail mail = new Mail(fromEmail, subject, toEmail, content);
    
        dynamic response = await sendGridClient.client.mail.send.post(requestBody: mail.Get());
    }
    
    0 讨论(0)
  • 2021-01-15 16:38

    I know this is an old post, but I found out why emails are not sent from a console application. When I add Console.Read(); at the end of the program.cs class, it sends the emails, but if I remove the Console.Read(); from the code, the emails do not send. So my assumption is, if I let the program wait a bit before it closes, the emails should send.

    0 讨论(0)
  • 2021-01-15 16:41

    Not sure if this is the case for everyone, but I had to create a Sender. https://app.sendgrid.com/settings/sender_auth/senders. That would match mailMsg.From

    0 讨论(0)
  • 2021-01-15 16:46

    Does this happen to be a console application? I've been testing SendGrid and found that when I try to send an email from a Console application the emails are never sent. However, when I tried it from a web application (using the same exact sending code) the emails are sent. I have no explanation for why the console application does not work.

    0 讨论(0)
  • 2021-01-15 16:52
    public static bool SendMailBySendGrid(string offMail, string mailFormatDetails,string subject,string ccMail=null,string bccMail=null)
    {
      string mailFromId = System.Configuration.ConfigurationManager.AppSettings["Sender"];
      SendGridMessage sndmsg = new SendGridMessage();
      sndmsg.From = new MailAddress(mailFromId);
      sndmsg.To = new MailAddress[] { new MailAddress(offMail) };
      if (!string.IsNullOrEmpty(ccMail))
        sndmsg.Cc = new MailAddress[] { new MailAddress(ccMail) };
      if(!string.IsNullOrEmpty(bccMail))
        sndmsg.Bcc = new MailAddress[] { new MailAddress(bccMail) };
      sndmsg.Subject = subject;         
      sndmsg.Html = "<div>" + mailFormatDetails + "</div>";
      bool result= SendEmail(sndmsg);
      return result;
    }
    

    Include using SendGrid; in your project.

    0 讨论(0)
提交回复
热议问题