Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am trying to send mail thorough my site. I have used the dfollwoing code for sending mail. But I am getting as “failed to send email”. Any ides on this…
I checked pinging my smtp Host also from command prompt and its replying back.
public bool mFnSendEmail(string aStrSubject,string aStrBody)
    {
        try
        {
            MailMessage lObjMail = new MailMessage();
            lObjMail.To.Add(new MailAddress("xyz@abc.com"));
            lObjMail.From = new MailAddress("xyz@abc.com ");
            lObjMail.Subject = "Auto Generated: " + aStrSubject.Trim();
            lObjMail.Body = aStrBody;
            lObjMail.IsBodyHtml = true;
            lObjMail.Priority = MailPriority.High;
            SmtpClient lObjclient = new SmtpClient("mail.abc.COM");
                  
            try
            {
                lObjclient.Send(lObjMail);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return true;

        }
        catch (Exception)
        {
            throw;
        }
    }

Thanks
Sreenath
Posted
Updated 3-Feb-11 18:27pm
v2
Comments
shakil0304003 4-Feb-11 1:47am    
Check your smtp client("mail.abc.COM") is working & there is no need of username & password.
Sreenath Gv 4-Feb-11 1:49am    
yeah..host is in working state.

Probably, your email system requires credentials in order to send.
Here is my generic Email routine:
C#
/// <summary>
/// Send an email from XXX.com
/// </summary>
/// <param name="to">Message to address</param>
/// <param name="body">Text of message to send</param>
/// <param name="subject">Subject line of message</param>
/// <param name="fromAddress">Message from address</param>
/// <param name="fromDisplay">Display neame for "message from address"</param>
/// <param name="credentialUser">User whose credentials are used for message send</param>
/// <param name="credentialPassword">User password used for message send</param>
/// <param name="attachments">Optional attachments for message</param>
public static void Email(string to,
                         string body,
                         string subject,
                         string fromAddress,
                         string fromDisplay,
                         string credentialUser,
                         string credentialPassword,
                         params MailAttachment[] attachments)
    {
    if (to == null)
        {
        to = ConfigurationManager.AppSettings["SMTPDefaultToAddress"];
        }
    string host = ConfigurationManager.AppSettings["SMTPHost"];
    body = UpgradeEmailFormat(body);
    try
        {
        MailMessage mail = new MailMessage();
        mail.Body = body;
        mail.IsBodyHtml = true;
        mail.To.Add(new MailAddress(to));
        mail.From = new MailAddress(fromAddress, fromDisplay, Encoding.UTF8);
        mail.Subject = subject;
        mail.SubjectEncoding = Encoding.UTF8;
        mail.Priority = MailPriority.Normal;
        foreach (MailAttachment ma in attachments)
            {
            mail.Attachments.Add(ma.File);
            }
        SmtpClient smtp = new SmtpClient();
        smtp.Credentials = new System.Net.NetworkCredential(credentialUser, credentialPassword);
        smtp.Host = host;
        smtp.Send(mail);
        }
    catch (Exception ex)
        {
        StringBuilder sb = new StringBuilder(1024);
        sb.Append("\nTo:" + to);
        sb.Append("\nbody:" + body);
        sb.Append("\nsubject:" + subject);
        sb.Append("\nfromAddress:" + fromAddress);
        sb.Append("\nfromDisplay:" + fromDisplay);
        sb.Append("\ncredentialUser:" + credentialUser);
        sb.Append("\ncredentialPasswordto:" + credentialPassword);
        sb.Append("\nHosting:" + host);
        ErrorLog(sb.ToString(), ex.ToString(), ErrorLogCause.EmailSystem);
        }
    }

Ignore the ErrorLog and UpdateEmailFormat methods: you can remove them or write your own.

My web.config file coantains the following in the <appSettings> section:
XML
<add key="SMTPHost" value="LocalHost"/>
<add key="SMTPCredentialUser" value="DoNotReply@XXX.com"/>
<add key="SMTPCredentialPassword" value="XXXX"/>
<add key="SMTPDefaultFromAddress" value="Webmaster@XXX.com"/>
<add key="SMTPDefaultToAddress" value="Webmaster@XXX.com"/>


Try filling in your details, and trying that - I know it works!
 
Share this answer
 
Comments
Espen Harlinn 6-Feb-11 11:39am    
Good effort, a 5
It can be because of various reasons. You need to look at them one by one.
Is the port open? Firewall permissions in place?
Further make sure you have configured SMTP configuration in Web.Config:
<system.net>
   <mailSettings>
     <smtp from="abc@somedomain.com">
       <network host="somesmtpserver" port="25" userName="name" password="pass" defaultCredentials="true" />
     </smtp>
   </mailSettings>
</system.net>

If needed, have a look at this Microsoft Video tutorial:
Use ASP.NET to send Email from Website[^]
 
Share this answer
 
Comments
Espen Harlinn 6-Feb-11 11:38am    
Good 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