By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,558 Members | 1,177 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,558 IT Pros & Developers. It's quick & easy.

Cannot smtp in C# - cant connect to server

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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.