469,356 Members | 2,010 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

Cannot smtp in C# - cant connect to server

I am trying to send email in C#. I wrote 2 pieces of code:
1.
MailMessage mail = new MailMessage();
mail.From = "from_address";
mail.To = "to_address";
mail.Subject = "subject";
mail.BodyFormat = MailFormat.Text;
mail.Body = "body";
SmtpMail.SmtpServer = "smtp.server";
mail.Fields.Clear();
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "userl");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "25");
SmtpMail.Send(mail);

/////////////////////////////////////////////////////////////////////////////////////////////
2.

MailMessage mail = new MailMessage("from_address", "to_addressl");
mail.Subject = "subject";
mail.Body = "body";
SmtpClient smtp = new SmtpClient("smtp.server", 25);
smtp.EnableSsl = false;
smtp.Credentials = new System.Net.NetworkCredential("user", "password");
smtp.Send(mail);
I am thrown error:

System.Net.Mail.SmtpException: Failure sending mail. --->
System.Net.WebException: Unable to connect to the remote server --->
System.Net.Sockets.SocketException: A connection attempt failed because the
connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to respond
what is wrong.?

PS
I wrote another application (in borland c++) where I specified the same data
(from/to addresse, server address, user/password) and it works ok
Sep 27 '06 #1
5 23392
Try adding the '@' character before your quoted strings:

i.e.

mail.Fileds.Add(@"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1");

The compiler may be interpreting everything after // as a comment.

"Chris" <Ch***@discussions.microsoft.comwrote in message
news:B1**********************************@microsof t.com...
>I am trying to send email in C#. I wrote 2 pieces of code:
1.
MailMessage mail = new MailMessage();
mail.From = "from_address";
mail.To = "to_address";
mail.Subject = "subject";
mail.BodyFormat = MailFormat.Text;
mail.Body = "body";
SmtpMail.SmtpServer = "smtp.server";
mail.Fields.Clear();
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",
"1");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",
"userl");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",
"password");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
"25");
SmtpMail.Send(mail);

/////////////////////////////////////////////////////////////////////////////////////////////
2.

MailMessage mail = new MailMessage("from_address", "to_addressl");
mail.Subject = "subject";
mail.Body = "body";
SmtpClient smtp = new SmtpClient("smtp.server", 25);
smtp.EnableSsl = false;
smtp.Credentials = new System.Net.NetworkCredential("user", "password");
smtp.Send(mail);
I am thrown error:

System.Net.Mail.SmtpException: Failure sending mail. --->
System.Net.WebException: Unable to connect to the remote server --->
System.Net.Sockets.SocketException: A connection attempt failed because
the
connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to respond
what is wrong.?

PS
I wrote another application (in borland c++) where I specified the same
data
(from/to addresse, server address, user/password) and it works ok

Sep 27 '06 #2
You need to make sure your smtp server is reachable, that is try to connect
to it using something simple like "ping" or "telnet smtp.server 25".
Firewall may block this port as well, so watch out for them.

Willy.
"Chris" <Ch***@discussions.microsoft.comwrote in message
news:B1**********************************@microsof t.com...
|I am trying to send email in C#. I wrote 2 pieces of code:
| 1.
| MailMessage mail = new MailMessage();
| mail.From = "from_address";
| mail.To = "to_address";
| mail.Subject = "subject";
| mail.BodyFormat = MailFormat.Text;
| mail.Body = "body";
| SmtpMail.SmtpServer = "smtp.server";
| mail.Fields.Clear();
|
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",
"1");
|
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",
"userl");
|
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",
"password");
|
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
"25");
| SmtpMail.Send(mail);
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
| 2.
|
| MailMessage mail = new MailMessage("from_address", "to_addressl");
| mail.Subject = "subject";
| mail.Body = "body";
| SmtpClient smtp = new SmtpClient("smtp.server", 25);
| smtp.EnableSsl = false;
| smtp.Credentials = new System.Net.NetworkCredential("user", "password");
| smtp.Send(mail);
|
|
| I am thrown error:
|
| System.Net.Mail.SmtpException: Failure sending mail. --->
| System.Net.WebException: Unable to connect to the remote server --->
| System.Net.Sockets.SocketException: A connection attempt failed because
the
| connected party did not properly respond after a period of time, or
| established connection failed because connected host has failed to respond
|
|
| what is wrong.?
|
| PS
| I wrote another application (in borland c++) where I specified the same
data
| (from/to addresse, server address, user/password) and it works ok
Sep 27 '06 #3
Jim Boddie wrote:
Try adding the '@' character before your quoted strings:

