Click here to Skip to main content
15,312,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am getting below exception
 while sending mail in c#
 Error in processing. The server response was: 5.7.3 STARTTLS is required to send mail [MR2P264CA0029.FRAP264.PROD.OUTLOOK.COM]

What I have tried:

Code used to send mail updated as below:
web.config
<pre>  <add key="AdminEmail" value="alexander.junior@test.com"/>
    <add key="SMTPServer" value="smtp.office365.com"/>
    <add key="SMTPServerPort" value="587"/>

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.Send(objMailMsg);
Posted
Updated 6-Nov-21 23:34pm

1 solution

   
Comments
Member 15421351 8-Nov-21 1:39am
   
I added EnableSSL property as true. Now getting below exception
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]
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);

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