0

I have code which sends email to the email id that has been fed by the user on registration. It can be either our company domain id or any other domain for that matter.

Now there are 3 different buttons which call three different email pages. Leave can be approved, denied or cancelled. The code is generic, only the boy changes and the result is reflected on the database by changing values of status field.

Here is the code I am using:

    try
        {
            string fromEmail = ConfigurationManager.AppSettings["EmailID"].ToString();
            string toEmail = lbl_emp_email.Text.Trim();                
            string bcc = Convert.ToString(lbl_logger_mailID.Text);

            MailMessage message = new MailMessage(fromEmail, toEmail);
            message.CC.Add("abcd@aaaa-india.org");
            message.Bcc.Add(new MailAddress(bcc));
            //send email to sender,the boss in this case.notify him he has approved a leave request

            message.Subject = lbl_subject.Text.Trim();
            message.Body = txt_mail_body.Text.Trim();
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.aaaa-india.org";
            smtp.Send(message);
            Response.Write("<script type=\"text/javascript\">alert('Notification of leave approval sent.');</script>");
        }
        catch (Exception exce)
        {
            Response.Write("MAIL NOT SENT. AN ERROR OCCURRED." + exce.ToString());
        }


   <system.net>
<mailSettings>
  <smtp deliveryMethod="Network" from="support@aaaa-india.org" >
    <network defaultCredentials="false"
             enableSsl="true"
             host="smtp.aaaa-india.org"
             port="587"
             userName="support@aaaa-india.org"
             password="abcd"/>
  </smtp>
</mailSettings>
   </system.net>
   <appSettings>
<add key="EmailID" value="support@aaaa-india.org"/></appSettings>

I get 3 different errors depending on the port number i use. As I have used the port 587 above I get the following error:

    MAIL NOT SENT. AN ERROR OCCURRED.
    System.Security.Authentication.AuthenticationException: 
    The remote certificate is invalid according to the validation procedure. at 
    System.Net.Security.SslState.StartSendAuthResetSignal
    (ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception)at  
    System.Net.Security.SslState.CheckCompletionBeforeNextReceive
    (ProtocolToken message, AsyncProtocolRequest asyncRequest) at 
     System.Net.Security.SslState.StartSendBlob
    (Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest) at 
    System.Net.Security.SslState.ProcessReceivedBlob
   (Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest) at 
    System.Net.Security.SslState.StartReadFrame
   (Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) at 
    System.Net.Security.SslState.StartReceiveBlob
    (Byte[] buffer, AsyncProtocolRequest asyncRequest)

When i change the port number to 465 the error is:

   AN ERROR OCCURRED. System.Net.Mail.SmtpException: The operation has timed out. at 
   System.Net.Mail.SmtpClient.Send(MailMessage message) 
   at the line smtp.Send(message);

When i use the port 25 the error is again:

    MAIL NOT SENT. AN ERROR OCCURRED.
    System.Security.Authentication.AuthenticationException: 
    The remote certificate is invalid according to the validation procedure. at 
    System.Net.Security.SslState.StartSendAuthResetSignal
    (ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception) at 
    System.Net.Security.SslState.CheckCompletionBeforeNextReceive
   (ProtocolToken message, AsyncProtocolRequest asyncRequest) at 
    System.Net.Security.SslState.StartSendBlob
   (Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)

I have tried to open a telnet connection to the mail server on the specified port. And it works. I even checked HTTP services option in Allow a program through the firewall.

I also checked into the firewall if it is blocking anything, but its not.I manually enabled the firewall to allow all the email related ports. I checked with my administrator for any blocking privileges, but there are no restrictions that wont let me send email. As a last resort i installed my company's mail client on my computer and tried using smtp on various ports.

Everything works. Except in this website. Please help.

avneesh
  • 87
  • 2
  • 10

1 Answers1

0

Check the answer to the question here - seems similar: Getting "The remote certificate is invalid according to the validation procedure" when SMPT server has a valid certificate

The answer I have finally found is that the SMTP service on the server is not using the same certificate as https.

The diagnostic steps I had read here make the assumption they use the same certificate and every time I've tried this in the past they have done and the diagnostic steps are exactly what I've done to solve the problem several times.

In this case those steps didn't work because the certificates in use were different, and the possibility of this is something I had never come across.

The solution is either to export the actual certificate from the server and then install it as a trusted certificate on my machine, or to get a different valid/trusted certificate for the SMTP service on the server. That is currently with our IT department who administer the servers to decide which they want to do.

Community
  • 1
  • 1
user1666620
  • 4,800
  • 18
  • 27
  • I got it checked and the certificates have no difference. We checked each detail of the certificate and it is the same. What now? – avneesh Dec 01 '14 at 11:46
  • @avneesh you added the cert onto the server as a trusted cert? – user1666620 Dec 01 '14 at 11:50
  • @avneesh you can do something completely stupid, just as a sanity check, and turn off certificate validation. That way, if it works, you know the problem is with the cert. http://stackoverflow.com/questions/777607/the-remote-certificate-is-invalid-according-to-the-validation-procedure-using – user1666620 Dec 01 '14 at 11:51
  • solved it! i removed the certificate and then copied it again from the server. what i noticed is i was using the wrong name. My server supports mx. But a new problem has arisen.I am unable to deploy the site on intranet. It shows the error-"can not reach remote server" which is quite impossible as the site just sent mail through the server! Beats me what might be wrong. Any suggestions on this? – avneesh Dec 02 '14 at 06:16
  • @avneesh might be best to ask in a new question instead of here so you get more views. also would need more info regarding the setup of the server. – user1666620 Dec 02 '14 at 09:46