SMTP服务器需要安全连接或者...是:5.5.1需要身份验证

时间:2015-06-21 09:58:46

标签: c# smtp

我已经看到很多解决方案尝试了每一个解决方案,但是没有一个解决方案适用于我的情况。

我尝试了以下几点,但遗憾的是没有一个工作。

有人可以帮我解决一下......

<appSettings>
   <add key="MailsenderID" value="myemail@gmail.com"/>
   <add key="Password" value="my password"/>
   <add key="Host" value="smtp.gmail.com"/>
   <add key="Port" value="587"/>
</appSettings>


public bool SendMail() 
{
    bool isSend = false;

    System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();
    MailAddress mFromAddress = new MailAddress(Convert.ToString(ConfigurationManager.AppSettings["MailsenderID"]));
    MailAddress mCC = null;
    MailAddress mBCC = null;
    if (this.Attachment.ToString().Trim().Length > 0) 
    {
        string strAttachment = this.Attachment.ToString().Trim();
        foreach(string sAttachment in strAttachment.Split(",".ToCharArray())) 
        {
            if (!String.IsNullOrWhiteSpace(sAttachment.Trim())) 
            {
                Attachment attach = new Attachment(sAttachment.ToString());
                email.Attachments.Add(attach);
            }
        }
    }

    string MailLogo = "";
    string MailPassword = Convert.ToString(ConfigurationManager.AppSettings["Password"]);
    string MailUserName = Convert.ToString(ConfigurationManager.AppSettings["MailsenderID"]);
    string MailHost = Convert.ToString(ConfigurationManager.AppSettings["Host"]);
    string MailPort = Convert.ToString(ConfigurationManager.AppSettings["Port"]);

    email.From = new MailAddress(Convert.ToString(ConfigurationManager.AppSettings["MailsenderID"]));
    email.To.Add(this.mailTo.ToLower().Replace(" ", ""));
    email.Subject = this.subject;
    if (this.bcc.Length > 0) 
    {
        string[] emails = this.bcc.Split(',');
        foreach(string emBcc in emails) 
        {
            if (emBcc.Length > 0) 
            {
                mBCC = new MailAddress(emBcc);
                email.Bcc.Add(mBCC);
            }
        }
    }
    if (this.cc.Length > 0) 
    {
        string[] emails = this.cc.Split(',');
        foreach(string emCc in emails) 
        {
            if (emCc.Length > 0) 
            {
                mCC = new MailAddress(emCc);
                email.CC.Add(mCC);
            }
        }
    }

    email.IsBodyHtml = true;
    email.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
    email.Body = "<html><head></head><body>" + this.body + "</body></html>";
    email.Priority = System.Net.Mail.MailPriority.High;

    SmtpClient ObjMail = new SmtpClient();
    ObjMail.EnableSsl = true;

    NetworkCredential credential = new NetworkCredential(MailUserName, MailPassword);
    ObjMail.Credentials = credential;
    ObjMail.Host = MailHost;
    ObjMail.DeliveryMethod = SmtpDeliveryMethod.Network;
    ObjMail.UseDefaultCredentials = false;
    ObjMail.Port = Convert.ToInt16(MailPort);
    ObjMail.Send(email);
    isSend = true;

    return isSend;
}

例外:

  

SMTP服务器需要安全连接或   客户端未经过身份验证。服务器响应是:5.5.1   需要验证。

了解更多信息

0 个答案:

没有答案