SMTP 安全连接和身份验证

本文关键字:身份验证 连接 安全 SMTP | 更新日期: 2023-09-27 18:36:56

我已经考虑了几天,但没有成功,我无法确定我的代码未通过 smtp gmail 服务器进行身份验证的确切原因。我确保做什么:

  • 将 Gmail 设置为接受安全性较低的应用
  • 2 路验证已关闭
  • 验证码使用已关闭
  • IIS 6 SMTP
  • 使用 C# 正确编码 asp.net 网页
  • 端口 587 出站和入站设置为允许所有

这是代码

.aspx.cs

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;
using System.Net;

public partial class Contact : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Btn_Submit_Click(object sender, EventArgs e)
    {
        MailMessage mailMessage;
        SmtpClient smtpClient;
        try
        {
            mailMessage = new MailMessage();
            mailMessage.To.Add("013blitz@gmail.com");
            mailMessage.From = new MailAddress("crebrum.web.design@gmail.com");
            mailMessage.Subject = "ASP.NET e-mail test";
            mailMessage.Body = "Hello world,'n'nThis is an ASP.NET test e-mail!";
            smtpClient = new SmtpClient("smtp.gmail.com");
            smtpClient.Port = 587;
           NetworkCredential nc = new NetworkCredential("crebrum.web.design@gmail.com", "Meowqwe789doG");
           smtpClient.Credentials = nc;

            smtpClient.EnableSsl = true;

            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.UseDefaultCredentials = false;

            smtpClient.Send(mailMessage);
            //Response.Write("E-mail sent!");

        }
        catch (Exception ex)
        {
            //Response.Write("Could not send the e-mail - error: " + ex.Message);
        }
    }

}

.HTML

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage/MasterPage.master" AutoEventWireup="true" CodeFile="Contact.aspx.cs" Inherits="Contact" %>
<asp:Content ID="Content1" ContentPlaceHolderID="title" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="contentbody" Runat="Server">

    <div class="contact">
        <div class="contact-heading text-center">
            <div class="container">
                <div class="row">
                    <div class="col-md-6">
                        <h1>Contact Us</h1>
                        <div class="headingpng">
                            <img src="img/heading.png" />
                        </div>
                    </div>
                </div>        
            </div>
        </div>
    </div>
    <div id="contact-form" class="clearfix">
        <h4 class="text-center">
            Please fill in the contact form below with any questions you may have.
        </h4>
         <ul id="errors" class="">
             <li id="info">
                 There were some problems with your form submission:
             </li>
         </ul>
        <p id="success">
            Thanks for your message! We will get back to you ASAP!
        </p>
        <div>
            <label for="name">Name: <span class="required">*</span></label>
            <input type="text" id="name" name="name" value="" placeholder="John Doe" required="required" autofocus="autofocus" />
            <asp:TextBox runat="server" ID="Tb_Name" ></asp:TextBox>
            <label for="email">Email Address: <span class="required">*</span></label>
            <input type="email" id="email" name="email" value="" placeholder="johndoe@example.com" required="required" />
            <label for="telephone">Telephone: </label>
            <input type="tel" id="telephone" name="telephone" value="" />
            <label for="enquiry">Enquiry: </label>
            <select id="enquiry" name="enquiry">
                <option value="general">General</option>
                <option value="sales">Sales</option>
                <option value="support">Support</option>
            </select>
            <label for="message">Message: <span class="required">*</span></label>
            <textarea id="message" name="message" placeholder="Your message must be greater than 20 charcters" required="required" data-minlength="20"></textarea>
            <span id="loading"></span>
            <%--<input type="submit" value="Submit" id="submit-button" />--%>
            <asp:Button runat="server" ID="Btn_Submit" Text="Submit" OnClick="Btn_Submit_Click" />
            <p id="req-field-desc"><span class="required">*</span> indicates a required field</p>
        </div>
</div>

</asp:Content>

正如我所说,我已经四处寻找修复程序,最后我想出的是我正在正确执行 html 和 c#,一切似乎都符合 catch(异常 ex)代码行,然后向我发送这个

[System.Net.Mail.SmtpException] = {"SMTP 服务器需要安全连接,或者客户端未经过身份验证。服务器响应为:5.5.1 需要身份验证。了解更多"}

如果有人有任何建议,那就太好了!

SMTP 安全连接和身份验证

您只需将默认凭据更改为 true。

 smtpClient.UseDefaultCredentials = true;

以下是完整的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Btn_Submit_Click(object sender, EventArgs e)
    {
        MailMessage mailMessage;
        SmtpClient smtpClient;
        try
        {
            mailMessage = new MailMessage();
            mailMessage.To.Add("013blitz@gmail.com");
            mailMessage.To.Add("crebrum.web.design@gmail.com");
            mailMessage.From = new MailAddress("crebrum.web.design@gmail.com");
            mailMessage.Subject = "Change your password";
            mailMessage.Body = "Hello Crebrum,'n'nPlease change your password for crebrum.web.design@gmail.com!" +
                "You posted the password on stack overflow and anyone can access your email now.";
            smtpClient = new SmtpClient("smtp.gmail.com");
            smtpClient.Port = 587;
            NetworkCredential nc = new NetworkCredential("crebrum.web.design@gmail.com", 
                "{Here is where I masked your password. You are welcome}");
            smtpClient.Credentials = nc;

            smtpClient.EnableSsl = true;

            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.UseDefaultCredentials = true;

            smtpClient.Send(mailMessage);
            //Response.Write("E-mail sent!");

        }
        catch (Exception ex)
        {
            //Response.Write("Could not send the e-mail - error: " + ex.Message);
        }
    }

}
相关文章: