Click here to Skip to main content
15,888,088 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want send gmail with localhost
and my code is this

C#
////my message setting
         MailMessage msg = new MailMessage();
         msg.From = new MailAddress("XXXXX@gmail.com");
         msg.To.Add("YYYY@gmail.com");
         msg.Body = "body my mail";
         msg.Subject = "my mail subject";
         //msg.IsBodyHtml = true;
         //msg.Priority = MailPriority.High;

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


         client.Send(msg);



and 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
where is problem in smtp client?
Posted
Updated 8-Mar-12 3:33am
v2

The problem is within your authentication credentials. The gmail server is rejecting your request as it does not recognise the credentials so you need to use a user id and password that will be accepted. I notice that you have already been given some suggestions here[^]; what happens when you try them?

[edit]
According to the gmail guidelines[^], the SMTP server uses port 465 rather than 587 as you have coded.
[/edit]

[edit]
Further testing by me reveals that port 465 times out, but 587 works.
[/edit]
 
Share this answer
 
v3
i go to controlpanel----administrator toools--smtp setting
and set my credential and ....
and start my project
but a have this problem too



Failure sending Mail
 
Share this answer
 
Comments
Richard MacCutchan 8-Mar-12 10:06am    
Try changing your credentials to only use user name and password, I have tested this myself and it works fine without the domain part.
sadegh_rusta 8-Mar-12 15:02pm    
please write this code for me
Richard MacCutchan 9-Mar-12 4:35am    
Write what? I have suggested what you need to change in your code, so give it a try.
try this

MailAddress mailfrom = new MailAddress ( "frommail@gmail.com" );
           MailAddress mailto = new MailAddress ( "tomail@gmail.com" );
           MailMessage newmsg = new MailMessage ( mailfrom, mailto );

           newmsg.Subject = "Subject of Email";
           newmsg.Body = "Body(message) of email";

           ////For File Attachment, more file can also be attached

           Attachment att = new Attachment ( "G:\\code.txt" );
           newmsg.Attachments.Add ( att );

           SmtpClient smtps = new SmtpClient ( "smtp.gmail.com", 587 );
           smtps.UseDefaultCredentials = false;
           smtps.Credentials = new NetworkCredential ( "mail@gmail.com", "pwd" );
           smtps.EnableSsl = true;
           smtps.Send ( newmsg );
 
Share this answer
 
Try this

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