i.e.
mail.Fileds.Add(@"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1");

The compiler may be interpreting everything after // as a comment.
No - otherwise the line wouldn't compile in the first place.

Forward slashes are fine in string literals. Verbatim string literals
(i.e. those with the @ in front) deal with escaping which is normally
done with backslashes.

Jon

Sep 27 '06 #4

"Chris" <Ch***@discussions.microsoft.comwrote in message
news:B1**********************************@microsof t.com...
>I am trying to send email in C#. I wrote 2 pieces of code:
1.
MailMessage mail = new MailMessage();
mail.From = "from_address";
mail.To = "to_address";
mail.Subject = "subject";
mail.BodyFormat = MailFormat.Text;
mail.Body = "body";
SmtpMail.SmtpServer = "smtp.server";
mail.Fields.Clear();
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",
"1");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",
"userl");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",
"password");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
"25");
SmtpMail.Send(mail);

/////////////////////////////////////////////////////////////////////////////////////////////
2.

MailMessage mail = new MailMessage("from_address", "to_addressl");
mail.Subject = "subject";
mail.Body = "body";
SmtpClient smtp = new SmtpClient("smtp.server", 25);
smtp.EnableSsl = false;
smtp.Credentials = new System.Net.NetworkCredential("user", "password");
smtp.Send(mail);
I am thrown error:

System.Net.Mail.SmtpException: Failure sending mail. --->
System.Net.WebException: Unable to connect to the remote server --->
System.Net.Sockets.SocketException: A connection attempt failed because
the
connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to respond
what is wrong.?

PS
I wrote another application (in borland c++) where I specified the same
data
(from/to addresse, server address, user/password) and it works ok
I'd double check you can telnet into it on port 25 as someone else said -
I've used code similar to your version 2 and it works ok...
Sep 27 '06 #5
Chris,

Those schema.microsoft.com things are tricky.

You also need to be aware of the different authentication schemes.

None
Basic
SSL

I have complete downloadable examples (1.1 and 2.0) at:

http://sholliday.spaces.live.com/?_c...26ayear%3d2006

2/8/2006 datestamp


"Chris" <Ch***@discussions.microsoft.comwrote in message
news:B1**********************************@microsof t.com...
I am trying to send email in C#. I wrote 2 pieces of code:
1.
MailMessage mail = new MailMessage();
mail.From = "from_address";
mail.To = "to_address";
mail.Subject = "subject";
mail.BodyFormat = MailFormat.Text;
mail.Body = "body";
SmtpMail.SmtpServer = "smtp.server";
mail.Fields.Clear();
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenti
cate", "1");
>
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername
", "userl");
>
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword
", "password");
>
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverpo
rt", "25");
SmtpMail.Send(mail);

////////////////////////////////////////////////////////////////////////////
/////////////////
2.

MailMessage mail = new MailMessage("from_address", "to_addressl");
mail.Subject = "subject";
mail.Body = "body";
SmtpClient smtp = new SmtpClient("smtp.server", 25);
smtp.EnableSsl = false;
smtp.Credentials = new System.Net.NetworkCredential("user", "password");
smtp.Send(mail);
I am thrown error:

System.Net.Mail.SmtpException: Failure sending mail. --->
System.Net.WebException: Unable to connect to the remote server --->
System.Net.Sockets.SocketException: A connection attempt failed because
the
connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to respond
what is wrong.?

PS
I wrote another application (in borland c++) where I specified the same
data
(from/to addresse, server address, user/password) and it works ok

Sep 27 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by aa | last post: by
reply views Thread by Amol Shambharkar | last post: by
6 posts views Thread by tony010409020622 | last post: by
4 posts views Thread by Chris L | last post: by
6 posts views Thread by Jean-Marc Blaise | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.