从我的网站发送电子邮件

时间:2016-05-14 11:50:14

标签: c# email

the eror:

  

System.Net.Mail.SmtpException:SMTP服务器需要安全   连接或客户端未经过身份验证。服务器响应   是:5.5.1需要身份验证。了解更多信息   System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode,   System.Net.Mail.MailCommand.Send的字符串响应(SmtpConnection   conn,Byte []命令,MailAddress from,Boolean allowUnicode)at   System.Net.Mail.SmtpTransport.SendMail(MailAddress sender,   MailAddressCollection收件人,String deliveryNotify,布尔值   allowUnicode,SmtpFailedRecipientException&例外)   System.Net.Mail.SmtpClient.Send(MailMessage消息)at   forget.Button1_Click(Object sender,EventArgs e)in   c:\ Users \ Champion \ Desktop \ DinoSite \ forgot.aspx.cs:第24行

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 个答案:

答案 0 :(得分:1)

需要考虑的要点:

1)对安全性较低的应用的访问权限已“开启”https://www.google.com/settings/security/lesssecureapps

2)为您的Google帐户https://g.co/allowaccess

启用其他时区/ IP登录

答案 1 :(得分:0)

→使用SMTP安全端口:465

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

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