curiouswebster
asked on
The transport failed to connect to the server.
I am trying to get my C#.NET program to send an email with code which used to work. I do get warnings that the code is outdated. Here it is.
Can anyone find the problem or simply have working code to take its place? I just need to send an email. The machine running my program will be an XP machine.
Thanks,
newbieweb
MailMessage mailMsg = new MailMessage();
mailMsg.From = fromAddress;
mailMsg.To = toAddress;
mailMsg.Subject = subject;
mailMsg.Body = messageBody;
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "target@123.com");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "123");
SmtpMail.SmtpServer = Configuration.SMTPServer;
SmtpMail.Send(mailMsg);
mailMsg = null;
Can anyone find the problem or simply have working code to take its place? I just need to send an email. The machine running my program will be an XP machine.
Thanks,
newbieweb
MailMessage mailMsg = new MailMessage();
mailMsg.From = fromAddress;
mailMsg.To = toAddress;
mailMsg.Subject = subject;
mailMsg.Body = messageBody;
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "target@123.com");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "123");
SmtpMail.SmtpServer = Configuration.SMTPServer;
SmtpMail.Send(mailMsg);
mailMsg = null;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is some working code, if needed.
Also need,
using System.Net.Mail
Also need,
using System.Net.Mail
//Initialize SMTP and Mail components
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
//From address
MailAddress fromAddress = new MailAddress(replyEmail, emailDisplayName);
message.From = fromAddress;
message.Subject = mailSubject;
message.IsBodyHtml = true;
message.Bcc.Add(emailBcc);
//Message body content
strHtml = "<table width=100% border=0 align=center cellpadding=0 cellspacing=0><tr align=center><td>";
strHtml = strHtml + "<image src=http://" + emailImage + "></td></tr><tr><td> </td></tr></table>";
strHtml = strHtml + mailBody;
strHtml = strHtml + "<table width=100% border=0 align=center cellpadding=0 cellspacing=0><tr><td> </td></tr>";
strHtml = strHtml + "<tr><td></td></tr></table>";
message.Body = strHtml;
//Add attachment if specified
if (fileName != "")
{
message.Attachments.Add(new Attachment(@"c:\inetpub\wwwroot\listserv\attachments\" + fileName));
}
//Send SMTP mail
smtpClient.Send(message);
ASKER
ashutosh_kumar,
Your code still generates a warning that the code is outdated. Did I do something wrong?
rhythmluvr,
I must have missed something. Here is the code as I am trying to use it.
//Initialize SMTP and Mail components
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
//From address
MailAddress fromAddressObj = new MailAddress(fromAddress, "");
message.From = fromAddressObj;
message.Subject = subject;
message.IsBodyHtml = true;
//Message body content
string strHtml = "<table width=100% border=0 align=center cellpadding=0 cellspacing=0><tr align=center><td>";
strHtml = strHtml + messageBody;
strHtml = strHtml + "<table width=100% border=0 align=center cellpadding=0 cellspacing=0><tr><td>&nbs p;</td></t r>";
strHtml = strHtml + "<tr><td></td></tr></table >";
message.Body = strHtml;
//Send SMTP mail
smtpClient.Send(message);
the exception is:
"The SMTP host was not specified."
Do you know what variable I am supposed to set, and how to set it?
thanks,
newbieweb
Your code still generates a warning that the code is outdated. Did I do something wrong?
rhythmluvr,
I must have missed something. Here is the code as I am trying to use it.
//Initialize SMTP and Mail components
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
//From address
MailAddress fromAddressObj = new MailAddress(fromAddress, "");
message.From = fromAddressObj;
message.Subject = subject;
message.IsBodyHtml = true;
//Message body content
string strHtml = "<table width=100% border=0 align=center cellpadding=0 cellspacing=0><tr align=center><td>";
strHtml = strHtml + messageBody;
strHtml = strHtml + "<table width=100% border=0 align=center cellpadding=0 cellspacing=0><tr><td>&nbs
strHtml = strHtml + "<tr><td></td></tr></table
message.Body = strHtml;
//Send SMTP mail
smtpClient.Send(message);
the exception is:
"The SMTP host was not specified."
Do you know what variable I am supposed to set, and how to set it?
thanks,
newbieweb
ASKER
also, I noticed I wrecked the HTML formatting for the table, above, when I took out the line of code for the emailImage, since I am not using an image. Maybe you could format if for me as well, since tables are not my forte.
to send a mail your need to know the mail server name or IP, like if your sending it from gmail, you should use smtp.gmail.com, if yahoo mail, then smtp.yahoo.com etc.
that you must specify in SmtpClient either in the constructor or later like this
1)
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp_server_name";
or
2)
SmtpClient smtpClient = new SmtpClient("smtp_server_na me");
No, my code doesn't give any warning....
Also, its not required to send the message in HTML, you can send plain text also
so, instead of
message.IsBodyHtml = true;
//Message body content
string strHtml = "<table width=100% border=0 align=center cellpadding=0 cellspacing=0><tr align=center><td>";
strHtml = strHtml + messageBody;
strHtml = strHtml + "<table width=100% border=0 align=center cellpadding=0 cellspacing=0><tr><td>&nbs p;</td></t r>";
strHtml = strHtml + "<tr><td></td></tr></table >";
message.Body = strHtml;
-----------------------
just use
message.Body = messageBody;
that you must specify in SmtpClient either in the constructor or later like this
1)
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp_server_name";
or
2)
SmtpClient smtpClient = new SmtpClient("smtp_server_na
No, my code doesn't give any warning....
Also, its not required to send the message in HTML, you can send plain text also
so, instead of
message.IsBodyHtml = true;
//Message body content
string strHtml = "<table width=100% border=0 align=center cellpadding=0 cellspacing=0><tr align=center><td>";
strHtml = strHtml + messageBody;
strHtml = strHtml + "<table width=100% border=0 align=center cellpadding=0 cellspacing=0><tr><td>&nbs
strHtml = strHtml + "<tr><td></td></tr></table
message.Body = strHtml;
-----------------------
just use
message.Body = messageBody;
ASKER
OK. I made the change and now set smtpClient.Host. I also added the ToAddress to the message.To list. But I get a different exception at smtpClient.Send()
Failure sending mail.
InnerException = {"Unable to read data from the transport connection: net_io_connectionclosed."}
Any ideas what the problem is?
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = Configuration.SMTPServer;
MailMessage message = new MailMessage();
MailAddress fromAddressObj = new MailAddress(fromAddress, "");
message.From = fromAddressObj;
MailAddress toAddressObj = new MailAddress(toAddress, "");
message.To.Add(toAddressOb j);
message.Subject = subject;
message.IsBodyHtml = false;
message.Body = messageBody;
smtpClient.Send(message);
Failure sending mail.
InnerException = {"Unable to read data from the transport connection: net_io_connectionclosed."}
Any ideas what the problem is?
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = Configuration.SMTPServer;
MailMessage message = new MailMessage();
MailAddress fromAddressObj = new MailAddress(fromAddress, "");
message.From = fromAddressObj;
MailAddress toAddressObj = new MailAddress(toAddress, "");
message.To.Add(toAddressOb
message.Subject = subject;
message.IsBodyHtml = false;
message.Body = messageBody;
smtpClient.Send(message);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Come to think of it, you are right. My email has not worked for months!
Any idea how to open this? I can test the technology using my Sprint wireless card. But I realy need to geet port 25 open again. I have talked to my cable provider but nothing...
newbieweb
Any idea how to open this? I can test the technology using my Sprint wireless card. But I realy need to geet port 25 open again. I have talked to my cable provider but nothing...
newbieweb
Take a look at this link, my assumption about it being port 25 closed was not entirely accurate. I believe this article has the solution to your problem.
http://www.velocityreviews.com/forums/t301912-systemnetmail-error-unable-to-read-data-from-the-transport-connection-netioconnectionclosed.html
http://www.velocityreviews.com/forums/t301912-systemnetmail-error-unable-to-read-data-from-the-transport-connection-netioconnectionclosed.html
ASKER
rhythmluvr,
I was not able to read through the link you'd sent, partly because your suggestion seems to have helped. Running from my Sprint card, I now get a different exception.
Here it is:
[System.Net.Mail.SmtpExcep tion] = {"The SMTP server requires a secure connection or the client was not authenticated. The server response was: authentication needed"}
The solution was the code from ashutosh_kumar
//if your server requires authentication add this also
sc.Credentials = new NetworkCredential("usernam e", "password");
It's working great!
Thanks,
newbieweb
I was not able to read through the link you'd sent, partly because your suggestion seems to have helped. Running from my Sprint card, I now get a different exception.
Here it is:
[System.Net.Mail.SmtpExcep
The solution was the code from ashutosh_kumar
//if your server requires authentication add this also
sc.Credentials = new NetworkCredential("usernam
It's working great!
Thanks,
newbieweb
SmtpMail.SmtpServer = Configuration.SMTPServer;
are you sure the value of "Configuration.SMTPServer"