Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am getting below error while trying to send mail in c#.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 Client not authenticated to send mail. [PN1PR01CA0073.INDPRD01.PROD.OUTLOOK.COM]
I have set EnablSSL property as true in the code.

What I have tried:

Code is as below:
MailMessage objMailMsg = new MailMessage();
MailAddress fromAddress = new MailAddress(Common.FromMail);
SmtpClient smtpClient = new SmtpClient();

int port = Common.SMTPPort;
string FileName = Common.EMAIL_FILE_PATH+objEmail.AttachedFileName;
string IPaddr = Common.SMTPServer;
objMailMsg.From = fromAddress;

string[] Toarray = objEmail.ToEmailId.Split(',');
foreach (string ToAddress in Toarray)
{
if (ToAddress.Trim() != string.Empty)
objMailMsg.To.Add(ToAddress.Trim());
}
if (objEmail.CCEmailId != null && objEmail.CCEmailId.Length > 0)
{
string[] Ccarray = objEmail.CCEmailId.Split(',');
foreach (string CcAddress in Ccarray)
{
if(CcAddress.Trim()!= string.Empty)
objMailMsg.CC.Add(CcAddress.Trim());
}
}

char[] chrDelim = new char[] { ',' };

foreach (string strSubstr in FileName.Split(chrDelim))
{
if (File.Exists(strSubstr))
{
Attachment objAttachment = new Attachment(strSubstr);
objMailMsg.Attachments.Add(objAttachment);
}
}
objMailMsg.Subject = objEmail.EmailSubject;
objMailMsg.Body = objEmail.EmailBody;
objMailMsg.IsBodyHtml = true;
if (port == 0)
{
port = 25;

}
Common.WriteDDSLog("Email Sent :-"+FileName);
smtpClient.Port = port;
smtpClient.Host = IPaddr;
smtpClient.EnableSsl = true;
smtpClient.Send(objMailMsg);
Posted
Updated 8-Nov-21 22:13pm

1 solution

You need to add the authentication details to the SMTP object. See SmtpClient.Credentials Property (System.Net.Mail) | Microsoft Docs[^]
 
Share this answer
 

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


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