Click here to Skip to main content
12,290,502 members (66,978 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET Windows IIS Win7 SMTP , +
i want send gmail with localhost
and my code is this

////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 8-Mar-12 3:30am
Edited 8-Mar-12 3:33am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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]
  Permalink  
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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 );
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Try this

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;
   }
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160518.1 | Last Updated 9 Mar 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100