服务器响应是: 5.7.0 必须首先发出 STARTTLS 命令。 i16sm1806350pag.18 - gsmtp

Posted

技术标签:

【中文标题】服务器响应是: 5.7.0 必须首先发出 STARTTLS 命令。 i16sm1806350pag.18 - gsmtp【英文标题】:The server response was: 5.7.0 Must issue a STARTTLS command first. i16sm1806350pag.18 - gsmtp 【发布时间】:2013-07-02 00:10:11 【问题描述】:

我正在尝试使用 gmail 发送邮件,但我收到了一个异常 The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. i16sm1806350pag.18 - gsmtp

我为发送邮件编写的代码是:

MailMessage mail = new MailMessage(); 
mail.To.Add(txtEmail.Text.Trim()); 
mail.To.Add("Secondry@gmail.com");
mail.From = new MailAddress("mysendingmail@gmail.com");
mail.Subject = "Confirmation of Registration on Job Junction.";
string Body = "Hi, this mail is to test sending mail using Gmail in ASP.NET";
mail.Body = Body;
mail.IsBodyhtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
// smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential("mysendingmail@gmail.com", "password");
// smtp.Port = 587;
//Or your Smtp Email ID and Password
smtp.UseDefaultCredentials = false;
// smtp.EnableSsl = true;
smtp.Send(mail);

请告诉我解决方案,我没有针对此异常的任何解决方案。

【问题讨论】:

您好@Ankur,如果您解决了您的问题,请帮助我,因为我遇到了同样的错误。 @AnkurGupta - 请详细解释一下如何操作。谢谢! 【参考方案1】:

对于像我这样的非常基本的用户,来自 (FromEmailAddress) 的电子邮件是在 Gmail 上创建的实际电子邮件地址,您还需要设置不太安全的应用程序访问才能使其正常工作。 https://myaccount.google.com/lesssecureapps

那么语句的顺序也应该是正确的。

    public static bool SendEmail(string body, string subject, string toEmail)
            
                MailAddress fromAddress = new MailAddress("noreply.mymail@gmail.com", "Mail Support");
                MailAddress toAddress = new MailAddress(toEmail, "Dear Customer");
                const string fromPassword = "mymail_login_password.";
                SmtpClient smtpClient = new SmtpClient();
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = new NetworkCredential(fromAddress.Address, fromPassword);
                smtpClient.Host = "smtp.gmail.com";
                smtpClient.Port = 587;
                smtpClient.EnableSsl = true;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                MailMessage mailMessage = new 
MailMessage(fromAddress.Address,toAddress.Address,subject, body);
                try
                
                    smtpClient.Send(mailMessage);
                    return true;
                
                catch (SmtpException ex)
                
                      return false;
                
            

【讨论】:

【参考方案2】:

这个问题一夜之间也困扰着我。以下是解决方法:

将主机设置为:smtp.gmail.com 将端口设置为:587

这是 TLS 端口。我一直在使用所有其他 SMTP 端口,但没有成功。如果你这样设置enableSsl = true

Dim SMTP As New SmtpClient(HOST)
SMTP.EnableSsl = True

修剪用户名和密码字段(如果用户像我一样在注册时输入电子邮件和密码,这是防止错误的好方法),如下所示:

SMTP.Credentials = New System.Net.NetworkCredential(EmailFrom.Trim(), EmailFromPassword.Trim())

使用 TLS 端口会将您的 SMTP 视为允许您进行身份验证的 SMTPS。我立即收到来自 Google 的警告,说我的电子邮件阻止了一个存在安全风险或已过时的应用程序。我继续“打开不太安全的应用程序”。然后我更新了关于我的电话号码的信息,谷歌通过短信向我发送了一个验证码。我输入了它,瞧!

我再次运行应用程序,它成功了。我知道这个线程很旧,但我在网上搜索了它抛出的所有异常并在每行之后添加 MsgBoxes 以查看出了什么问题。这是我为便于阅读而修改的工作代码,因为我的所有变量都来自 mysql 数据库:

Try
    Dim MySubject As String = "Email Subject Line"
    Dim MyMessageBody As String = "This is the email body."
    Dim RecipientEmail As String = "recipient@gmail.com"
    Dim SenderEmail As String = "sender@gmail.com"
    Dim SenderDisplayName As String = "FirstName LastName"
    Dim SenderEmailPassword As String = "SenderPassword4Gmail"

    Dim HOST = "smtp.gmail.com"
    Dim PORT = "587" 'TLS Port
    
    Dim mail As New MailMessage
    mail.Subject = MySubject
    mail.Body = MyMessageBody
    mail.To.Add(RecipientEmail) 
    mail.From = New MailAddress(SenderEmail, SenderDisplayName)

    Dim SMTP As New SmtpClient(HOST)
    SMTP.EnableSsl = True
    SMTP.Credentials = New System.Net.NetworkCredential(SenderEmail.Trim(), SenderEmailPassword.Trim())
    SMTP.DeliveryMethod = SmtpDeliveryMethod.Network 
    SMTP.Port = PORT
    SMTP.Send(mail)
    MsgBox("Sent Message To : " & RecipientEmail, MsgBoxStyle.Information, "Sent!")
