2
votes

I have some code that I am attempting to use to send emails from my ASP.NET MVC 5 website. From what I have read I am doing this the right way and the code is correct

public static Task SendEmailAsync(IEnumerable<string> to, 
    IEnumerable<string> cc, string body)
{
    if (to == null || to.Count() == 0)
        return null;
    if (String.IsNullOrEmpty(body))
        throw new ArgumentNullException("body of the email is empty");

    // Send email.
    return Task.Factory.StartNew(() =>
        {
            // Client setup.
            using (SmtpClient smtpClient = new SmtpClient("smtp.servername.com", 25)) // Tried 587 too.
            {
                smtpClient.Credentials = new System.Net.NetworkCredential()
                {
                    UserName = Constants.AdminEmailAddress,
                    Password = Constants.AdminPwd
                };
                smtpClient.UseDefaultCredentials = true;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.EnableSsl = true;

                // Mail message.
                using (MailMessage mail = new MailMessage())
                {
                    mail.Body = body;
                    mail.From = new MailAddress(Constants.AdminEmailAddress, "Business Name Here");
                    foreach (var a in to)
                        mail.To.Add(new MailAddress(a));
                    if (cc != null)
                        foreach (var a in cc)
                            mail.CC.Add(new MailAddress(a));
                    smtpClient.Send(mail); // Here I get exception.
                }
            }
        });
}

But on the line marked above I get

System.Net.Mail.SmtpException: Unable to connect to the remote server A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond NNN.NNN.NNN.NNN:MMM

I understand that this is clearly saying that the client cannot connect, but these are the exact same details that I am using to send email from Microsoft Outlook and that works.

Why can't I connect to the mail server using these credentials, what am I missing?

I am aware that I may have to contact the mail service provider and ask for permissions, but this is my first attempt at this and would like some clarification about what to do. I have looked at mandrillapp.com which provides a mail service, is this a good option, can someone advise?

Thank for your time.


Note, I have read

  1. How to send email from Asp.net Mvc-3?
  2. http://www.c-sharpcorner.com/UploadFile/sourabh_mishra1/sending-an-e-mail-using-Asp-Net-mvc/
  3. send email from MVC 4 ASP.NET app

Update

Following the very helpful suggestions by @Dave Zych below I have removed the SmtpException but I now have System.Security.Authentication.AuthenticationException saying "The remote certificate is invalid according to the validation procedure.". So I am in the same boat and still cannot send email.

1
regarding the remote certificate, has it been added to your computer as as a trusted cert?user1666620
No, I am totally new to this, so I am unaware of the requirement and how to do this. Where would I get the certificate? I am sure once I have the certificate I could establish how to add it... Thanks for your comment.MoonKnight

1 Answers

3
votes

Don't specify UseDefaultCredentials (or set it to false). That causes it to use the credentials of the currently logged in user, not what you set in the Credentials property.

From MSDN:

Some SMTP servers require that the client be authenticated before the server sends e-mail on its behalf. Set this property to true when this SmtpClient object should, if requested by the server, authenticate using the default credentials of the currently logged on user. For client applications, this is the desired behavior in most scenarios.

...

If the UseDefaultCredentials property is set to false, then the value set in the Credentials property will be used for the credentials when connecting to the server. If the UseDefaultCredentials property is set to false and the Credentials property has not been set, then mail is sent to the server anonymously.