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:

                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!";
                System.Windows.Forms.MessageBox.Show("Sent mail");
            catch (Exception e)

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

Recommended Answers

All 2 Replies

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;

Thanks a ton man it worked :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.