Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public static void SendMail(string sHost, int nPort, string sUserName, string sPassword, string sFromName, string sFromEmail,
    string sToName, string sHeader, string sMessage, bool fSSL)
   {
       try
       {
           //if (sToName.Length == 0)
           //    sToName = sToEmail;
           if (sFromName.Length == 0)
               sFromName = sFromEmail;
           System.Web.Mail.MailMessage Mail = new System.Web.Mail.MailMessage();
           Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = sHost;
           Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;
           Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = nPort.ToString();
           if (fSSL)
               Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true";
           if (sUserName.Length == 0)
           {
               //Ingen auth
           }
           else
           {
               Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
               Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = sUserName;
               Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = sPassword;
           }
           Mail.To = sToName;
           Mail.From = sFromEmail;
           Mail.Subject = sHeader;
           Mail.Body = sMessage;
           Mail.BodyFormat = System.Web.Mail.MailFormat.Html;
          // System.Web.Mail.SmtpMail.SmtpServer = sHost;
           System.Web.Mail.SmtpMail.SmtpServer.Insert(0, sHost);
           System.Web.Mail.SmtpMail.Send(Mail);
       }
       catch (Exception ex)
       {
           insertErrorLog.ErrorLogWithOutUserId(Auth.GetCurrentPageName(), ex.Message);
       }
   }



error is


The transport failed to connect to the server.
Posted
Comments
LittleYellowBird 14-Jun-10 6:01am    
Hi, you will get better answers if you describe both what you are trying to achieve and what the problem is in more detail. :-)

1 solution

I assume you get the error in your catch block, otherwise it would be useful to know what line is generating the error.

My guess is

System.Web.Mail.SmtpMail.Send(Mail);


If I were you I'd try using another SMTPHost name - it seems like you can't connect to the one you're using...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900