Link to home
Start Free TrialLog in
Avatar of bmanmike39
bmanmike39

asked on

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

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
Avatar of Kimputer
Kimputer

465 is the SSL port (your mentioned port uses TLS)
Avatar of bmanmike39

ASKER

First thank you.  But i tried port 465 and it times out
Error:
The operation has timed out.
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.
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
ASKER CERTIFIED SOLUTION
Avatar of Kimputer
Kimputer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank You!  I don't know whet the difference was but it worked.  Your code had a "sender"  and the credentials where configured different.