Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
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 1: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

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

try commenting the line :
C#
smtp.UseDefaultCredentials = false;

Also try setting the properties outside the constructor:
C#
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
 
Share this answer
 
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.
 
Share this 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