使用gmail smtp发送电子邮件c#

时间:2011-05-04 13:42:22

标签: c# .net

  

可能重复:
  Sending email in .NET through Gmail

您好,

我正在使用如下代码,但我收到错误:

"Unable to connect to the remote server"

 MailMessage mail = new MailMessage();
        mail.To.Add("test1@gmail.com");
        mail.From = new MailAddress("test2@gmail.com");
        mail.Subject = "Test Email";
        string Body = "<b>Welcome to CodeDigest.Com!!</b>";
        mail.Body = Body;
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.Credentials = new System.Net.NetworkCredential("test1@gmail.com", "myPass");
        smtp.EnableSsl = true;
        smtp.Send(mail);

1 个答案:

答案 0 :(得分:3)

您不是通过与您进行身份验证的同一电子邮件地址发送的内容...您必须将test1@gmail.com添加为“来自”

编辑:正如Bala R所说,可能是你的防火墙基于你得到的错误