Solved

How do i send email through gmail in asp.net C#

Posted on 2014-03-11
6
3,407 Views
Last Modified: 2014-11-12
I'm trying to send email from my asp.net page, but keep receiving the same error:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at 

Open in new window



Below is the code in button to send the message:

using System.Net.Mail;

Open in new window


MailMessage mail = new MailMessage();
        mail.From = new MailAddress("myname@gmail.com");
        mail.To.Add("recientEmail@gmail.com");
        mail.IsBodyHtml = true;
        mail.Subject = "Email Sent";
        mail.Body = "Body content from";

        SmtpClient smtp = new SmtpClient();
        smtp.Credentials = new System.Net.NetworkCredential("myname@gmail.com", "mypassword", "smtp.gmail.com");
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp.EnableSsl = true;
        smtp.UseDefaultCredentials = false;
        smtp.Send(mail);

Open in new window


Thanks
0
Comment
Question by:bmanmike39
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 35

Expert Comment

by:Kimputer
ID: 39922196
465 is the SSL port (your mentioned port uses TLS)
0
 

Author Comment

by:bmanmike39
ID: 39922211
First thank you.  But i tried port 465 and it times out
Error:
The operation has timed out.
0
 
LVL 35

Expert Comment

by:Kimputer
ID: 39922298
okay return to 587 please. Now try again, but with

 smtp.UseDefaultCredentials = false;

Open in new window



ABOVE the line where you have the credentials.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bmanmike39
ID: 39922321
I get the following error:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
0
 
LVL 35

Accepted Solution

by:
Kimputer earned 500 total points
ID: 39922336
Strange. I only made minor changes. But just to be sure, can you use this code instead:

            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("xx@gmail.com");
            mail.Sender = new MailAddress("xx@gmail.com");
            mail.To.Add("external@emailaddress");
            mail.IsBodyHtml = true;
            mail.Subject = "Email Sent";
            mail.Body = "Body content from";

            SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
            smtp.UseDefaultCredentials = false;

            smtp.Credentials = new System.Net.NetworkCredential("xx@gmail.com", "xx");
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.EnableSsl = true;
            
            smtp.Timeout = 30000;
            try
            {

                smtp.Send(mail);
            }
            catch (SmtpException e)
            {
                textBox1.Text= e.Message;
            }

Open in new window

0
 

Author Closing Comment

by:bmanmike39
ID: 39922583
Thank You!  I don't know whet the difference was but it worked.  Your code had a "sender"  and the credentials where configured different.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re looking to gather data for research or gather feedback on an idea, being able to build and distribute your own online survey is not only cost-effective, but allows you to reach a larger audience and receive results in real-time. Googl…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial will introduce a Google tool, which is a great way to learn more about dimensions in metrics in Google Analytics, even if you use the interface or the API.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question