0

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

nickornotto
  • 1,946
  • 4
  • 36
  • 68
  • try handling SmtpClient.SendCompleted event and look at Error in EventArgs – Robert Aug 15 '14 at 07:53
  • you need to set port e.g 587 in smtpClient see given below answer. set manually port – Jatin Gadhiya Aug 15 '14 at 07:56
  • doesn't work how ? It throws exception ? – Antonio Bakula Aug 15 '14 at 07:57
  • I can't see anything wrong with your .net code. Are you sure the values for host, port and credentials are retrieved from your webconfig correctly? – U r s u s Aug 15 '14 at 08:10
  • @Dura, yes the values are correct. Anyway I said event if use manually values instead of web.config it doesn't work. – nickornotto Aug 15 '14 at 09:51
  • @Antonio see paragraphs 2-4 of my post. – nickornotto Aug 15 '14 at 09:51
  • For me it looks like email are not read by the server as authenticated for some reason – nickornotto Aug 15 '14 at 09:53
  • @Robert I tried SendCompleted handler but that requires SendAsync which fails for me. I get `Failure sending mail. at System.Net.Mail.SmtpClient.SendAsync(MailMessage message, Object userToken)` – nickornotto Aug 15 '14 at 10:14
  • can you post the entire exception string? – Robert Aug 15 '14 at 10:15
  • @Robert `Failure sending mail. at System.Net.Mail.SmtpClient.SendAsync(MailMessage message, Object userToken) at myproject.Utilities.SendEmail(String senderName, String senderEmail, String recipient, String comments, String subject, Boolean inTemplate) in d:\inetpub\vhosts\mywebsite.com\httpdocs\test\App_Code\myproject.Utilities.cs:line 71 at usercontrols_SendEnquiry.btnSendEnquiry_Click(Object sender, EventArgs e) in d:\inetpub\vhosts\mywebsite.com\httpdocs\test\usercontrols\SendEnquiry.ascx.cs:line 159 ` – nickornotto Aug 15 '14 at 10:25
  • Thanks, but this is just a call stack, I think there should be an inner exception saying what went wrong – Robert Aug 15 '14 at 10:26
  • @Robet That's all I get. I have this at the end of my SendMail `try { smtpClient.SendAsync(mail, null); // Set the method that is called back when the send operation ends. smtpClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback); } catch (ObjectDisposedException ex) { throw ex; }` but it fails on line 71 which is SendAsync code – nickornotto Aug 15 '14 at 10:35
  • the error must be somewhere, either on client or on server, if you have access, try log errors on the server – Robert Aug 15 '14 at 10:52

2 Answers2

0

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);
Jatin Gadhiya
  • 1,955
  • 5
  • 23
  • 42
0

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

nickornotto
  • 1,946
  • 4
  • 36
  • 68