Click here to Skip to main content
15,127,128 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hi,
I used this code to sending email from my desktop application
C#
string smtpAddress = "smtp.gmail.com";
            int portNumber = 587;
            bool enableSSL = true;

            string emailFrom = "xyz@gmail.com";
            string password = "myPassword";
            string emailTo = "xyz@yahoo.com";
            string subject = "Hello";
            string body = "Hello, I'm just writing this to say Hi!";

            using (MailMessage mail = new MailMessage())
            {
                mail.From = new MailAddress(emailFrom);
                mail.To.Add(emailTo);
                mail.Subject = subject;
                mail.Body = body;
                mail.IsBodyHtml = true;
                // Can set to false, if you are sending pure text.

                mail.Attachments.Add(new Attachment("C:\\n.txt"));

                using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
                {
                    smtp.Credentials = new NetworkCredential(emailFrom, password);
                    smtp.EnableSsl = enableSSL;
                    smtp.Send(mail);
                }
            }


and i have this error
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Authentication required

any help ?
Posted
Updated 28-Aug-17 5:54am
v3
Comments
Shweta N Mishra 28-Nov-14 8:36am
   
do you have a smtp configured at your end ?
You can not access yahoo's SMTP, you do not have access to it.
Heba Kamel 28-Nov-14 8:46am
   
No, how can I do this?
I can change yahoo's SMTP to GMail's SMTP (smtp.gmail.com), right?
Richard MacCutchan 28-Nov-14 8:57am
   
You need to have an account with the SMTP service to use it for email, whether it is Yahoo or gmail.
Heba Kamel 28-Nov-14 18:41pm
   
How can i get this account, and where i used this account in my code?
Richard MacCutchan 29-Nov-14 4:23am
   
Go to gmail and create it.
Heba Kamel 30-Nov-14 16:37pm
   
How ?
Richard MacCutchan 1-Dec-14 3:21am
   
What do you mean how? Go to Gmail and follow the instructions.
Heba Kamel 1-Dec-14 8:59am
   
I mean I go to Gmail but don't know from where get account ..

I have tested this entire code in my own IDE and the result for this problem was the username/password match error.

Make sure that you're using the correct password for the valid username on the server.

Possible issue: You're asking a question about Gmail, but you're using the Yahoo smtp server. Are you sure you're using the correct settings for credentials for the Yahoo server, or did you mistakenly wrote that Yahoo smpt host address?
   
v2
Comments
Heba Kamel 28-Nov-14 8:56am
   
i wrote this line
Smtp.Port = 25;
but still error

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. c5sm16036467wik.3 - gsmtp
Afzaal Ahmad Zeeshan 28-Nov-14 10:04am
   
Did you try writing it inside the using block?
Afzaal Ahmad Zeeshan 28-Nov-14 14:22pm
   
Please see my updated answer.
Heba Kamel 28-Nov-14 17:57pm
   
Sorry, it's mistake i mean Gmail smtp (smtp.gmail.com)
yes, i used it in using block
in this line
smtp.Credentials = new NetworkCredential("xyz@gmail.com", password);
i must write email or user name in first parameter?
Heba Kamel 28-Nov-14 18:45pm
   
Make sure that you're using the correct password for the valid username on the server .

Did you mean my email password ?
Afzaal Ahmad Zeeshan 29-Nov-14 6:44am
   
Yes, your username and password combination must be correct and valid. Otherwise it will generate this error.
Heba Kamel 29-Nov-14 10:05am
   
but my password and username is correct
Heba Kamel 29-Nov-14 10:18am
   
I create new account for test and this is a code , you can test it please
but I have the same error

string smtpAddress = "smtp.gmail.com";
int portNumber = 587;
bool enableSSL = true;

string emailFrom = "sendtest875@gmail.com";
string password = "testsend0123";
string emailTo = "xyz@yahoo.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";

using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// Can set to false, if you are sending pure text.

mail.Attachments.Add(new Attachment("C:\\n.txt"));

using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
Heba Kamel 30-Nov-14 14:01pm
   
Afzaal ?
Afzaal Ahmad Zeeshan 30-Nov-14 14:45pm
   
Heba, the only possible issues have been shared with you and the solution is to make sure you're passing correct username/password combination to the correct SMTP server. Otherwise you will continue to get this error.
Heba Kamel 30-Nov-14 16:05pm
   
Okay, did you test the previous code with email (sendtest875@gmail.com)
I created this mail for testing and I have the same error , you can test and this is a password (testsend0123)
Afzaal Ahmad Zeeshan 1-Dec-14 10:40am
   
I have tried it, there is a login error. If you login to the account you will see there are a bunch of errors saying Login attempt declined etc.
Heba Kamel 1-Dec-14 15:14pm
   
Error in login to Gmail account ?!
I try it and it's correct
sendtest875@gmail.com
testsend0123
Afzaal Ahmad Zeeshan 1-Dec-14 15:23pm
   
No, not in the desktop version from mail.google.com, but seems like from the Console application that I am testing... anyways try using your own account, because for me, my account works fine. :-)
Heba Kamel 1-Dec-14 15:34pm
   
but my account didn't work too ?!!
Heba Kamel 30-Nov-14 16:23pm
   
Is there any setting in Gmail before sending ?
BillWoodruff 28-Nov-14 21:14pm
   
+5 good analysis, and fact-checking !
Afzaal Ahmad Zeeshan 29-Nov-14 6:43am
   
Thanks Bill. :-)
Heba Kamel 29-Nov-14 10:58am
   
I agree with you,
Thank you Afzaal
first
string smtpAddress = "smtp.gmail.com";
second
check ur credentials , username and password of ur account
   
Comments
Heba Kamel 28-Nov-14 17:58pm
   
Did you mean in this line i must write user name rather than email in first parameter ?
smtp.Credentials = new NetworkCredential("xyz@gmail.com", password);
[no name] 15-Aug-16 9:50am
   
Hi afzal did you get the solution. I have the same problem, unable to figure out what to do

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