Authenticated mail sending not working in .NET

524 views Asked by At

I have a script sending mail from my .net application.

My hosting provider requires the emails to be authenticated if I don't want to cause delays in receiving them (currently delay is almost a day from sending).

I have build mail sending script with authentication but it doesn't work - it doesn't produce any error or exception but the emails are still not received instantly.

In the other hand similar script made in classic asp works and I receive mails instantly.

Am I missing something?

This is asp script (which works):

<%
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "[email protected]"
objEmail.To = "[email protected]"
objEmail.Subject = "Test Mail"
objEmail.Textbody = "Test Mail"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.mydomain.com"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = "1"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "myusername"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
objEmail.Configuration.Fields.Update
objEmail.Send
If Not err.number=0 Then
Response.write "ERROR: " & err.Description
err.Clear
end if
%>

This is .NEt (which doesn't work):

public static void SendEmail(string senderName, string senderEmail, string recipient, string comments, string subject, bool inTemplate)
{
    MailAddress from = new MailAddress(senderEmail);
    MailAddress to = new MailAddress(recipient);
    MailMessage mail = new MailMessage(from, to);
    mail.ReplyToList.Add(senderEmail);
    mail.Subject = subject;
    mail.IsBodyHtml = true;
    mail.Body = comments;

    Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
    MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
    NetworkCredential basicCredentials = new NetworkCredential(mailSettings.Smtp.Network.UserName, mailSettings.Smtp.Network.Password);

    SmtpClient smtpClient = new SmtpClient();
    smtpClient.Host = mailSettings.Smtp.Network.Host;
    smtpClient.Port = mailSettings.Smtp.Network.Port;
    smtpClient.UseDefaultCredentials = false;
    smtpClient.Credentials = basicCredentials;

    try
    {
        smtpClient.Send(mail);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

These are my web.config settings:

  <system.net>
    <mailSettings>
      <smtp>
        <network host="mail.mydomain.com" userName="myusername" password="mypassword" port="25" />
      </smtp>
    </mailSettings>
  </system.net>

The web.config settings are correct but even if I input server settings manually into the class - it doesn't work.

Thanks

2

There are 2 answers

0
nickornotto On BEST ANSWER

It turned out that the problem was at the hosting provider side. Not sure what they did but it eventually started to work.

1
Jatin Gadhiya On

Try this:

 MailMessage mailMsg = new MailMessage();
    mailMsg.To.Add("[email protected]");
                // From
    MailAddress mailAddress = new MailAddress("[email protected]");
    mailMsg.From = mailAddress;

    // Subject and Body
    mailMsg.Subject = "subject";
    mailMsg.Body = "body";

    // Init SmtpClient and send on port 587 in my case. (Usual=port25)
    SmtpClient smtpClient = new SmtpClient("mailserver", 587);
    System.Net.NetworkCredential credentials = 
       new System.Net.NetworkCredential("username", "password");
    smtpClient.Credentials = credentials;

    smtpClient.Send(mailMsg);