Click here to Skip to main content
12,506,338 members (52,696 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET .NET C#4.0 .NET4
i am tried following code for sending mail

 protected void Page_Load(object sender, EventArgs e)
    {
        sendMail();
    }
    public void sendMail()
    {
        string from="shivani.sharma.corporate@gmail.com";
        string to = "shivani.sharma.corporate@gmail.com";
        string subject = "Hi there";
        string body = "Hey there <br /> Check out http://www.ossagho.com";
        sendMailall(from, to, subject, body);
    }
    public void sendMailall(string from, string to, string subject, string body)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(to);
        mail.From = new MailAddress(from);
        mail.Subject = subject;
        mail.Body = body;
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Credentials =
             new System.Net.NetworkCredential("shivani.sharma.corporate@gmail.com",
                                                                "thisisnotmypassword");
        smtp.EnableSsl = true;
        smtp.Send(mail);
     }
}




i am getting an error in

smtp.Send(mail);

error is::::::
Failure sending mail.
Posted 31-Jan-13 20:06pm
Updated 31-Jan-13 20:09pm
v2
Comments
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
 
good
Please Sign up or sign in to vote.

Solution 1

use this code :
var smtp = new System.Net.Mail.SmtpClient();
    {
        smtp.Host = "smtp.gmail.com";
        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);
  Permalink  
Comments
shivani 2013 1-Feb-13 3:12am
   
not getting result..........
same msg
Rate this: bad
 
good
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
        Try
            Dim smtpserver As New SmtpClient
            Dim mailmsg As New MailMessage
            smtpserver.Host = "smtp.gmail.com"
            smtpserver.Port = 465
            smtpserver.EnableSsl = True
            smtpserver.UseDefaultCredentials = False
            mailmsg.From = New MailAddress(shivani.sharma.corporate@gmail.com)
            mailmsg.Subject = "Hi there"
            mailmsg.To.Add(shivani.sharma.corporate@gmail.com)
            mailmsg.IsBodyHtml = True
            mailmsg.Body =  "Hey there <br /> Check out http://www.ossagho.com";
            smtpserver.Send(mailmsg)
            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
http://support.google.com/mail/bin/answer.py?hl=en&answer=78775[^]
  Permalink  
v4
Rate this: bad
 
good
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; 
#endregion
 
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)
        {
            try
            {
            System.Net.NetworkCredential credential = new System.Net.NetworkCredential(from, password);
            System.Net.Mail.MailMessage mailMessage = new MailMessage();
            mailMessage.To.Add(to);
            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;
           
                smtpClient.Send(mailMessage);
                return true;
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Exceeption in sending mail:" + ex.Message);
                return false;
            }
        }
    }

Value of smtpClient=new SmtpClient("smtp.gmail.com", 587)
port = 587
from: XXX@gmail.com
password: Password of user XXX.
Make sure that you are connected to internet otherwise you'll get Failure sending mail error as you mentioned.
  Permalink  
v4
Comments
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
 
good
Please Sign up or sign in to vote.

Solution 4

Try with below code ..it 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);
client.Send(message);
}

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

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


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

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 | Mobile
Web02 | 2.8.160927.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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