c#窗体应用程序-发送电子邮件使用gmail smtp
本文关键字:gmail smtp 电子邮件 窗体 应用程序 -发 | 更新日期: 2023-09-27 18:05:06
我一直在尝试创建一个小程序,通过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);
}
有办法解决这个问题吗?
将端口改为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);
}
如何在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);
您可以使用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);