C# SmtpClient 遇到的两个Exception

来源:互联网 发布:酷宝数据 造假 编辑:程序博客网 时间:2024/06/08 16:58

1、命令顺序不正确。 服务器响应为:Error: need EHLO and AUTH first !  解决方法。

      我目前遇到这个问题有两种情况:

①设置了SmtpClient.UseDefaultCredentials属性为True。那么SmtpClient.NetworkCredential 属性设置应该在其之后。(也就是你设置的发送邮箱的用户名和密码在这个属性之后)

SMTP 事务的主机的名称或 IP 地址 是 QQ邮箱的话,需要设置SmtpClient.EnableSsl。因为QQ邮箱是用ssl套接字。

2、参数或变量中有语法错误。 服务器响应为:mail from address must be same as authorization user

①、网上大多是说用户名或密码错误。使用QQ邮箱 需要 在邮箱设置-账户-里面 生成授权码即密码,并且使用授权码进行登录。
        #region 发送邮件        /// <summary>        ///  发送邮件 带附件        /// </summary>        /// <param name="fromUser">发件人地址</param>        /// <param name="fromUserName">发件人显示名</param>        /// <param name="toUser">收件人地址</param>        /// <param name="cc">抄送地址,多个</param>        /// <param name="subjectNm">主题</param>        /// <param name="bodyAll">正文</param>        /// <param name="fj">附件地址</param>        /// <returns></returns>        public static bool SendMailsf(string fromUser, string fromUserName, string toUser, string cc, string subjectNm, string bodyAll, string fj)        {            bool ret = false;            try            {                Attachment objMailAttachment;                //创建一个附件对象                  //objMailAttachment = new Attachment("d:\\test.txt");//发送邮件的附件                  objMailAttachment = new Attachment(fj);//发送邮件的附件                  MailMessage mm = new MailMessage();                mm.From = new MailAddress(fromUser, fromUserName, Encoding.UTF8);                mm.To.Add(toUser);                mm.Attachments.Add(objMailAttachment);//将附件附加到邮件消息对象中                  mm.Subject = subjectNm;//主题                mm.SubjectEncoding = Encoding.UTF8;                mm.Body = bodyAll;//正文                mm.BodyEncoding = Encoding.UTF8;                mm.IsBodyHtml = true;                //mm.Priority = MailPriority.High;//加急邮件!                #region 抄送 收件人地址                string[] listc = null;                try                {                    if (cc != null && cc != "")                    {                        listc = cc.Split(',');                        for (int i = 0; i < listc.Length; i++)                        {                            mm.CC.Add(listc[i].ToString());                        }                    }                }                catch (Exception list)                {                    throw list;                }                #endregion                SmtpClient client = new SmtpClient();                System.Net.NetworkCredential nc = new System.Net.NetworkCredential();                nc.UserName = "发送的邮箱";                nc.Password = "发送的密码";                client.UseDefaultCredentials = true;                client.Credentials = nc;                client.DeliveryMethod = SmtpDeliveryMethod.Network;                client.Host = "smtp.qq.com";                client.EnableSsl = true; //QQ邮件 使用                client.Send(mm);                ret = true;            }            catch (System.Net.Mail.SmtpException ex)            {                MessageBox.Show(ex.ToString());            }            return ret;        }        #endregion

1 0
原创粉丝点击