Click here to Skip to main content
15,311,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi

I use the code below to send mail using yahoo smtp and ended up with error failed sending mail pl check the code and tell me where I went wrong
C#
public void sendMail()
  {
      SmtpClient client = new SmtpClient();
      MailMessage message = new MailMessage();
      client.Port = 465;
      //client.Host = "smtp.gmail.com";
      client.Host = "plus.smtp.mail.yahoo.com";//"smtp.bizmail.yahoo.com";
      client.UseDefaultCredentials = true;
      client.Credentials = new System.Net.NetworkCredential("abinav@yahoo.com", "MyPassword");
      client.EnableSsl = true;
      try
      {
          MailAddress SendTo = new MailAddress("mahesh@yahoo.com");
         // MailAddress SendTo = new MailAddress("abinav@yahoo.com");
          MailAddress SendFrom = new MailAddress("abinav@yahoo.com");
          message = new MailMessage(SendFrom, SendTo);
          message.Body = "Test Mail";
          message.Subject = "Test Mail";
          client.Send(message);
      }
      catch (Exception ex)
      {
      }
  }


and I get the error Failure Sending mail

and the inner exception states "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host." It works well with smtp.gmail.com but not with yahoo pl help me its very urgent
Posted

The EnableSsl property specifies whether SSL is used to access the specified SMTP mail server.

The default value for this property can also be set in a machine or application configuration file. Any changes made to the EnableSsl property override the configuration file settings.

The SmtpClient class only supports the SMTP Service Extension for Secure SMTP over Transport Layer Security as defined in RFC 3207. In this mode, the SMTP session begins on an unencrypted channel, then a STARTTLS command is issued by the client to the server to switch to secure communication using SSL. See RFC 3207 published by the Internet Engineering Task Force (IETF) for more information.

An alternate connection method is where an SSL session is established up front before any protocol commands are sent. This connection method is sometimes called SMTP/SSL, SMTP over SSL, or SMTPS and by default uses port 465. This alternate connection method using SSL is not currently supported.

You can use ClientCertificates to specify which client certificates should be used to establish the SSL connection. The ServerCertificateValidationCallback allows you to reject the certificate provided by the SMTP server. The SecurityProtocol property allows you to specify the version of the SSL protocol to use.


http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl.aspx[^]


It works with System.Web.Mail (which is marked as obsolete):
   
v2
MailMessage oMsg = new MailMessage();
                oMsg.From = new MailAddress("xxx@gmail.com", "xxx");
                oMsg.To.Add(new MailAddress("yyy@gmail.com", "xxx"));
                oMsg.Subject = "Packet Parsing Problem";
                oMsg.Body = " Problem Occuread test mail";
                oMsg.SubjectEncoding = System.Text.Encoding.UTF8;
                oMsg.BodyEncoding = System.Text.Encoding.UTF8;
                oMsg.IsBodyHtml = false;
                oMsg.Priority = MailPriority.High;
 
                SmtpClient oSmtp = new SmtpClient("smtp.gmail.com", 587);
                oSmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                oSmtp.EnableSsl = true;
 
                NetworkCredential oCredential = new NetworkCredential("xxx@gmail.com", "123456aA");
                oSmtp.UseDefaultCredentials = false;
                oSmtp.Credentials = oCredential;
                oSmtp.Send(oMsg);


Try this , it's Working for me .
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900