简体   繁体   中英

Using SMTP in Visual Studio 2012

I am having problems with sending an email using SMTP, it says "waiting for local host" then eventually times out.

My assumption is that SMTP is disabled on local host, so I had a look at the directions here: http://msdn.microsoft.com/en-us/library/8b83ac7t%28v=vs.100%29.aspx But none of the steps were applicable to my computer (Windows 8, asp.net 4, VS 2012)

Is this enabled by default and the problem is my code? Here is my code

protected void SendMail()
{

    MailMessage email = new MailMessage();
    email.To.Add(new MailAddress("removed"));
    email.Subject = "Contact Form";
    email.From = new MailAddress(emailTextbox.Text);
    email.Body = "From: " + nameTextbox.Text + "<br />";
    email.Body = "Message: " + commentTextbox.Text + "<br />"; 

    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 465;
    smtp.Credentials = new NetworkCredential("removed", "removed");
    smtp.EnableSsl = true;
    smtp.Send(email);
}

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        SendMail();
        messageLabel.Text = "Thanks for your email, we will get back to you shortly.";
    }
    catch (Exception error) {
        messageLabel.Text = "Error " + error;
    }
}

Any help is appreciated, thanks!

Please see, if adding following configuration helps:

 smtp.UseDefaultCredentials = false;
 smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM