Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to send email using smtp from my asp.net webpage through gmail. I did this 3/4 month ago. But now my code dosn't work. May be security purpose for gmail. Now i cant find the way of sending email. Please help me. This is my code.

C#
protected void SendMail()
    {
        // Gmail Address from where you send the mail
        var fromAddress = "shariaretanvirakash@gmail.com";
        // any address where the email will be sending
        var toAddress = YourEmail.Text.ToString();
        //Password of your gmail address
        const string fromPassword = "*********";
        // Passing the values and make a email formate to display
        string subject = YourSubject.Text.ToString();
        string body = "From: " + YourName.Text + "\n";
        body += "Email: " + YourEmail.Text + "\n";
        body += "Subject: " + YourSubject.Text + "\n";
        body += "Question: \n" + Comments.Text + "\n";
        // smtp settings
        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 = 2000000;
        }
        // Passing values to smtp object
        smtp.Send(fromAddress, toAddress, subject, body);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            //here on button click what will done
            SendMail();
            DisplayMessage.Text = "Your Comments after sending the mail";
            DisplayMessage.Visible = true;
            YourSubject.Text = "";
            YourEmail.Text = "";
            YourName.Text = "";
            Comments.Text = "";
        }
        catch (Exception) { }
    }
Posted
Comments
Afzaal Ahmad Zeeshan 27-Jun-15 17:48pm    
You forgot to mention what the problem is, what went wrong now? Simply saying the code doesn't work doesn't make your post a question.

Share the exception or error received also.

1 solution

This is why you shouldn't send email via gmail, you should use the smtp server provided by your webhost or isp. Log into your gmail account manually as if it decides you need to pass a cpatcha test then it won't allow you to send email until it's done manually. There might be other issues, especially if you send a lot of email. Google can stop your mail from sending for all sorts of reasons, so as I said above, don't use them.
 
Share this answer
 
Comments
Member 11473803 11-Aug-17 7:45am    
thank you, yes the problem was on isp

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