提问者:小点点

从我的网站发送电子邮件


eror:

系统。网。邮件。SMTP服务器需要安全连接,否则客户端未通过身份验证。服务器响应为:5.5.1需要身份验证。了解更多在System.网。邮件。邮件命令。在System.网。邮件。邮件命令。在System.网。邮件。Smtp运输。SendMail(邮件地址发送者,邮件地址收集收件人,字符串传递通知,布尔允许Unicode,SmtpFailedRecipientExcION

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class forgot : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            using (var client = new SmtpClient())
            {
                MailMessage mail = new MailMessage("ozcohen06@gmail.com", (string)Email.Text);
                mail.Subject = "this is a test email.";
                mail.Body = "this is my test email body";
                client.Send(mail);
            }
            lblError.Text = "Message sent!";
        }
        catch (Exception ex)
        {
            lblError.Text = ex.ToString();
        }
    }
}

在Web配置中:

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5.2" />
  </system.web>
  <system.net>
    <mailSettings>
        <smtp deliveryMethod="Network" from="ozcohen06@gmail.com">
                <network host="smtp.gmail.com" userName="ozcohen06" password="mypass" port="587" enableSsl="true" />
        </smtp>
    </mailSettings>
</system.net>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  </system.serviceModel>
</configuration>

共2个答案

匿名用户

需要考虑的要点:

1) 对不太安全的应用程序的访问已“打开”https://www.google.com/settings/security/lesssecureapps

2) 为您的google帐户启用从其他时区/IP登录https://g.co/allowaccess

匿名用户

→ 使用SMTP安全端口:465

→ 强制脚本进行SMTP身份验证:

mail.UseDefaultCredentials = false;
mail.Credentials = basicCredential;