简体   繁体   中英

SMTP secure connection and authentication

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:

  • set gmail up to accept less secure apps
  • 2 way verify is OFF
  • Captcha use is OFF
  • iis 6 smtp
  • webpage coded properly using asp.net C#
  • port 587 outbound and inbound set to allow all

here is the 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>

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] = {"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!

You only need to change default credentials to true.

 smtpClient.UseDefaultCredentials = true;

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

    }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM