开发者

Unable to send email from button click. ASP.NET and C#

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("hmgomez90@gmail.com");
    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 = 开发者_如何学JAVAnew 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.


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("myusername@gmail.com", "mypwd"),
    EnableSsl = true
};
client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