Catch ex As Exception
    MsgBox(ex.ToString)
End Try

我希望这段代码对 OP 有所帮助,但也希望像我这样迟到的人。享受吧。

【讨论】:

【参考方案3】:

自我说明:“删除UseDefaultCredentials = false”。

【讨论】:

【参考方案4】:

i 创建没有任何电话号码或相关电子邮件的新电子邮件 然后开启不太安全的应用访问 和我一起做的

【讨论】:

【参考方案5】:

登录您的 gmail 帐户https://myaccount.google.com/u/4/security-checkup/4

(见照片)查看 Google 可能因“未知”或可疑活动而屏蔽的所有位置。

【讨论】:

同时打开“不太安全的应用访问”。然后使用 SSL。【参考方案6】:

如果您(像我一样)通过系统传递所有参数,如 portusernamepassword,并且不允许修改代码,然后您可以在web.config 上进行简单的更改:

<system.net>
  <mailSettings>
    <smtp>
      <network enableSsl="true"/>
    </smtp>
  </mailSettings>
</system.net>

【讨论】:

【参考方案7】:

发送按钮逻辑:

string fromaddr = "YOURMAILID@gmail.com";
string toaddr = TextBox1.Text;//TO ADDRESS HERE
string password = "YOUR PASSWROD";

MailMessage msg = new MailMessage();
msg.Subject = "Username &password";
msg.From = new MailAddress(fromaddr);
msg.Body = "Message BODY";
msg.To.Add(new MailAddress(TextBox1.Text));
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
NetworkCredential nc = new NetworkCredential(fromaddr,password);
smtp.Credentials = nc;
smtp.Send(msg);

此代码 100% 有效。如果您的系统或防火墙中有防病毒软件限制从您的系统发送邮件,请禁用您的防病毒软件和防火墙。在此之后运行此代码...在上面的代码中,TextBox1.Text 控件用于TOaddress

【讨论】:

抱歉天真,但是关闭防火墙有什么作用呢?当然,当它继续托管时,开发人员防火墙不会有所作为?那么是否每个用户都没有防火墙?【参考方案8】:

使用代码我也遇到了同样的错误:

MailMessage mail = new MailMessage(); 
mail.To.Add(txtEmail.Text.Trim()); 
mail.To.Add("Secondry@gmail.com");
mail.From = new MailAddress("mysendingmail@gmail.com");
mail.Subject = "Confirmation of Registration on Job Junction.";
string Body = "Hi, this mail is to test sending mail using Gmail in ASP.NET";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
// smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential("mysendingmail@gmail.com", "password");
// smtp.Port = 587;
//Or your Smtp Email ID and Password
smtp.UseDefaultCredentials = false;
// smtp.EnableSsl = true;
smtp.Send(mail);

但是向上移动 2 行解决了这个问题:

MailMessage mail = new MailMessage(); 
mail.To.Add(txtEmail.Text.Trim()); 
mail.To.Add("Secondry@gmail.com");
mail.From = new MailAddress("mysendingmail@gmail.com");
mail.Subject = "Confirmation of Registration on Job Junction.";
string Body = "Hi, this mail is to test sending mail using Gmail in ASP.NET";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
// smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("mysendingmail@gmail.com", "password");
// smtp.Port = 587;
//Or your Smtp Email ID and Password
smtp.Send(mail);

【讨论】:

