?
Solved

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

Posted on 2014-03-11
6
Medium Priority
?
3,459 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 36

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 36

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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

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 36

Accepted Solution

by:
Kimputer earned 2000 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

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

Question has a verified solution.

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

Working with spreadsheets can be a daunting task, especially when having to deal with large amounts of data. All you see are rows and rows of numbers and soon your eyes begin to glaze over. Take advantage of the tools in Google Sheets to create prof…
Learn about cloud computing and its benefits for small business owners.
This Micro Tutorial will demonstrate how marketers can use the Mobile Emulation Tool in Chrome Developer Tool. This will let you preview your site on any mobile device.
This Micro Tutorial demonstrates how to quickly find related content for YourTango's posts using MozBar Chrome extension.
Suggested Courses

762 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