Solved

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

Posted on 2014-03-11
6
3,421 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
Technology Partners: 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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
This Micro Tutorial will demonstrate the easy use of Gmail embedding images in your email so the recipient of your email can view them in context.
This Micro Tutorial will demonstrate how to use comments in Google Docs. You will learn how to create, edit, and delete a comment.

734 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