Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code for sending mail is as follow

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page
{
  
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        try
        {
            var fromaddress = "neena@gmail.com";  //address of sender
            const string password = "aaaaaaaaaa";                    //password of sender;
            var toaddress = TextBox2.Text;                  //address of reciever
            string content;
            content = "Hiii this is a example" + TextBox1.Text + " Thank you";
            //smtp setting
            SmtpClient cl = new SmtpClient();
            cl.Host = "smtp.gmail.com";
            cl.Port = 587;      //port no to send mail
            cl.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            cl.Credentials = new NetworkCredential(fromaddress, password);
            //cl.Timeout = 2000;
            //setting end
            cl.Send(fromaddress, toaddress, TextBox3.Text, content);
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }
}


while running this code I am getting the error as

C#
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. ab1sm20519532pbd.37 - gsmtp at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at System.Net.Mail.SmtpClient.Send(String from, String recipients, String subject, String body) at _Default.Button1_Click1(Object sender, EventArgs e) in f:\Training\Shopping\Default.aspx.cs:line 35


Reply as fast as possible.
Thank you.
Posted
v2
Comments
CHill60 18-Mar-13 11:49am    
Clue is in the error message - the credentials you are supplying are invalid
joshrduncan2012 18-Mar-13 11:50am    
What line is the error pointing to?
ZurdoDev 18-Mar-13 11:54am    
It's just a matter of sending the right values.

1 solution

You have missed lines.
C#
client.UseDefaultCredentials = false;
client.EnableSsl = true; //Gmail works on Server Secured Layer

And make sure your Gmail username and password is correct.

Check my answer sending email to gmail from asp.net[^].

Thanks...
 
Share this answer
 
v2
Comments
NeenaM 19-Mar-13 7:10am    
Can you tell me why client.UseDefaultCredentials = false; is used for???
This option will set the client to use the default credentials of the currently logged in user.

If you set it to true, then it will try to use the user's credentials.
If you set it to false, then it will use the values explicitly set for the Credentials property of the client.

As you are using Gmail to send mail, then you have to make it false to send the Gmail username and password in order to authenticate the user.
NeenaM 19-Mar-13 13:01pm    
Ok Thanks
It was really usefull.
Your code helped me.

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