PC Review


Reply
Thread Tools Rate Thread

C# - Email problems

 
 
Eugene Vital
Guest
Posts: n/a
 
      24th Nov 2007
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("(E-Mail Removed)");
message.Sender = new MailAddress("(E-Mail Removed)");
message.To.Add(new MailAddress("(E-Mail Removed)"));
message.Subject = "This is my subject";
message.Body = "This is the content";

SmtpClient smtpClient = new SmtpClient("smtp.bizmail.yahoo.com", 465);
System.Net.NetworkCredential mc = new
System.Net.NetworkCredential("(E-Mail Removed)", "mypassword");

smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = mc ;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network ;
smtpClient.EnableSsl = true;

try{
smtpClient.Send(message);

}
catch(Exception e) {
System.Console.WriteLine(e.InnerException.ToString());
}

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.Sockets.NetworkStream.Read(Byte[] buffer, Int32
offset, Int32 size)
at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset,
Int32 count)
at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset,
Int32 count)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader
caller, Boolean oneLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader
caller)
at System.Net.Mail.SmtpReplyReader.ReadLine()
at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpClient.GetConnection()
at System.Net.Mail.SmtpClient.Send(MailMessage message)
 
Reply With Quote
 
 
 
 
Peter Bromberg [C# MVP]
Guest
Posts: n/a
 
      24th Nov 2007
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.Collections.Generic;
using System.Text;

namespace PAB.Utils
{
public static class Sender
{
public static void SendGmail(string userName, string password,
string mailFrom,
string mailTo,string subject, string
message,bool isBodyHtml)
{
System.Net.Mail.MailMessage msg = new
System.Net.Mail.MailMessage(mailFrom,mailTo,
subject, message);
msg.IsBodyHtml = isBodyHtml;
System.Net.NetworkCredential cred = new
System.Net.NetworkCredential(userName, password);
System.Net.Mail.SmtpClient mailClient = new
System.Net.Mail.SmtpClient("smtp.gmail.com",587);
mailClient.EnableSsl = true;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = cred;
mailClient.Send(msg);
}
}
}
--
--Peter
"Inside every large program, there is a small program trying to get out."
http://www.eggheadcafe.com
http://petesbloggerama.blogspot.com
http://www.blogmetafinder.com



"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("(E-Mail Removed)");
> message.Sender = new MailAddress("(E-Mail Removed)");
> message.To.Add(new MailAddress("(E-Mail Removed)"));
> message.Subject = "This is my subject";
> message.Body = "This is the content";
>
> SmtpClient smtpClient = new SmtpClient("smtp.bizmail.yahoo.com", 465);
> System.Net.NetworkCredential mc = new
> System.Net.NetworkCredential("(E-Mail Removed)", "mypassword");
>
> smtpClient.UseDefaultCredentials = false;
> smtpClient.Credentials = mc ;
> smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network ;
> smtpClient.EnableSsl = true;
>
> try{
> smtpClient.Send(message);
>
> }
> catch(Exception e) {
> System.Console.WriteLine(e.InnerException.ToString());
> }
>
> 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.Sockets.NetworkStream.Read(Byte[] buffer, Int32
> offset, Int32 size)
> at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset,
> Int32 count)
> at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset,
> Int32 count)
> at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader
> caller, Boolean oneLine)
> at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader
> caller)
> at System.Net.Mail.SmtpReplyReader.ReadLine()
> at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
> at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
> at System.Net.Mail.SmtpClient.GetConnection()
> at System.Net.Mail.SmtpClient.Send(MailMessage message)
>

 
Reply With Quote
 
Eugene Vital
Guest
Posts: n/a
 
      26th Nov 2007
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.Collections.Generic;
> using System.Text;
>
> namespace PAB.Utils
> {
> public static class Sender
> {
> public static void SendGmail(string userName, string password,
> string mailFrom,
> string mailTo,string subject, string
> message,bool isBodyHtml)
> {
> System.Net.Mail.MailMessage msg = new
> System.Net.Mail.MailMessage(mailFrom,mailTo,
> subject, message);
> msg.IsBodyHtml = isBodyHtml;
> System.Net.NetworkCredential cred = new
> System.Net.NetworkCredential(userName, password);
> System.Net.Mail.SmtpClient mailClient = new
> System.Net.Mail.SmtpClient("smtp.gmail.com",587);
> mailClient.EnableSsl = true;
> mailClient.UseDefaultCredentials = false;
> mailClient.Credentials = cred;
> mailClient.Send(msg);
> }
> }
> }

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
email problems Brenda Walden Windows Vista Mail 1 17th Oct 2007 07:32 PM
email problems =?Utf-8?B?a2Vs?= Windows XP Help 1 9th Dec 2006 10:16 PM
Problems with posting email/news messages via OE6 and working with Web-based email Alex Vinokur Microsoft Windows 2000 2 18th Jun 2004 01:56 PM
Email Name Problems Bryan Phillips Microsoft Outlook 2 27th Apr 2004 01:17 AM
Email problems ladyblogg@earthlink.net Windows XP General 1 7th Feb 2004 07:06 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:35 AM.