SMTP安全连接和认证 [英] SMTP secure connection and authentication

查看:78
本文介绍了SMTP安全连接和认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在思索这几天,没有成功,我无法查明确切原因我code不能与Gmail的SMTP服务器验证。我已经确定的事:

I have been mulling over this for a few days, and without success I cannot pinpoint the exact cause for my code not authenticating with the smtp gmail server. What I have made sure to do:


  • 设置的Gmail为接受安全性较低的应用

  • 2的方式来检验是关闭

  • 验证码的用途是关闭

  • IIS 6 SMTP

  • 网页codeD正确使用asp.net C#

  • 端口587出站和入站设置为允许所有

这里是code

.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(异常前)code线,然后把我这个

As I have stated I have searched all over for the fix, what I have come up with in the end is that I am doing the html and c# properly, everything seems to work up to the catch (Exception ex) code line, which then sends me this

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

[System.Net.Mail.SmtpException] = {"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at"}

如果任何人有这将是伟大的任何建议!

If anyone has any suggestions that would be great!

推荐答案

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

You only need to change default credentials to true.

 smtpClient.UseDefaultCredentials = true;

下面是完整的code:

Here is the complete code:

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);
        }

    }


}

这篇关于SMTP安全连接和认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