我想学习如何发送一个简单的电子邮件与c#visual studio窗体应用程序。 (它也可以是我关心的所有控制台应用程序)。下面是我的代码(我看不出有什么不对的代码,它应该工作吧?):从窗体应用程序发送电子邮件(为什么不工作)
using System.Net.Mail;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("(here is my email)");
mail.To.Add("(here is my email)");
mail.Subject = "toja";
mail.Body = "ja";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("(here is my email)", "(here is my password)");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
你怎么知道它不起作用?什么是错误? – randominstanceOfLivingThing
我不能告诉你整个eror的原因,我不能复制它,但这里是开始的几行:“System.Net.Mail.SmtpException:SMTP服务器rewuires一个安全的连接或客户端没有authuenticated。服务器响应是:5.5.1身份验证“ –
从此[页面](https://support.google.com/mail/answer/78775?hl=zh-CN),如果您尝试在端口465上配置SMTP服务器(使用SSL/TLS)和587端口(使用STARTTLS),但仍然无法发送邮件,请尝试配置SMTP以使用端口25(使用SSL/TLS)。 – randominstanceOfLivingThing