简体   繁体   中英

C# Windows Form Application - Send email using gmail 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("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?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM