代码如下:
/// <summary>
///MailHelper的摘要说明
/// </summary>
public static class MailHelper
{
    public static string mailServer = "smtp.foxmail.com"; //ConfigurationSettings.AppSettings["mailServer"];
    public static string mailUserName = "[email protected]"; //ConfigurationSettings.AppSettings["mailUserName"];
    public static string mailPassWord = "password"; //ConfigurationSettings.AppSettings["mailPassWord"];
    public static bool RequireSmtpAuthentication = false; //ConfigurationSettings.AppSettings["RequireSmtpAuthentication"] == "1";
    /// <summary>
    /// 发送邮件
    /// </summary>
    /// <param name="to">收件人EmailAddress</param>
    /// <param name="from">发件人EmailAddress</param>
    /// <param name="subject">主题</param>
    /// <param name="body">正文</param>
    /// <param name="attach">附件</param>
    /// <param name="cc">抄送地址</param>
    /// <param name="useHtmlFormat">使用HTML格式</param>
    /// <returns>返回是否发送成功</returns>
    public static string SendMail(string to, string from, string subject, string body, string attach, string cc, bool useHtmlFormat)
    {
        if (from == "")
        {
            from = "[email protected]";
        }
        MailMessage mailMsg = new MailMessage(from,to,subject,body);
        if (ConfigurationSettings.AppSettings["EmailUseUTF8"] == "1")
        {
            mailMsg.BodyEncoding = Encoding.UTF8;
        }
        else
        {
            mailMsg.BodyEncoding = Encoding.Default;
        }
        if (attach != null && attach != "")
        {
            foreach (string sSubstr in attach.Split(','))
            {
                mailMsg.Attachments.Add(new Attachment(sSubstr));
            }
        }
        if (cc.Length > 0)
        {
            mailMsg.CC.Add(cc);
        }
        mailMsg.IsBodyHtml = useHtmlFormat;
        mailMsg.Priority = MailPriority.High;         SmtpClient client = new SmtpClient(mailServer,25);
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
        //client.EnableSsl = true;
        if (RequireSmtpAuthentication)
        {
            client.Credentials = new System.Net.NetworkCredential(mailUserName, mailPassWord, mailServer);
            client.UseDefaultCredentials = false;
        }
        try
        {
            client.Send(mailMsg);
            return "发送邮件成功。";
        }
        catch (Exception ex)
        {
            Exception source = ex;
            while (source.InnerException != null)
                source = source.InnerException;            return "发送邮件失败!错误信息:" + source.Message;
        }
    }    static void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        HttpContext.Current.Response.Write(e.Error.Message);
    }
}
然后使用
MailHelper.SendMail("[email protected]", "", "你好", "邮件内容!", "", "", true); 调用函数,总是收到
“命令顺序不正确。 服务器响应为: Error: need EHLO and AUTH first ! ” 的错误消息,请问是怎么回事?

解决方案 »

  1.   

    你要加上邮箱验证,在这个地方
    if (RequireSmtpAuthentication)
            {
                client.Credentials = new System.Net.NetworkCredential(mailUserName, mailPassWord, mailServer);
                client.UseDefaultCredentials = false;
            }改为例如:client.Credentials = new System.Net.NetworkCredential("*@qq.com","1234567890");
    可以查看MSDN
      

  2.   

    from 和mailUserName必须是一样的
      

  3.   

    using System.Net.Mail; 
    public void SendMail() 
        {    
            try 
            { 
                MailAddress from = new MailAddress("[email protected]"); 
                MailAddress to = new MailAddress("[email protected]"); 
                MailMessage message = new MailMessage(from, to); 
                message.Subject = txtTitle.Text;//发送邮件的标题 
                message.Body = txtContent.Text;//发送邮件的内容 
                //if (FileUpload1.PostedFile.FileName != "") 
                //{//发送附件 
                //    Attachment att = new Attachment(FileUpload1.PostedFile.FileName); 
                //    message.Attachments.Add(att); 
                //}             SmtpClient client = new SmtpClient("smtp.163.com"); 
                client.UseDefaultCredentials = false; 
                client.Credentials = new NetworkCredential("[email protected]", "邮箱密码"); 
                client.DeliveryMethod = SmtpDeliveryMethod.Network; 
                message.IsBodyHtml = true;             client.Send(message); 
            } 
            catch (Exception ex) 
            { 
                throw ex; 
            } 
        } //最关键的是用qq邮箱的smtp.qq.com 会报楼主的错误,换个邮箱就行了,不知道为什么