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;
newbiewebSr. Software EngineerAsked:
Who is Participating?
 
rhythmluvrConnect With a Mentor Commented:
It appears that port 25 is closed on your server
0
 
ashutosh_kumarCommented:
You are using
SmtpMail.SmtpServer = Configuration.SMTPServer;

are you sure the value of "Configuration.SMTPServer" is correct??? It should be the name or IP of your mail server....
0
 
ashutosh_kumarConnect With a Mentor Commented:
Are you using .net 1.1 ??? If not you should replace your code with this one

include these also in the file

using System.Net.Mail;
using System.Net.Configuration;
using System.Net;
MailMessage mailMsg = new MailMessage();
 
mailMsg.From = new MailAddress("yourID@xyz.com", "your name");
mailMsg.To.Add(new MailAddress("reciepent@xyz.com", "your friend's name"));
mailMsg.Subject = "Some subject";
mailMsg.Body = "message Body";
 
SmtpClient sc = sc = new SmtpClient("smtp.xyz.com");
 
//if your server requires authentication add this also
sc.Credentials = new NetworkCredential("username", "password");
 
sc.Send(mailMsg);

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
rhythmluvrCommented:
Here is some working code, if needed.
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>&nbsp;</td></tr></table>";
                                strHtml = strHtml + mailBody;
                                strHtml = strHtml + "<table width=100% border=0 align=center cellpadding=0 cellspacing=0><tr><td>&nbsp;</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);

Open in new window

0
 
newbiewebSr. Software EngineerAuthor Commented:
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>&nbsp;</td></tr>";
                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
0
 
newbiewebSr. Software EngineerAuthor Commented:
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.
0
 
ashutosh_kumarCommented:
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_name");


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>&nbsp;</td></tr>";
                strHtml = strHtml + "<tr><td></td></tr></table>";
                message.Body = strHtml;

-----------------------

just use
                message.Body = messageBody;

0
 
newbiewebSr. Software EngineerAuthor Commented:
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(toAddressObj);
                message.Subject = subject;
                message.IsBodyHtml = false;
                message.Body = messageBody;
                smtpClient.Send(message);
0
 
newbiewebSr. Software EngineerAuthor Commented:
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
0
 
rhythmluvrCommented:
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

0
 
newbiewebSr. Software EngineerAuthor Commented:
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.SmtpException] = {"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("username", "password");

It's working great!


Thanks,
newbieweb
0
All Courses

From novice to tech pro — start learning today.