无法通过单击按钮发送电子邮件。 ASP.NET 和 C#

发布于 2024-11-10 16:41:35 字数 964 浏览 0 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒几许 2024-11-17 16:41:35

我相信您还需要指定端口 (587) 和凭据才能使用 Google 的 SMTP 服务器。

看看这个问题:使用 C# 通过 Gmail SMTP 服务器发送电子邮件< /a> 其中包含代码:

var client = new SmtpClient("smtp.gmail.com", 587)
{
    Credentials = new NetworkCredential("[email protected]", "mypwd"),
    EnableSsl = true
};
client.Send("[email protected]", "[email protected]", "test", "testbody");

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:

var client = new SmtpClient("smtp.gmail.com", 587)
{
    Credentials = new NetworkCredential("[email protected]", "mypwd"),
    EnableSsl = true
};
client.Send("[email protected]", "[email protected]", "test", "testbody");
时间你老了 2024-11-17 16:41:35

我认为您对 MailMessage 的使用看起来是正确的,但我会质疑 SMTP 设置。 Gmail 往往很挑剔,可能会要求您定义一个端口。

来自 Gmail 帮助:

smtp.gmail.com(使用身份验证)

使用身份验证:

是 TLS/STARTTLS 端口:587

SSL 端口:465

所以您可以添加

smtp.Port = 587

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

smtp.Port = 587
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文