7

我一直在尝试创建一个小程序通过 smtp.gmail.com 发送电子邮件,但它总是提示我“操作已超时”。我知道网上有很多可用的解决方案,但没有一个有效。

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

有没有办法解决这个问题?

4

3 回答 3

14

将端口更改为 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);
}
于 2013-03-21T02:58:04.553 回答
1

如何发送存储在 c# windows 应用程序 d 驱动器中的 pdf 文件的电子邮件...答案是...

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);
于 2015-09-19T12:25:11.610 回答
1

您可以使用 SMTP 协议将此图像作为附件发送,您的代码将是这样的:

使用 MailKit.Net.Smtp;使用 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);
于 2021-10-08T12:51:38.100 回答