使用gmail smtp通过asp.net发送邮件时出现操作超时错误

时间:2015-01-31 16:08:40

标签: c# asp.net smtp sendmail

我正在使用下面的代码..在提交表单后,我收到以下错误:

" Error:System.Net.Mail.SmtpException: The operation has timed out. at System.Net.Mail.SmtpClient.Send(MailMessage message) at Consultancy.Registration.Button1_Click1(Object sender, EventArgs e) in G:\servetechsolutions\Consultancy\Registration.aspx.cs:line 48Your  "

代码:

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; }
    }
}

3 个答案:

答案 0 :(得分:1)

尝试将smtp端口更改为587:

smtp.Port = 587;

答案 1 :(得分:0)

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

答案 2 :(得分:0)

正如Nadson所说,正确的端口是587,但要进行身份验证,您还需要允许从您的Gmail帐户中安全性较低的应用程序进行访问。试一试here

它对我有用,希望它有帮助..