我正在尝试使用程序向一些同事发送电子邮件。这个程序在我自己的桌面上工作正常,但是出错了
错误的命令序列。服务器响应是:您必须首先进行身份验证(#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;
}
}
答案 0 :(得分:5)
我搜索了很多关于这个问题并找到错误信息的不同解释,例如:在BDAT命令后发出的MAIL命令所以它说错误的命令序列。但实际上,当我将上面的代码更改为
时,问题就被排序了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
在false
分配新凭据之前设置为Credentials
。将默认设置为false
的序列更改为我神奇地工作。
答案 1 :(得分:0)
仅为记录网络解决方案SMTP似乎需要小写的用户名。 为什么?这就是原因。
希望它有所帮助..花了我很长时间来弄明白......
Pat NH USA
答案 2 :(得分:0)
如果您尝试从其他服务器(可能来自localhost
)发送邮件,则会出现此错误,因此,如果要测试smtp mail
代码,则需要将您的代码上传到远程服务器并进行测试(在我的情况下为plesk server
)。
答案 3 :(得分:-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;
}
}
}