C# Windows Form Application - Send email using gmail smtp

前端 未结 2 480
梦如初夏
梦如初夏 2021-02-03 13:45

I have been trying to create a small program to send email through smtp.gmail.com, but it always prompt me that \"The operation has timed out\". I know there are lots of soluti

2条回答
  •  萌比男神i
    2021-02-03 14:39

    Change the port to 587:

    try
    {
        MailMessage message = new MailMessage();
        SmtpClient smtp = new SmtpClient();
    
        message.From = new MailAddress("from@gmail.com");
        message.To.Add(new MailAddress("to@gmail.com"));
        message.Subject = "Test";
        message.Body = "Content";
    
        smtp.Port = 587;
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new NetworkCredential("from@gmail.com", "pwd");
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp.Send(message);
    }
    catch (Exception ex)
    {
        MessageBox.Show("err: " + ex.Message);
    }
    

提交回复
热议问题