简体   繁体   中英

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 = "sender@mydomain.com"
objEmail.To = "recipient@gmail.com"
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

Try this:

 MailMessage mailMsg = new MailMessage();
    mailMsg.To.Add("test@hotmail.com");
                // From
    MailAddress mailAddress = new MailAddress("you@hotmail.com");
    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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM