【发布时间】:2014-04-10 11:32:19
【问题描述】:
无法通过 smtp 服务器发送邮件,因为发生“操作超时”错误。我已经尝试了这个网站上给出的几乎所有方法,尝试使用另一个端口号以及 gmail 以外的客户端,我想知道问题是什么。以下是附加的代码段。
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Security;
using System.Net.Mail;
using System.Net;
public partial class home : System.Web.UI.Page
{
SmtpClient smtp = new SmtpClient();
MailMessage newMail = new MailMessage();
protected void Page_Load(object sender, EventArgs e)
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 465;
smtp.Credentials = new NetworkCredential("user@gmail.com", "password");
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
newMail.From = new MailAddress("user@gmail.com");
newMail.Priority = MailPriority.High;
}
protected void Button_send_Click(object sender, EventArgs e)
{
try
{
newMail.To.Add(new MailAddress(TextBox_to.Text));
newMail.Subject = TextBox_sub.Text;
newMail.Body = TextBox_mail.Text;
smtp.Send(newMail);
Label_msg.Text = "Message Sent Successfully.";
Label_msg.Visible = true;
}
catch (Exception ex)
{
Label_msg.Text = "Message not sent <br/>" + ex.Message;
}
}
}
【问题讨论】:
-
我已经使用端口 587 运行了您的代码,并且它有效。它对你有用吗?
-
我尝试使用端口号 587,因为我已经提到我也使用了不同的端口号。但它仍然不起作用。它会超时。我的互联网连接也足够快,所以我不知道为什么会超时。
-
有趣!正如@ZeeshanAjmal 尝试了您的代码并且它有效。可能尝试使用不同的 Gmail 帐户以及检查您的帐户设置,不确定 Google 是否引入了新的东西!
-
我尝试了不同的帐户并更改了所有必需的设置,但仍然没有用。问题不仅在于 gmail,还在于所有其他供应商。!
-
@ZeeshanAjmal,你能告诉我发送这封电子邮件到底花了多少时间吗?
标签: c# asp.net email smtpclient mailmessage