Cannot smtp in C# - cant connect to server

G

Guest

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
 
W

Willy Denoyette [MVP]

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.


|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
 
J

Jon Skeet [C# MVP]

Jim said:
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
 
J

james

Chris said:
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...
 
S

sloan

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/?_...ogview&_c=blogpart&partqs=amonth=2&ayear=2006

2/8/2006 datestamp




Chris said:
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");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top