【发布时间】:2016-04-25 13:18:46
【问题描述】:
我正在尝试学习如何使用 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());
}
}
}
}
【问题讨论】:
-
你怎么知道它不起作用?错误是什么?
-
我无法向您展示整个错误,因为我无法复制它,但这里是开头几行:“System.Net.Mail.SmtpException: The SMTP server rewuires a secure connection or the客户端未通过身份验证。服务器响应为:5.5.1 身份验证“
-
来自page,如果您尝试在端口 465(使用 SSL/TLS)和端口 587(使用 STARTTLS)上配置 SMTP 服务器,但仍然无法发送邮件,请尝试配置您的 SMTP使用端口 25(使用 SSL/TLS)。
-
您可能应该按照文档尝试标准端口 25。
-
我尝试将端口设置为 25 和 465,但仍然无法正常工作。
标签: email gmail system.net.mail