我尝试使用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);
请告诉我解决方案我没有得到任何有关此例外的解决方案。
答案 0 :(得分:36)
Gmail要求您使用安全连接。这可以在你的web.config中设置,如下所示:
<network host="smtp.gmail.com" enableSsl="true" ... />
OR
也应该在网络服务器上启用SSL。 请参阅以下链接
答案 1 :(得分:21)
步骤(1): smtp.EnableSsl = true;
如果还不够:
步骤(2):&#34; 访问安全性较低的应用&#34;必须使用google's settings page
为NetworkCredential
使用的Gmail帐户启用
答案 2 :(得分:7)
这个问题也在一夜之间困扰着我。以下是解决方法:
这是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,允许您进行身份验证。我立即收到谷歌的警告说我的电子邮件阻止了一个存在安全风险或过时的应用。我接着打开了不那么安全的应用程序&#34;。然后我更新了有关我的电话号码的信息,谷歌通过短信向我发送了验证码。我进去了,瞧!“
我再次运行应用程序并且成功了。我知道这个帖子已经过时了,但是我在网上搜索了它抛出的所有例外情况,并在每一行之后添加了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 :(得分:5)
发送按钮逻辑
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);
- &gt;此代码100%工作,如果您的系统中有反病毒软件防病毒软件防火墙限制从我们的系统发送邮件,请禁用您的防病毒和防火墙系统。在此之后运行此代码....在上面的代码中,TextBox1.Text控件用于TOaddress
答案 4 :(得分:2)
“https://www.google.com/settings/security/lesssecureapps”登录您的Gmail帐户后使用此链接,然后点击开启。然后运行您的应用程序,它肯定会正常工作。
答案 5 :(得分:2)
如果您通过系统传递(像我一样)所有参数,如端口,用户名,密码,并且您不允许修改代码,那么您可以在web.config上进行这个简单的更改:
<system.net>
<mailSettings>
<smtp>
<network enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>
答案 6 :(得分:2)
如果在遵循将该参数添加到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>
答案 7 :(得分:0)
使用代码我也遇到了相同的错误:
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);
但是向上移动两行可解决此问题:
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);
答案 8 :(得分:0)
**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;
}
}
}
}
答案 9 :(得分:0)
答案 10 :(得分:0)
i创建没有任何电话号码或相关电子邮件的新电子邮件 然后打开不太安全的应用访问权限 我所做的
答案 11 :(得分:0)
自我注释:“删除UseDefaultCredentials = false
”。