Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Windows Form Application - Send email using gmail smtp

Tags:

c#

winforms

smtp

I have been trying to create a small program to send email through smtp.gmail.com, but it always prompt me that "The operation has timed out". I know there are lots of solutions available on the net but none of it works.

try
{
    MailMessage message = new MailMessage();
    SmtpClient smtp = new SmtpClient();

    message.From = new MailAddress("[email protected]");
    message.To.Add(new MailAddress("[email protected]"));
    message.Subject = "Test";
    message.Body = "Content";

    smtp.Port = 465;
    smtp.Host = "smtp.gmail.com";
    smtp.EnableSsl = true;
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = new NetworkCredential("[email protected]", "pwd");
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.Send(message);
}
catch (Exception ex)
{
    MessageBox.Show("err: " + ex.Message);
}

Is there any way to solve this?

like image 403
noobie Avatar asked Mar 21 '13 02:03

noobie


2 Answers

Change the port to 587:

try
{
    MailMessage message = new MailMessage();
    SmtpClient smtp = new SmtpClient();

    message.From = new MailAddress("[email protected]");
    message.To.Add(new MailAddress("[email protected]"));
    message.Subject = "Test";
    message.Body = "Content";

    smtp.Port = 587;
    smtp.Host = "smtp.gmail.com";
    smtp.EnableSsl = true;
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = new NetworkCredential("[email protected]", "pwd");
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.Send(message);
}
catch (Exception ex)
{
    MessageBox.Show("err: " + ex.Message);
}
like image 166
uNople Avatar answered Sep 27 '22 23:09

uNople


how to how to send email of pdf file which is store in d drive in c# windows application...the answer is...

MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress(txtFrom.Text.ToString());
            mail.To.Add(txtmailTo.Text.ToString());
            mail.Subject = "Mail Pdf";
            var filename = @"D:/your file path/.pdf";
            mail.Attachments.Add(new Attachment(filename));
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new 
            System.Net.NetworkCredential(txtFrom.Text, txtPassword.Text);
            SmtpServer.EnableSsl = true;
            SmtpServer.Send(mail);
like image 38
Lalit Avatar answered Sep 27 '22 23:09

Lalit