-4

Possible Duplicate:
Sending email in .NET through Gmail

I am facing the issue of Network timed out while sending the email using the C# program

My code is

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);
}

I tried using the port number 587 it gives

An attempt was made to access a socket in a way forbidden by its access permissions 74.125.25.108:587.

I have added the port under Filewall settings -> inbound Rule -> Port

Community
  • 1
  • 1
  • 1
    There are many many questions about sending email through GMail that have been posted here in the past. Did you look at **any** of them? http://stackoverflow.com/search?q=%5Bc%23%5D+gmail+email – Oded Oct 16 '12 at 09:08
  • Yes, I have read that and using that only I have configured the mail for port 587 I am getting the exception An attempt was made to access a socket in a way forbidden by its access permissions 74.125.141.109:587 – Kanika Maheshwari Oct 16 '12 at 09:14

1 Answers1

0

This is the simplest way..

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);
}
Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105