locked
SendGrid does not send emails but no error either RRS feed

  • Question

  • User154448021 posted

    I deployed my MVC app to Azure and created SendGrid account.  I followed a tutorial and have the following code.  The app can submit the info successfully but no confirmation email has been received.  No error message either.  I checked online for solution.  One post said his code didn't send emails and no error either.  However, without any change, his code just worked the next day.  I am not as lucky as he was.  My app still does not send emails.  Your help will be highly appreciated.

    using Microsoft.AspNet.Identity;
    using SendGrid;
    using System.Configuration;
    using System.Net;
    using System.Threading.Tasks;
    
    namespace MyInfoOnline.Controllers
    {
        public class EmailService : IIdentityMessageService
        {
            public Task SendAsync(IdentityMessage message)
            {
                return configSendGridasync(message);
            }
    
            private Task configSendGridasync(IdentityMessage message)
            {
                var myMessage = new SendGridMessage();
                myMessage.AddTo(message.Destination);
                myMessage.From = new System.Net.Mail.MailAddress(
                                    ConfigurationManager.AppSettings["mailFrom"], "Info Online");
                myMessage.Subject = message.Subject;
                myMessage.Text = message.Body;
                myMessage.Html = message.Body;
    
                var credentials = new NetworkCredential(
                           ConfigurationManager.AppSettings["mailAccount"],
                           ConfigurationManager.AppSettings["mailPassword"]
                           );
    
                // Create a Web transport for sending email.
                var transportWeb = new Web(credentials);
    
                // Send the email.
                if (transportWeb != null)
                {
                    return transportWeb.DeliverAsync(myMessage);
                }
                else
                {
                    return Task.FromResult(0);
                }
            }
        }
    }
    private IdentityMessage CreateGridMessage(string strDestination, string strSubject, string strBody)
            {
                IdentityMessage message = new IdentityMessage();
                message.Destination = strDestination;
                message.Subject = strSubject;
                message.Body = strBody;
    
                return message;
            }
    IdentityMessage myMessage = CreateGridMessage("myemail@gmail.com", 
    "New Info Sumbitted", User.Identity + " has submitted a new info."); EmailService emailService = new EmailService(); await emailService.SendAsync(myMessage);



    Saturday, June 4, 2016 1:10 PM

Answers

  • User-646145796 posted

    Hi,

    I would suggest you to add try...catch in your code to see whether it will catch any exception. You could also try this sample code: https://github.com/sendgrid/sendgrid-csharp. The latest update was 24 days ago. I think it will have a  new version. Please try it.

    Best Regards,

    Jambor

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 6, 2016 2:15 AM

All replies

  • User154448021 posted

    Can anybody help?  Any help will be highly appreciated!

    Sunday, June 5, 2016 2:34 PM
  • User-646145796 posted

    Hi,

    I would suggest you to add try...catch in your code to see whether it will catch any exception. You could also try this sample code: https://github.com/sendgrid/sendgrid-csharp. The latest update was 24 days ago. I think it will have a  new version. Please try it.

    Best Regards,

    Jambor

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 6, 2016 2:15 AM
  • User154448021 posted

    Thank you, Jambor!  That sample code you recommended worked.

    Tuesday, June 7, 2016 12:05 AM