无法通过单击按钮发送电子邮件。 ASP.NET 和 C#
我对 C# 的经验很少,目前在通过单击按钮向指定地址发送电子邮件时遇到问题。
我有一个 ASP.NET 网页,其中包含联系表单。我希望用户在文本框中输入数据,然后当他们单击“发送”按钮时,该信息将通过电子邮件发送。
但是,当我单击“发送”时,我得到以下信息。
连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败,因为连接的主机未能响应209.85.229.109:25
C#代码如下。
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(txtemail.Text);
mail.To.Add("[email protected]");
mail.Subject = "Taxi Taxi Support";
mail.IsBodyHtml = true;
mail.Body = "First Name: " + txtname.Text;
mail.Body += "Email: " + txtemail.Text;
mail.Body += "Comments: " + txtquestion.Text;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Send(mail);
}
我正在使用 System.Net 和 System.Net.Mail
任何帮助将不胜感激。
I have little experience with C# and I am currently having an issue with sending an email from a button click to a specified address.
I have an ASP.NET webpage which contains a contact form. I want a user to enter data into text boxes and then when they click the "SEND" button this information is sent via email.
However when i click send i get the following.
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 209.85.229.109:25
The C# code is as follows.
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(txtemail.Text);
mail.To.Add("[email protected]");
mail.Subject = "Taxi Taxi Support";
mail.IsBodyHtml = true;
mail.Body = "First Name: " + txtname.Text;
mail.Body += "Email: " + txtemail.Text;
mail.Body += "Comments: " + txtquestion.Text;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Send(mail);
}
I am using the System.Net and System.Net.Mail
Any help will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您还需要指定端口 (587) 和凭据才能使用 Google 的 SMTP 服务器。
看看这个问题:使用 C# 通过 Gmail SMTP 服务器发送电子邮件< /a> 其中包含代码:
I believe you'll also need to specify the port (587) and credentials in order to use Google's SMTP server.
Have a look at this question: Sending email through Gmail SMTP server with C# which includes the code:
我认为您对 MailMessage 的使用看起来是正确的,但我会质疑 SMTP 设置。 Gmail 往往很挑剔,可能会要求您定义一个端口。
来自 Gmail 帮助:
smtp.gmail.com(使用身份验证)
使用身份验证:
是 TLS/STARTTLS 端口:587
SSL 端口:465
所以您可以添加
I think you're usage of MailMessage looks correct but I would question the SMTP settings. Gmail tends to be picky and will probably require you to define a port.
From the Gmail Help:
smtp.gmail.com (use authentication)
Use Authentication: Yes
Port for TLS/STARTTLS: 587
Port for SSL: 465
So you might add