我对 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("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c1a9aca6aeaca4bbf8f181a6aca0a8adefa2aeac" rel="noreferrer noopener nofollow">[email protected]</a>");
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
任何帮助将不胜感激。
最佳答案
我相信您还需要指定端口 (587) 和凭据才能使用 Google 的 SMTP 服务器。
看看这个问题:Sending email through Gmail SMTP server with C#其中包括代码:
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c7165696f796e727d71795c7b717d7570327f7371" rel="noreferrer noopener nofollow">[email protected]</a>", "mypwd"),
EnableSsl = true
};
client.Send("myus<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f194839f909c94b1969c90989ddf929e9c" rel="noreferrer noopener nofollow">[email protected]</a>", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e08d99959385928e818d85a0878d81898cce838f8d" rel="noreferrer noopener nofollow">[email protected]</a>", "test", "testbody");
关于c# - 无法通过单击按钮发送电子邮件。 ASP.NET 和 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6190036/