我正在使用下面的代码..提交表单后,我收到以下错误:
错误:System.Net.Mail.Smtp异常:操作已超时。在 System.Net.Mail.SmtpClient.Send(MailMessage message) at Consultancy.Registration.Button1_Click1(对象发送器,事件参数 e) 在 G:\服务技术解决方案\咨询\注册.aspx.cs:第48行您的
法典:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
namespace Consultancy
{
public partial class Registration : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
try
{
MailMessage mailMsg = new MailMessage();
mailMsg.From = new MailAddress(TextBox4.Text);
mailMsg.To.Add("ssumishra@gmail.com");
mailMsg.IsBodyHtml = true;
mailMsg.Subject = "Contact Details";
mailMsg.Body = "akjmsjfh";
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.Port = 465;
//mailMsg.Priority = MailPriority.Normal;
smtp.Credentials = new System.Net.NetworkCredential("username@gmail.com", "password");
smtp.Timeout = 25000;
smtp.EnableSsl = true;
smtp.Send(mailMsg);
lblResult.Text = "Thank you. Your contact details and feed back has been submitted.";
}
catch (Exception ex)
{
Response.Write("Error:" + ex.ToString());
}
}
public bool True { get; set; }
}
}
尝试将 smtp 端口更改为 587:
smtp.Port = 587;
SmtpClient.TimeOut value is in milli seconds. Default value is 100,000 (100 seconds).
因此,要么不指定值(默认需要 100 秒),要么提供更多值。
https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.timeout%28v=vs.110%29.aspx
正如 Nadson 所说,正确的端口是 587,但要进行身份验证,您还需要允许来自 Gmail 帐户中安全性较低的应用程序的访问。在这里尝试一下
它对我有用,希望它有帮助。