I am trying to send email to some of my colleagues using a program. This program is working fine at my own desktop but is giving an error
Bad sequence of commands. The server response was: You must authenticate first (#5.5.1)
on one of our servers in remote location. Can someone point out the error I have tried to change the position of the line of authentication. And I am not able to deduce much from the error. The same username and password are being used at both locations.
public static Boolean sendemail(String strFrom, string strTo, string strCC, string strBCC, string strReplyTO, string strSubject, string strBody, string strAttachmentPath, bool IsBodyHTML)
{
//Array arrToArray;
//char[] splitter = { ';' };
//arrToArray = strTo.Split(splitter);
MailMessage mm = new MailMessage();
bool flag = isEmailsString(strFrom);
if (flag)
{
mm.From = new MailAddress(strFrom);
}
flag = isEmailsString(strTo);
if (flag)
{
mm.To.Add(strTo);
}
flag = isEmailsString(strCC);
if (flag)
{
mm.CC.Add(strCC);
}
flag = isEmailsString(strBCC);
if (flag)
{
mm.Bcc.Add(strBCC);
}
flag = Regex.IsMatch(strReplyTO.Trim(), @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
if (strReplyTO != null)
{
if (flag)
{
mm.ReplyTo = new MailAddress(strReplyTO);
}
}
if (strSubject != null)
{
mm.Subject = strSubject;
}
mm.IsBodyHtml = IsBodyHTML;
if (!string.IsNullOrEmpty(strAttachmentPath))
{
Array attachmentArray;
char[] attachSplitter = { ',' };
attachmentArray = strAttachmentPath.Split(attachSplitter);
foreach (string s in attachmentArray)
{
string st = s.Trim();
if (!string.IsNullOrEmpty(st))
{
Attachment attach = new Attachment(st);
// Add the file attachment to this e-mail message.
mm.Attachments.Add(attach);
//strBodyFinal.Append(Environment.NewLine+"Go to the following link for more information"+Environment.NewLine);
//strBodyFinal.Append(st);
}
}
}
if (strBody != null)
{
mm.Body = strBody;
}
//foreach (string s in arrToArray)
// {
// mm.To.Add(new MailAddress(s));
// }
SmtpClient smtp = new SmtpClient();
try
{
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "my user name";
NetworkCred.Password = "my password";
smtp.Credentials = NetworkCred;
smtp.Host = "my company mail site";//host of your mail account
//smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Port = 2525;//port no of your mail account
smtp.Timeout = 500000;
smtp.Send(mm);
return true;
}
catch (Exception ex)
{
mm.Dispose();
smtp = null;
Console.WriteLine(ex.Message, ex.StackTrace);
return false;
}
}
I searched a lot about the issue and finding different interpretations for the error message like: MAIL command issued after BDAT command so it says Bad sequence of commands. But actually the problem was sorted when I changed my code above to
Notice that
UseDefaultCredentialsis set tofalsebefore theCredentialsare assigned new credentials. This change of sequence of setting default tofalsemagically worked for me.