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:

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
public void sendMail()
      SmtpClient client = new SmtpClient();
      MailMessage message = new MailMessage();
      client.Port = 465;
      //client.Host = "";
      client.Host = "";//"";
      client.UseDefaultCredentials = true;
      client.Credentials = new System.Net.NetworkCredential("", "MyPassword");
      client.EnableSsl = true;
          MailAddress SendTo = new MailAddress("");
         // MailAddress SendTo = new MailAddress("");
          MailAddress SendFrom = new MailAddress("");
          message = new MailMessage(SendFrom, SendTo);
          message.Body = "Test Mail";
          message.Subject = "Test Mail";
      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 but not with yahoo pl help me its very urgent

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.[^]

It works with System.Web.Mail (which is marked as obsolete):
MailMessage oMsg = new MailMessage();
                oMsg.From = new MailAddress("", "xxx");
                oMsg.To.Add(new MailAddress("", "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("", 587);
                oSmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                oSmtp.EnableSsl = true;
                NetworkCredential oCredential = new NetworkCredential("", "123456aA");
                oSmtp.UseDefaultCredentials = false;
                oSmtp.Credentials = oCredential;

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