7

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("from@gmail.com");
    message.To.Add(new MailAddress("to@gmail.com"));
    message.Subject = "Test";
    message.Body = "Content";

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

Is there any way to solve this?

noobie
  • 452
  • 4
  • 6
  • 22
  • Similar question: http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail – Nathan Goings Mar 21 '13 at 02:59
  • The timeout value is probably set to low, also check out this: http://stackoverflow.com/questions/15249817/send-mail-with-attachment/15250867#15250867 As for port 465, that should be ok – TimothyP Mar 21 '13 at 03:00
  • 3
    I'd like to point out from personal experience, and the similar question I just commented about. Many anti-malware software block this particular activity. I have personally found malware using this. You might have to look for alternatives if you are publishing to end-users. – Nathan Goings Mar 21 '13 at 03:02
  • @NathanGoings do you mean software installed on the computer where the code is being run? Then perhaps he needs to add rules to that anti malware software? Anyway... user2163646, uNople is correct. To test, download putty and open a raw connection to smtp.gmail.com:587 you should get something like `220 mx.google.com ESMTP ....` if not check your local firewall and anti-malware applications as Nathan suggested – TimothyP Mar 21 '13 at 03:08
  • @TimothyP, The link I provided has a comment concerning McAfee. However, in this case I feel he simply needs to use port 587 and/or increase the timeout. I was just warning that this isn't a good practice for software being distributed to end-users. – Nathan Goings Mar 21 '13 at 06:57
  • possible duplicate of [Sending email through Gmail SMTP server with C#](http://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp) – Ben Mar 21 '13 at 12:05
  • I know this question it too old but still... I guess it's better to create an API and use that to send the email, rather than hard-coding the credentials in the application. – mrid Feb 09 '19 at 14:30

3 Answers3

14

Change the port to 587:

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

    message.From = new MailAddress("from@gmail.com");
    message.To.Add(new MailAddress("to@gmail.com"));
    message.Subject = "Test";
    message.Body = "Content";

    smtp.Port = 587;
    smtp.Host = "smtp.gmail.com";
    smtp.EnableSsl = true;
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = new NetworkCredential("from@gmail.com", "pwd");
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.Send(message);
}
catch (Exception ex)
{
    MessageBox.Show("err: " + ex.Message);
}
uNople
  • 554
  • 5
  • 10
  • It should be, but timed out on my test app when I put it in. Changed to 587, and boom it worked. – uNople Mar 21 '13 at 03:01
  • Ok, just tested you are absolutely correct! My apologies. When opening a raw connection to 465, the connection is established but no SMTP identification is received, on 487 it works fine – TimothyP Mar 21 '13 at 03:07
  • But when I changed it to Port 587, it mention 'Failure sending mail'. Is there any mistake in my configuration? – noobie Mar 21 '13 at 03:14
  • 1
    I copied your code, changed the username/password and from/to to my email address, and ran it (timed out), changed the port to 587 and it worked. This code should work perfectly, maybe you're behind a firewall which doesn't allow outbound on either port. Make sure the email you're sending from/to exists, you can try just sending to/from the same address. Also login to the web UI to make sure your username/password combo works. – uNople Mar 21 '13 at 03:16
  • I'm pretty sure that the emails exist. I have changed my implementation using System.Web.Mail, and currently everything works pretty well. Thanks for you help anyway. – noobie Mar 22 '13 at 00:13
1

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);
benka
  • 4,732
  • 35
  • 47
  • 58
Lalit
  • 61
  • 3
1

you can use SMTP protocol to send this image as an Attachment, your code will be something like that :

using MailKit.Net.Smtp; using MimeKit;

        MimeMessage message = new MimeMessage();
        BodyBuilder Attachmint = new BodyBuilder();
        message.From.Add(new MailboxAddress("name sender", "Mail From"));
        message.To.Add(MailboxAddress.Parse("Mail To"));
        message.Subject = Subject;
        message.Body = new TextPart("plain")
        {
            Text = tex_body.Text + Massage
        };

        Attachmint.Attachments.Add("Attatchment Path");
        message.Body = Attachmint.ToMessageBody();

        SmtpClient client = new SmtpClient();
        client.Connect("smtp.gmail.com", 465, true);
        client.Authenticate("Mail from", "Password mail");
        client.Send(message);
  • SmtpClient implements IDIsposable so you need to make sure you wrap it in a using statement. – Justin Oct 08 '21 at 13:19