3

我正在尝试使用某个程序向我的一些同事发送电子邮件。该程序在我自己的桌面上运行良好,但出现错误

错误的命令顺序。服务器响应是:您必须先进行身份验证(#5.5.1)

在我们位于远程位置的一台服务器上。有人可以指出我试图更改身份验证行位置的错误。而且我无法从错误中推断出太多。两个位置都使用相同的用户名和密码。

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;
        }

    }
4

5 回答 5

7

我对这个问题进行了很多搜索,并为错误消息找到了不同的解释,例如:在 BDAT 命令之后发出的 MAIL 命令,所以它说Bad sequence of commands。但实际上,当我将上面的代码更改为

SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = false; // <<<you need THIS
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.Port = 2525;//port no of your mail account 
smtp.Timeout = 500000;
smtp.Send(mm);

请注意,在分配新凭据之前UseDefaultCredentials设置为。这种设置默认顺序的更改对我来说神奇地起作用。falseCredentialsfalse

于 2013-02-15T12:22:14.430 回答
0

只是为了记录网络解决方案 SMTP 似乎需要小写的用户名。 为什么?只是因为,这就是原因。

希望它有帮助..我花了很长时间才弄清楚..

美国新罕布什尔州帕特

于 2016-02-12T15:53:31.890 回答
0

如果您尝试从不同的服务器(可能来自您的localhost)发送邮件,则会出现此错误,因此如果您想测试smtp mail代码,则需要将代码上传到远程服务器并测试plesk server在我的情况下)然后它将工作。

于 2018-08-02T15:44:49.737 回答
0

@puneet 提到的答案smtp.UseDefaultCredentials = false让我们走上了正确的道路。

但我们的情况略有不同。我们也遇到了错误Bad sequence of commands. The server response was: You must authenticate first (#5.5.1),但它只发生在使用 ASP.NET Membership 内置的密码恢复 Web 部件重置密码时。

密码恢复 Web 部件依赖于一个web.config名为的部分,<mailSettings>因此我们更改defaultCredentials="true"defaultCredentials="false"web.config

<mailSettings>
<smtp deliveryMethod="Network" from="donotreply@someone.net">
    <network defaultCredentials="false" host="mail.someone.net" userName="notifier@someone.net" password="password" port="587" />
</smtp>

这解决了密码重置的错误。基本上,这与@puneet 相同,但在不同的位置。

于 2019-03-12T17:43:05.557 回答
-1
System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient(System.Configuration.ConfigurationSettings.AppSettings["smtp"]);                

//code by cv 8july2014
mailClient.UseDefaultCredentials = false; // <<<you need THIS
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "username";
NetworkCred.Password = "Pasword";
mailClient.Credentials = NetworkCred;
//   mailClient.Host = "my company mail site";//host of your mail account
mailClient.Port = 2525;

// mailMsg = null;
//SmtpMail.SmtpServer = System.Configuration.ConfigurationSettings.AppSettings["smtp"];

while (counter < 3)
{
    try
    {
        mailClient.Send(mailMsg);        
        Thread.Sleep(2000);
        counter = 3;
        mailMsg = null;
    }
    catch (Exception ex)
    {
        counter = counter + 1;
        if (counter == 2)
        {
            throw ex;
            counter = 0;
        }
    }
}
于 2014-07-08T08:36:10.607 回答