Click here to Skip to main content
14,389,928 members
Rate this:
Please Sign up or sign in to vote.
See more:
protected void Button1_Click(object sender, EventArgs e)
    {
  try
     {
         SmtpClient smtp= new SmtpClient("smtp.gmail.com", 587);
         smtp.EnableSsl = true;
         smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
         smtp.UseDefaultCredentials = false;
         smtp.Credentials = new System.Net.NetworkCredential("computersaddict.himanshu@gmail.com", "password");
         smtp.Timeout = 30000;

       MailMessage message = new MailMessage();
       message.To.Add("himanshu92chawla@gmail.com");
       message.From = new MailAddress("computersaddict.himanshu@gmail.com");
       message.Subject = "Test Mail";
       message.Body = "This is for testing SMTP mail from GMAIL";
         smtp.Send(message);
         Response.Write("E-mail sent!");
     }
                                
        catch (Exception ex)
        {
            Response.Write("Could not send the e-mail - error: " + ex.Message);
        }
    }
Posted
Updated 8-Jun-15 2:22am
v3
Comments
F-ES Sitecore 8-Jun-15 5:03am
   
Please do basic research like using google before asking a question, this has to be one of the most frequently asked questions and there are a ton of articles that cover sending email through gmail and the issues you might have (often it's not your code but the fact that you haven't configured your account to allow emails to be automated). If you really want to solve this issue then google is your friend, however the proper solution to your problem is to not send emails through gmail at all

http://forums.asp.net/post/5825785.aspx
Rate this:
Please Sign up or sign in to vote.

Solution 1

Make sure the username and password is correct in the line:
smtp.Credentials = new System.Net.NetworkCredential("computersaddict.himanshu@gmail.com", "password");

try commenting the line :
smtp.UseDefaultCredentials = false;

Also try setting the properties outside the constructor:
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Hi,

Check the solution 1 after follow this below steps.

1) https://accounts.google.com/DisplayUnlockCaptcha[^] //Click allow access to your Google account

2) You have to enable login from other timezone / ip for your google account.

to do this follow the link https://g.co/allowaccess[^] and allow access by clicking the continue button.

3) go to security settings at the followig link https://www.google.com/settings/security/lesssecureapps[^] and enable less secure apps . So that you will be able to login from all apps.
   

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



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