Click here to Skip to main content
15,888,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have this code for send gmail
:
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("mailFrom@gmail.com ", "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;

        }




C#
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;
               //Attachment att = new Attachment("G:\\code.txt");
               //newmsg.Attachments.Add(att);
               mail.BodyEncoding = System.Text.Encoding.UTF8;
               mail.IsBodyHtml = true;
               mail.Priority = MailPriority.High;
           }
           catch (Exception ex)
           {
               throw;
           }
           return mail;
       }


but after run,i have this error :
smtpexeception was caught

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
please help me
Posted
Updated 10-Mar-12 11:20am
v2

Just one problem which caught my eye: you brutally abuse exception. Return value is the technique superseded by exceptions. You just don't need this int return result. You just need to let exception propagate up the stack. They are designed exactly for propagation, not for catching them here and there. You practically disable exception mechanism.

Basically, you should catch exceptions only on the very top of stack in each thread (threads are usually not used in ASP.NET, so you have just one). With some exceptions from this rule in some more special cases.

Please also see my past answers:
When i run an application an exception is caught how to handle this?[^],
throw . .then ... rethrowing[^],
Handling exceptions in class library (dll)[^].

—SA
 
Share this answer
 
This is the 5th time you have posted this question and you have not done anything to investigate the issues suggested to you. Please check your credentials are valid for the SMTP server you are trying to use, rather than just reposting this same question. If all else fails then contact the server's owners and ask them for help.
 
Share this answer
 
Please refer following url to send email using gmail.

Send mail using Google Apps[^]

Hoe this may help you...
 
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