【问题标题】:The SMTP server requires a secure connection or the client was not authenticated-- connection timed outSMTP 服务器需要安全连接或客户端未通过身份验证 - 连接超时
【发布时间】:2014-10-30 13:46:48
【问题描述】:

这个问题已经被问过了,我已经尝试了我在堆栈溢出中发现的每一件事>

using (MailMessage mm = new MailMessage("sender@gmail.com", txtEmail.Text))
{
    mm.Subject = "Account Activation";
    string body = "Hello " + txtUsername.Text.Trim() + ",";
    body += "<br /><br />Please click the following link to activate your account";
    body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("CS.aspx", "CS_Activation.aspx?ActivationCode=" + activationCode) + "'>Click here to activate your account.</a>";
    body += "<br /><br />Thanks";
    mm.Body = body;
    mm.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587;
    smtp.EnableSsl = true;
    smtp.UseDefaultCredentials = false;
    NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "password");
    smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
    smtp.Credentials = NetworkCred;
    smtp.Send(mm);
}

我尝试将默认凭据更改为 false,我注意到当我更改端口号时。从 587 到 465 我连接超时。

【问题讨论】:

标签: c# email email-validation


【解决方案1】:

我认为你可能过于复杂了。您不需要使用 NetworkCredential。应该是这样的:

using (MailMessage mm = new MailMessage("sender@gmail.com", txtEmail.Text))
{
    mm.Subject = "Account Activation";
    string body = "Hello " + txtUsername.Text.Trim() + ",";
    body += "<br /><br />Please click the following link to activate your account";
    body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("CS.aspx", "CS_Activation.aspx?ActivationCode=" + activationCode) + "'>Click here to activate your account.</a>";
    body += "<br /><br />Thanks";
    mm.Body = body;
    mm.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 465;
    smtp.ConnectType = SmtpConnectType.ConnectSSLAuto;
    smtp.User = "gmailid@gmail.com";
    smtp.Password = "yourpassword";
    smtp.Send(mm);
}

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 2011-05-28
    • 2017-12-02
    • 2012-03-06
    • 2015-11-26
    • 2016-09-25
    相关资源
    最近更新 更多