无法在Windows窗体中使用SMTP发送电子邮件 [英] Cannot send email with SMTP in Windows Forms
本文介绍了无法在Windows窗体中使用SMTP发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,我正试图从我的Windows Form Porject发送带有SMTP的邮件。它完全适用于hotmail(smtp.live.com),但我无法使用gmail。这是我的代码:
public static void sendMailGmail( string 从,字符串到,字符串 subject, string text, string password)
{
MailMessage mail = new MailMessage( from ,to,subject,text);
SmtpClient client = new SmtpClient( smtp .gmail.com跨度>);
client.Port = 587 ;
client.EnableSsl = true ;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false ;
client.Credentials = new System.Net.NetworkCredential( from ,password);
尝试
{
client.Send(mail);
MessageBox.Show( Mesage有很好的& quot);
}
catch (Exception ex)
{
MessageBox.Show( 发送消息时失败& quot);
MessageBox.Show(ex.Message);
}
}
所以,哪里我犯了错误?
解决方案
你不是第一个,毫无疑问不会是最后一个:来自我的Windows窗体的SMTP - Google搜索 [ ^ ]。
解决方案是在我的Gmail帐户中启用允许安全性较低的应用。
So, I am trying to send mail with SMTP from my Windows Form Porject. It perfectly works for hotmail(""smtp.live.com"") but I cann't make it work with gmail. Here's my Code:
public static void sendMailGmail(string from, string to, string subject, string text, string password)
{
MailMessage mail = new MailMessage(from, to, subject, text);
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(from, password);
try
{
client.Send(mail);
MessageBox.Show("Mesage has benn sant"");
}
catch (Exception ex)
{
MessageBox.Show("Failure while sending message"");
MessageBox.Show(ex.Message);
}
}
So, where I am making a mistake ?
解决方案
You are not the first, and no doubt will not be the last: SMTP from my Windows Form - Google Search[^].
Solution is to turn on "allow less secure apps" in my Gmail Account.
这篇关于无法在Windows窗体中使用SMTP发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文