CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Friday, April 06, 2007

Configure SMTP Server

I would like to address the common configuration problems we encounter while sending an email thru code.


A list of common errors


1. System.Runtime.InteropServices.COMException: The transport failed to connect to the server


2. COMException (0x80040213): The transport failed to connect to the server.


3. Could not access 'CDO.Message' object.


Solution


In order to send an email thru code we need to configure the smtp server accordingly.


Here is the step by step procedure to configure the same


1. Go the “SMTP” Server and select “Default SMTP Virtual Server”


2. Right click and select “Properties”



3. Under “Access” tab—> “Authentication” button. See if “Anonymous access” is checked.


4. Under “Access” tab —> “Connection” button.


Opt “Only the list below”.


Add application server IP in that “Computers” list. In development environment add 127.0.0.1.


5. Under “Access” tab —> “Relay” button.


Opt “Only the list below”. 


Add application server IP in that “Computers” list. In development environment add 127.0.0.1.


6. Under “Delivery” tab —> “Advanced” button


Give the SMTP SERVER NAME  in “Smart host” field.


Hint: Find the smtp server name from Outlook. Tools -->Options —> Mail Setup


—> Email Accounts —> Press Next —> Double click the Name of the exchange server —> Here you will find the exchange server name


7. Add ASP.NET machine account to Administrative group.


 


Even after this configuration if u face an error, try the following options


1. Check the antivirus program.


Check if the “Antivirus” program installed on the machine “Macfee” is coming in between.


Some times this program blocks the mails and port 25.


Check if this is the culprit.


2. Use  the following code


//SmtpMail.SmtpServer = "127.0.0.1";


SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["SmtpServer"];


System.Web.Mail.MailMessage msgMail = new System.Web.Mail.MailMessage();



msgMail.From = txtFrom.Text.ToString();


msgMail.To = txtTo.Text;  msgMail.Cc = txtC.Text;   msgMail.Bcc = txtBcc.Text;


msgMail.Subject = txtSubject.Text;


msgMail.Body = txtMessage.Text;


msgMail.Attachments.Add(new MailAttachment(txtAttachment.Text));


SmtpMail.Send(msgMail);


 


Strange but sometimes


SmtpMail.SmtpServer = "127.0.0.1";


this line may not work.


Try replacing this with


SmtpMail.SmtpServer.insert(0,”SMTP Server Name”)




 


 


 


 

Post a Comment