954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Smtp Mail not working, I'm at a loss.

I'm trying to send an e-mail through a C# program. It works until it sends it, then the operation times out. This is what I have:

{
            try
            {
                SmtpClient client = new SmtpClient("smtp.gmail.com");
                client.UseDefaultCredentials = false;
                client.EnableSsl = true;
                client.Port = 465;
                client.Credentials = new NetworkCredential("mygmailacc@gmail.com", "mypassword");
                MailAddress from = new MailAddress("mygmailacc@gmail.com");
                MailAddress to = new MailAddress("receiver@host.com");
                MailMessage message = new MailMessage(from, to);
                message.Subject = "Hello";
                message.Body = "Hello world of SMTP email!";
                client.Send(message);
                System.Windows.Forms.MessageBox.Show("Sent mail");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }


Can you figure out what is wrong with the above code? I'm at a loss...

Armanious
Light Poster
25 posts since Apr 2011
Reputation Points: 10
Solved Threads: 2
 

Hi, try the following code hope it helps

protected void Page_Load(object sender, EventArgs e)
    {
        Send("receiver@host.com", "Test Email", "Hello", false);
        
    }
     

    public void Send(string _to, string _subject, string _body, bool isHtml)
    {
        MailMessage mm = new MailMessage();
        mm.From = new MailAddress("mygmailacc@gmail.com");
        mm.To.Add(new MailAddress(_to));
        mm.Subject = _subject;
        mm.Body = _body;
        mm.IsBodyHtml = isHtml;

        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true; 
        System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
        NetworkCred.UserName = "mygmailacc@gmail.com";
        NetworkCred.Password = "mypassword";
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mm);
    }
Alan81
Newbie Poster
6 posts since Mar 2011
Reputation Points: 10
Solved Threads: 3
 

Thanks a ton man it worked :)

Armanious
Light Poster
25 posts since Apr 2011
Reputation Points: 10
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: