我正在尝试使用某个程序向我的一些同事发送电子邮件。该程序在我自己的桌面上运行良好,但出现错误
错误的命令顺序。服务器响应是:您必须先进行身份验证(#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;
}
}