C# - Email problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eugene Vital

    #1

    C# - Email problems

    Hi all
    I am having trouble with sending email via a C#2.0 application, I use
    the same settings as I use in Outlook but I cannot get email to send.

    I am trying to use SSL on port 465 and get the error below, If I disable
    SSL and use port 587 all goes well.


    I have been Googling for 2 days without success and this seems to be a
    common problem.


    Any help is appreciated.

    Thanks.


    Here is the code I am using.


    MailMessage message = new MailMessage();
    message.From = new MailAddress("my address@mydomai n.com");
    message.Sender = new MailAddress("my address@mydomai n.com");
    message.To.Add( new MailAddress("my address@mydomai n.com"));
    message.Subject = "This is my subject";
    message.Body = "This is the content";

    SmtpClient smtpClient = new SmtpClient("smt p.bizmail.yahoo .com", 465);
    System.Net.Netw orkCredential mc = new
    System.Net.Netw orkCredential(" myusername@mydo main.com", "mypassword ");

    smtpClient.UseD efaultCredentia ls = false;
    smtpClient.Cred entials = mc ;
    smtpClient.Deli veryMethod = SmtpDeliveryMet hod.Network ;
    smtpClient.Enab leSsl = true;

    try{
    smtpClient.Send (message);

    }
    catch(Exception e) {
    System.Console. WriteLine(e.Inn erException.ToS tring());
    }

    smtpClient = null;

    Console.Write(" Press any key to continue . . . ");
    Console.ReadKey (true);



    The error I get is this:

    Unable to read data from the transport connection: An existing
    connection was forcibly closed by the remote host.

    at System.Net.Sock ets.NetworkStre am.Read(Byte[] buffer, Int32
    offset, Int32 size)
    at System.Net.Dele gatedStream.Rea d(Byte[] buffer, Int32 offset,
    Int32 count)
    at System.Net.Buff eredReadStream. Read(Byte[] buffer, Int32 offset,
    Int32 count)
    at System.Net.Mail .SmtpReplyReade rFactory.ReadLi nes(SmtpReplyRe ader
    caller, Boolean oneLine)
    at System.Net.Mail .SmtpReplyReade rFactory.ReadLi ne(SmtpReplyRea der
    caller)
    at System.Net.Mail .SmtpReplyReade r.ReadLine()
    at System.Net.Mail .SmtpConnection .GetConnection( String host, Int32 port)
    at System.Net.Mail .SmtpTransport. GetConnection(S tring host, Int32 port)
    at System.Net.Mail .SmtpClient.Get Connection()
    at System.Net.Mail .SmtpClient.Sen d(MailMessage message)
  • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

    #2
    RE: C# - Email problems

    Here is a sample method that I use with my gmail account. The port is
    different, but it should work for you with the correct port:

    using System;
    using System.Collecti ons.Generic;
    using System.Text;

    namespace PAB.Utils
    {
    public static class Sender
    {
    public static void SendGmail(strin g userName, string password,
    string mailFrom,
    string mailTo,string subject, string
    message,bool isBodyHtml)
    {
    System.Net.Mail .MailMessage msg = new
    System.Net.Mail .MailMessage(ma ilFrom,mailTo,
    subject, message);
    msg.IsBodyHtml = isBodyHtml;
    System.Net.Netw orkCredential cred = new
    System.Net.Netw orkCredential(u serName, password);
    System.Net.Mail .SmtpClient mailClient = new
    System.Net.Mail .SmtpClient("sm tp.gmail.com",5 87);
    mailClient.Enab leSsl = true;
    mailClient.UseD efaultCredentia ls = false;
    mailClient.Cred entials = cred;
    mailClient.Send (msg);
    }
    }
    }
    --
    --Peter
    "Inside every large program, there is a small program trying to get out."






    "Eugene Vital" wrote:
    Hi all
    I am having trouble with sending email via a C#2.0 application, I use
    the same settings as I use in Outlook but I cannot get email to send.
    >
    I am trying to use SSL on port 465 and get the error below, If I disable
    SSL and use port 587 all goes well.
    >
    >
    I have been Googling for 2 days without success and this seems to be a
    common problem.
    >
    >
    Any help is appreciated.
    >
    Thanks.
    >
    >
    Here is the code I am using.
    >
    >
    MailMessage message = new MailMessage();
    message.From = new MailAddress("my address@mydomai n.com");
    message.Sender = new MailAddress("my address@mydomai n.com");
    message.To.Add( new MailAddress("my address@mydomai n.com"));
    message.Subject = "This is my subject";
    message.Body = "This is the content";
    >
    SmtpClient smtpClient = new SmtpClient("smt p.bizmail.yahoo .com", 465);
    System.Net.Netw orkCredential mc = new
    System.Net.Netw orkCredential(" myusername@mydo main.com", "mypassword ");
    >
    smtpClient.UseD efaultCredentia ls = false;
    smtpClient.Cred entials = mc ;
    smtpClient.Deli veryMethod = SmtpDeliveryMet hod.Network ;
    smtpClient.Enab leSsl = true;
    >
    try{
    smtpClient.Send (message);
    >
    }
    catch(Exception e) {
    System.Console. WriteLine(e.Inn erException.ToS tring());
    }
    >
    smtpClient = null;
    >
    Console.Write(" Press any key to continue . . . ");
    Console.ReadKey (true);
    >
    >
    >
    The error I get is this:
    >
    Unable to read data from the transport connection: An existing
    connection was forcibly closed by the remote host.
    >
    at System.Net.Sock ets.NetworkStre am.Read(Byte[] buffer, Int32
    offset, Int32 size)
    at System.Net.Dele gatedStream.Rea d(Byte[] buffer, Int32 offset,
    Int32 count)
    at System.Net.Buff eredReadStream. Read(Byte[] buffer, Int32 offset,
    Int32 count)
    at System.Net.Mail .SmtpReplyReade rFactory.ReadLi nes(SmtpReplyRe ader
    caller, Boolean oneLine)
    at System.Net.Mail .SmtpReplyReade rFactory.ReadLi ne(SmtpReplyRea der
    caller)
    at System.Net.Mail .SmtpReplyReade r.ReadLine()
    at System.Net.Mail .SmtpConnection .GetConnection( String host, Int32 port)
    at System.Net.Mail .SmtpTransport. GetConnection(S tring host, Int32 port)
    at System.Net.Mail .SmtpClient.Get Connection()
    at System.Net.Mail .SmtpClient.Sen d(MailMessage message)
    >

    Comment

    • Eugene Vital

      #3
      Re: C# - Email problems

      Thanks,

      That is pretty much the code I am using and it doesn't work.

      It appears as though it is timing out then disconnecting, I have enabled
      logging but it doesn't give me anything useful.

      Peter Bromberg [C# MVP] wrote:
      Here is a sample method that I use with my gmail account. The port is
      different, but it should work for you with the correct port:
      >
      using System;
      using System.Collecti ons.Generic;
      using System.Text;
      >
      namespace PAB.Utils
      {
      public static class Sender
      {
      public static void SendGmail(strin g userName, string password,
      string mailFrom,
      string mailTo,string subject, string
      message,bool isBodyHtml)
      {
      System.Net.Mail .MailMessage msg = new
      System.Net.Mail .MailMessage(ma ilFrom,mailTo,
      subject, message);
      msg.IsBodyHtml = isBodyHtml;
      System.Net.Netw orkCredential cred = new
      System.Net.Netw orkCredential(u serName, password);
      System.Net.Mail .SmtpClient mailClient = new
      System.Net.Mail .SmtpClient("sm tp.gmail.com",5 87);
      mailClient.Enab leSsl = true;
      mailClient.UseD efaultCredentia ls = false;
      mailClient.Cred entials = cred;
      mailClient.Send (msg);
      }
      }
      }

      Comment

      Working...