发送邮件时出现异常 [英] Exception while sending mail
本文介绍了发送邮件时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发送邮件的代码如下:
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Data.SqlClient;
使用 System.Net;
使用 System.Net.Mail;
public partial class _Default:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{
}
protected void Button1_Click1( object sender,EventArgs e)
{
尝试
{
var fromaddress = neena@gmail.com; // 发件人地址
const string password = aaaaaaaaaa ; // 发件人密码;
var toaddress = TextBox2.Text; // 收件人地址
string content;
content = Hiii这是一个例子 + TextBox1.Text + 谢谢;
// smtp setting
SmtpClient cl = 新 SmtpClient();
cl.Host = smtp.gmail.com;
cl.Port = 587 ; // 端口号发送邮件
cl.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod 。网络;
cl.Credentials = new NetworkCredential(fromaddress,password);
// cl.Timeout = 2000;
// 设置结束
cl.Send(fromaddress,toaddress,TextBox3.Text,content);
}
catch (例外情况)
{
Response.Write(ex);
}
}
}
运行此代码我收到的错误是
System.Net.Mail.SmtpException:SMTP服务器需要安全连接或客户端未经过身份验证。服务器响应为: 5 。 7 。 0 必须首先发出STARTTLS命令。 ab1sm20519532pbd。 37 - System.Net.Mail.MailCommand.CheckResponse上的gsmtp(SmtpStatusCode statusCode, String 响应)在System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte []命令, String String deliveryNotify,SmtpFailedRecipientException& System.Net.Mail.SmtpClient.Send( String 来自,字符串收件人,字符串主题, String body位于_Default.Button1_Click1(对象发件人, EventArgs e)在 f:\ Training\Shopping\Default.aspx.cs:line 35
尽快回复 。
谢谢。
解决方案
你错过了一行。
client.UseDefaultCredentials = false 跨度>;
client.EnableSsl = true ; // Gmail适用于服务器安全层
并确保您的Gmail用户名和密码正确无误。
查看我的答案从asp.net发送电子邮件到gmail [ ^ ]。
感谢...
My code for sending mail is as follow
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
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 Button1_Click1(object sender, EventArgs e)
{
try
{
var fromaddress = "neena@gmail.com"; //address of sender
const string password = "aaaaaaaaaa"; //password of sender;
var toaddress = TextBox2.Text; //address of reciever
string content;
content = "Hiii this is a example" + TextBox1.Text + " Thank you";
//smtp setting
SmtpClient cl = new SmtpClient();
cl.Host = "smtp.gmail.com";
cl.Port = 587; //port no to send mail
cl.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
cl.Credentials = new NetworkCredential(fromaddress, password);
//cl.Timeout = 2000;
//setting end
cl.Send(fromaddress, toaddress, TextBox3.Text, content);
}
catch (Exception ex)
{
Response.Write(ex);
}
}
}
while running this code I am getting the error as
System.Net.Mail.SmtpException: 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. ab1sm20519532pbd.37 - gsmtp at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at System.Net.Mail.SmtpClient.Send(String from, String recipients, String subject, String body) at _Default.Button1_Click1(Object sender, EventArgs e) in f:\Training\Shopping\Default.aspx.cs:line 35
Reply as fast as possible.
Thank you.
解决方案
You have missed lines.
client.UseDefaultCredentials = false; client.EnableSsl = true; //Gmail works on Server Secured Layer
And make sure your Gmail username and password is correct.
Check my answer sending email to gmail from asp.net[^].
Thanks...
这篇关于发送邮件时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文