在C#中使用hostname smtp.gmail.com发送电子邮件时遇到的问题

时间:2012-10-16 09:06:19

标签: c#

  

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

我在使用C#程序发送电子邮件时面临网络超时问题

我的代码是

protected void sendEmail(String m_RecipentTo)
{
    MailMessage mailMessage = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    mailMessage.To.Add(m_RecipentTo);
    mailMessage.Subject = m_Subject;
    mailMessage.Body = m_Body;
    mailMessage.From =  new MailAddress(m_SenderEmail);
    SmtpServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
    SmtpServer.Port = 465;
    SmtpServer.EnableSsl = true;
    SmtpServer.Credentials = new System.Net.NetworkCredential(username, password);
    SmtpServer.Send(mailMessage);
}

我尝试使用它提供的端口号587

尝试以其访问权限74.125.25.108:587禁止的方式访问套接字。

我在Filewall设置下添加了端口 - >入境规则 - >端口

1 个答案:

答案 0 :(得分:0)

这是最简单的方法..

using System.Net.Mail;

SmtpClient smtpClient = new SmtpClient("smtp.gmail.com",587);
smtpClient.EnableSsl = true;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("USER_NAME", "PASSWORD");
smtpClient.Credentials = credentials;

string[] tos;
this.to=this.to.Replace(',',' ');
//this.Bcc = this.bcc;
tos=this.to.Split();
for (int i = 0; i < tos.Length; i++)
{
    MailMessage ml = new MailMessage(this.from, tos[i].ToString(), this.sub, 
                     this.msg);
    ml.IsBodyHtml = true;
    smtpClient.Send(ml);
}