Click here to Skip to main content
13,593,166 members
Rate this:
Please Sign up or sign in to vote.
i am tried following code for sending mail

 protected void Page_Load(object sender, EventArgs e)
    public void sendMail()
        string from="";
        string to = "";
        string subject = "Hi there";
        string body = "Hey there <br /> Check out";
        sendMailall(from, to, subject, body);
    public void sendMailall(string from, string to, string subject, string body)
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress(from);
        mail.Subject = subject;
        mail.Body = body;
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "";
        smtp.Credentials =
             new System.Net.NetworkCredential("",
        smtp.EnableSsl = true;

i am getting an error in


error is::::::
Failure sending mail.
Posted 31-Jan-13 20:06pm
Updated 31-Jan-13 20:09pm
Richard MacCutchan 1-Feb-13 8:46am
Put your send in a try block and catch the exception that gets raised. This will tell you more about the problem.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

use this code :
var smtp = new System.Net.Mail.SmtpClient();
        smtp.Host = "";
        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
        smtp.Timeout = 20000;
    // Passing values to smtp object
    smtp.Send(fromAddress, toAddress, subject, body);
shivani 2013 1-Feb-13 3:12am
not getting result..........
same msg
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I guess the error is because of the port
try with both 465 and 587. and make sure the username/pwd is correct and SSL is enabled. it works for me on 587
Try this
please convert it to c#

Imports System.Net
Imports System.Net.Mail

    Protected Sub cmd_send_Click(sender As Object, e As System.EventArgs) Handles cmd_send.Click
            Dim smtpserver As New SmtpClient
            Dim mailmsg As New MailMessage
            smtpserver.Host = ""
            smtpserver.Port = 465
            smtpserver.EnableSsl = True
            smtpserver.UseDefaultCredentials = False
            mailmsg.From = New MailAddress(
            mailmsg.Subject = "Hi there"
            mailmsg.IsBodyHtml = True
            mailmsg.Body =  "Hey there <br /> Check out";
            lbl_err.Text = "Sent"
        Catch ex As Exception
            lbl_err.Text = ex.Message.ToString
        End Try
    End Sub

just give a check on the below link as well of gmail smtp settings[^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Code written below worked for me..
#region using
using System;
using System.Diagnostics;
using System.Net.Mail; 
namespace ProjectUtils
    /// <summary>
    /// Helps to send mail messages.
    /// </summary>
    public class EmailHelper
        /// <summary>
        /// Sends the mail to recepients.
        /// </summary>
        /// <param name="from">Sender</param>
        /// <param name="password">Password of mail sender</param>
        /// <param name="to">Recepient</param>
        /// <param name="subject">Subject of Message</param>
        /// <param name="body">Message body</param>
        /// <param name="smtpClient">Name of smtp client</param>
        /// <param name="port">Port number of smtp client</param>
        /// <returns>True if succeeds, false if exception occurs</returns>
        public bool SendMail(string from, string password, string to, string subject, string body, System.Net.Mail.SmtpClient smtpClient, int port)
            System.Net.NetworkCredential credential = new System.Net.NetworkCredential(from, password);
            System.Net.Mail.MailMessage mailMessage = new MailMessage();
            mailMessage.Subject = subject;
            mailMessage.From = new System.Net.Mail.MailAddress(from);
            mailMessage.Body = body;
            System.Net.Mail.SmtpClient smtpClient = smtpClient;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.EnableSsl = true;
            smtpClient.Credentials = credential;
            smtpClient.Port = port;
                return true;
            catch (Exception ex)
                Trace.WriteLine("Exceeption in sending mail:" + ex.Message);
                return false;

Value of smtpClient=new SmtpClient("", 587)
port = 587
password: Password of user XXX.
Make sure that you are connected to internet otherwise you'll get Failure sending mail error as you mentioned.
Kishor Deshpande 1-Feb-13 9:33am
You can make SendMail() as static method if you dnt wish to create instances of EmailHelper :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Try with below code worked for me.

public void SendMail(string to, string subject, string body)
string uname = WebConfigurationManager.AppSettings["name"].ToString();
string pwd = WebConfigurationManager.AppSettings["pwd"].ToString();
int port = Convert.ToInt32(WebConfigurationManager.AppSettings["clientport"].ToString());
string host = WebConfigurationManager.AppSettings["SMTPMailServer"].ToString();
MailMessage message = new MailMessage();
message.To.Add(new MailAddress(to));
message.From = new MailAddress(uname);
message.Subject = subject;
message.Body = body;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.SubjectEncoding = System.Text.Encoding.UTF8;
SmtpClient client = new SmtpClient();
client.Port = port;
client.Host = host;
System.Net.NetworkCredential nc = new System.Net.NetworkCredential(uname, pwd);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = nc;
message.IsBodyHtml = true;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);

public bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
// replace with proper validation
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
return false;

and in web.config file set username,password and host like..

<add key="SMTPMailServer" value=""/>
    <add key="name" value="ur mail id"/>
    <add key="pwd" value="set password here"/>
    <add key="clientport" value="587"/>
    <add key="EnableSSLforMail" value="true"/>

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web03-2016 | 2.8.180618.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100