Search code examples
asp.netauthenticationsendmailmailsettings

Authenticated mail sending not working in .NET


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


Solution

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