Solved

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

Posted on 2014-03-11
6
3,341 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
  • 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Learn new improvements released by Google for Google Calendar. Noted in this article are simple tips and tricks that can make your everyday use of Google Calendar better.
There are many add-ons for Google docs, slides and sheets that can be extremely helpful in keeping your files organized and simple to use. Add-ons are installed into your Google docs, slides or sheets through Google's Add-On Store. One of my favorit…
This Micro Tutorial  demonstrates whether your site uses one subdomain or multiple subdomains, how to create full URLs from Google Analytics content reports. This procedure is called concatenation and can also be done with the CONCATENTATE function.…
This Micro Tutorial will demonstrate using Google Doc how to import live data to another spreadsheet in Google Spreadsheets using the IMPORTRANGE function.

759 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now