我不知道它对你有什么作用。我仍然遇到完全相同的问题。【参考方案9】:
**this is first  part of program**
<head runat="server">
    <title></title>
    <style>
        .style4
        
            margin-left:90px;
        
        .style3
            margin-left:130px;
        
        .style2
            color:white;
            margin-left:100px;
            height:400px;
            width:450px;
            text-align:left;
                
        .style1
            height:450px;
            width:550px;
            margin-left:450px;
            margin-top:100px;
            margin-right:500px;
            background-color:rgba(0,0,0,0.9);
        
        body
            margin:0;
            padding:0;
        
        body
            background-image:url("/stock/50.jpg");
            background-size:cover;
            background-repeat:no-repeat;
               
       </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>

            <div class="style1">
                <table class="style2">
                   <tr>
                       <td colspan="2"><h1 class="style4">Sending Email</h1></td>
                   </tr>
                   <tr>
                       <td>To</td>
                       <td><asp:TextBox ID="txtto" runat="server" Height="20px" Width="250px" placeholder="Abc@gmail.com"></asp:TextBox><asp:RequiredFieldValidator ForeColor="Red" runat="server" ErrorMessage="Required" ControlToValidate="txtto" Display="Dynamic"></asp:RequiredFieldValidator><asp:RegularExpressionValidator runat="server" ForeColor="Red" ControlToValidate="txtto" Display="Dynamic" ErrorMessage="Invalid Email_ID" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator> </td>
                   </tr>
                     <tr>
                       <td>From</td>
                       <td><asp:TextBox ID="txtfrom" runat="server" Height="20px" Width="250px" placeholder="xyz@gmail.com"></asp:TextBox> <asp:RequiredFieldValidator ForeColor="Red" Display="Dynamic" runat="server" ErrorMessage="Required" ControlToValidate="txtfrom"></asp:RequiredFieldValidator>
                           <asp:RegularExpressionValidator Display="Dynamic" runat="server" ErrorMessage="Invalid Email-ID" ControlToValidate="txtfrom" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                         </td>
                   </tr>
                     <tr>
                       <td>Subject</td>
                       <td><asp:TextBox ID="txtsubject" runat="server" Height="20px" Width="250px" placeholder="Demonstration on Youtube"></asp:TextBox><asp:RequiredFieldValidator ForeColor="Red" runat="server" ErrorMessage="Required" ControlToValidate="txtsubject"></asp:RequiredFieldValidator> </td>
                   </tr>
                     <tr>
                       <td>Body</td>
                       <td><asp:TextBox ID="txtbody" runat="server" Width="250px" TextMode="MultiLine" Rows="5" placeholder="This is the body text"></asp:TextBox><asp:RequiredFieldValidator ForeColor="Red" runat="server" ErrorMessage="Required" ControlToValidate="txtbody"></asp:RequiredFieldValidator> </td>
                   </tr>
                     <tr>
                       <td colspan="2"><asp:Button CssClass="style3" BackColor="Green" BorderColor="green" ID="send" Text="Send" runat="server" Height="30px"  Width="100px" OnClick="send_Click"/></td>
                     </tr>
                    <tr>
                        <td colspan="2"><asp:Label ID="lblmessage" runat="server"></asp:Label> </td>
                    </tr>
               </table>
            </div>

        </div>
    </form>
</body>
</html>


**this is second part of program**

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;

namespace WebApplication6

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

        

        protected void send_Click(object sender, EventArgs e)
        
            try
            
                MailMessage message = new MailMessage();
                message.To.Add(txtto.Text);
                message.Subject = txtsubject.Text;
                message.Body = txtbody.Text;
                message.From = new MailAddress(txtfrom.Text);
                SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                client.EnableSsl = true;
                client.Credentials = new System.Net.NetworkCredential(txtfrom.Text, "Sunil@123");
                for(int i=1;i<=100;i++)
                 
                client.Send(message);
                lblmessage.Text = "Mail Successfully send";
                
            
            catch (Exception ex)
            
                lblmessage.Text = ex.Message;
            
        
    

【讨论】:

欢迎来到 Stack Overflow!一般来说,如果答案包含对代码的用途的解释,以及为什么在不介绍其他人的情况下解决了问题,那么答案会更有帮助。【参考方案10】:

如果您在按照建议将该参数添加到 web.config 时收到错误“无法识别的属性 'enableSsl'”。我发现我可以通过以这种格式将其添加到我的代码文件中来解决该错误:

SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = true;

try

    smtp.Send(mm);

catch (Exception ex)

    MsgBox("Message not emailed: " + ex.ToString());

这是我的 web.config 的 system.net 部分:

<system.net>
  <mailSettings>
    <smtp from="<from_email>">
      <network host="smtp.gmail.com"
       port="587"
       userName="<your_email>"
       password="<your_app_password>" />
    </smtp>
  </mailSettings>
</system.net>

【讨论】:

【参考方案11】:

“https://www.google.com/settings/security/lesssecureapps”在登录你的gmail账号后使用这个链接并点击开启。然后运行你的应用程序,它肯定会工作。

【讨论】:

这个答案为我指明了正确的方向。因为我为谷歌邮件激活了 2fa,所以我必须创建一个单独的应用程序密码。关于安全性是有道理的。 感谢您的链接!该设置隐藏在大量额外的bs中。【参考方案12】:

步骤(1): smtp.EnableSsl = true;

如果还不够:

第 (2) 步:访问不太安全的应用程序”必须为 NetworkCredential 使用 google's settings page 的 Gmail 帐户启用:

【讨论】:

【参考方案13】:

Gmail 要求您使用安全连接。这可以像这样在您的 web.config 中设置:

<network host="smtp.gmail.com" enableSsl="true" ... />

也应该在网络服务器上启用 SSL。 参考以下链接

Enabling SSL on IIS 7.0

【讨论】:

以上是关于服务器响应是: 5.7.0 必须首先发出 STARTTLS 命令。 i16sm1806350pag.18 - gsmtp的主要内容,如果未能解决你的问题,请参考以下文章

无法发送AUTH LOGIN命令。错误:530 5.7.0必须首先发出STARTTLS命令

SMTP -> 错误:服务器不接受 AUTH:530 5.7.0 必须先发出 STARTTLS 命令。 c3sm5545732pax.9 - gsmtp PHPMailer

如何从 ELMAH 发送电子邮件?

调用必须等待服务器响应的函数的问题

Django REST Framework 发出异步请求以响应

带有异步请求的 NodeJS