Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i write this code for send gmail with asp.net 4
but this code has a problem

where is problem?

C#
////my message setting
MailMessage msg = new MailMessage();
msg.From = new MailAddress("gsadegh.rusta@gmail.com");
msg.To.Add("sadegh_rusta@yahoo.com");
msg.Body = "body my mail";
msg.Subject = "my mail subject";
msg.Priority = MailPriority.High;

SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("gsadegh.rusta@gmail.com", "gsadeghrusta", "smtp.gmail.com");
client.Host = "smtp.gmail.com";
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = false;
client.UseDefaultCredentials =true;

client.Send(msg);


error is:

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
or:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. v51sm5742961eef.2


i do test with ports:25 and 465
Posted
Updated 8-Mar-12 1:38am
v2

Couple of suggestion. Set UseDefaultCredentials attribute before instantiating NetworkCredential. Your code will be re ordered as:

C#
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials =true;
client.Credentials = new NetworkCredential("gsadegh.rusta@gmail.com", "gsadeghrusta", "smtp.gmail.com");
client.Host = "smtp.gmail.com";
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = false;

client.Send(msg);
 
Share this answer
 
Comments
sadegh_rusta 8-Mar-12 7:50am    
i do it
but timeout my project after run
help me
Another idea is:

After enabling POP or IMAP on your Gmail account, there is no matching enableSsl setting and so config file will be:

XML
<system.net>
  <mailSettings>
    <smtp from="myusername@gmail.com" deliveryMethod="Network">
      <network defaultCredentials="false" enableSsl="true" host="smtp.gmail.com" port="587" password="password" userName="myusername@gmail.com"/>
    </smtp>
  </mailSettings>
</system.net>
 
Share this answer
 
Comments
sadegh_rusta 8-Mar-12 8:02am    
i do it but has error
errror is :
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
i have this error now
Failure send email
 
Share this answer
 
Comments
Richard MacCutchan 8-Mar-12 11:40am    
Please stop posting questions and replies as solutions. Use the "Have a Question or Comment" link, or the "Reply" link on specific messages. And try to do some sensible debugging for yourself; the above comment is totally meaningless as we do not know the context of where or how it appears.
Try this code

C#
public int SendUserMail(string fromad, string toad, string body, string header, string subjectcontent)
   {
       int result = 0;
       MailMessage usermail = Mailbodplain(fromad, toad, body, header, subjectcontent);
       SmtpClient client = new SmtpClient();
       //Add the Creddentials- use your own email id and password
       client.Credentials = new System.Net.NetworkCredential("your user id ", "pwd"); ;

       client.Host = "smtp.gmail.com";
       client.Port = 587;
       client.EnableSsl = true;
       try
       {
           client.Send(usermail);
           result = 1;
       }
       catch (Exception ex)
       {
           result = 0;
       } // end try

       return result;

   }
   public MailMessage Mailbodplain(string fromad, string toad, string body, string header, string subjectcontent)
   {
       System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
       try
       {
           string from = fromad;
           string to = toad;
           mail.To.Add(to);
           mail.From = new MailAddress(from, header, System.Text.Encoding.UTF8);
           mail.Subject = subjectcontent;
           mail.SubjectEncoding = System.Text.Encoding.UTF8;
           mail.Body = body;
           mail.BodyEncoding = System.Text.Encoding.UTF8;
           mail.IsBodyHtml = true;
           mail.Priority = MailPriority.High;
       }
       catch (Exception ex)
       {
           throw;
       }
       return mail;
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900