我试图通过asp.net C#代码发送电子邮件 . 但此代码不发送电子邮件并生成错误,该错误在下面的双引号中给出
“System.Net.Mail.SmtpException:发送邮件失败.---> System.Net.WebException:无法解析远程名称:System.Net.ServicePoint.GetConnection上的'smtp.gmail.com'(PooledStream PooledStream System.Net.PooledStream.Activate(Object)上的System.Net.PooledStream.Activate(Object owningObject,Boolean async,Int32 timeout,GeneralAsyncDelegate asyncCallback)中的,对象所有者,布尔异步,IP地址和地址,Socket&abortSocket,Socket&abortSocket6,Int32 timeout) System.Net.Mail.SmtpTransport.GetConnection上的System.Net.Mail.SmtpConnection.GetConnection(字符串主机,Int32端口)上的System.Net.ConnectionPool.GetConnection(Object owningObject,GeneralAsyncDelegate asyncCallback,Int32 creationTimeout)中的owningObject,GeneralAsyncDelegate asyncCallback) (String主机,Int32端口)System.Net.Mail.SmtpClient.Send(MailMessage消息)上的System.Net.Mail.SmtpClient.GetConnection()---内部异常堆栈跟踪结束---在System.Net上 . Mail.SmtpClient.Send(MailMessage消息)在emailtest._Default.Button1_Click(Object sender,EventArgs e)“
下面给出的行是我在按钮点击时调用的代码
protected void SendMail()
{
// Gmail Address from where you send the mail
var fromAddress = "myemailaddress@gmail.com";
// any address where the email will be sending
var toAddress = YourEmail.Text.ToString();
//Password of your gmail address
const string fromPassword = "myEmailAddressPassword";
// Passing the values and make a email formate to display
string subject = YourSubject.Text.ToString();
string body = "From: " + YourName.Text + "\n";
body += "Email: " + YourEmail.Text + "\n";
body += "Subject: " + YourSubject.Text + "\n";
body += "Question: \n" + Comments.Text + "\n";
// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
//here on button click what will done
SendMail();
DisplayMessage.Text = "Your Comments after sending the mail";
DisplayMessage.Visible = true;
YourSubject.Text = "";
YourEmail.Text = "";
YourName.Text = "";
Comments.Text = "";
}
catch (Exception) { }
}
please help i am stuck.
2 回答
这是我的代码: