如何从c#程序发送电子邮件?

问题描述 投票:1回答:2

我需要从我的程序发送一封电子邮件,我写了这段代码,但它没有用,错误是

“SMTP服务器需要安全连接,或客户端未经过身份验证。”服务器响应为:5.7.0必须首先发出STARTTLS命令e2sm19845644wix.15 - gsmtp

英文翻译:

“SMTP服务器需要安全连接或客户端未经过身份验证服务器响应为:5.7.0必须首先发出STARTTLS命令e2sm19845644wix.15 - gsmtp”

private void button3_Click(object sender, EventArgs e)
{
    try
    {   
         MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

         SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "xxx");
        mail.From = new MailAddress("[email protected]");
        mail.To.Add(textBox4.Text);
        mail.Subject = "Attention";
        mail.Body = textBox1.Text + textBox2.Text + textBox3.Text + "est absent de " + comboBox5.SelectedValue + "a" + comboBox4.SelectedValue;

        SmtpServer.Port = 587;



        SmtpServer.Send(mail);
        SmtpServer.EnableSsl = true;
        MessageBox.Show("mail Send");

    }
    catch (Exception ex)  
    {
        MessageBox.Show(ex.ToString());
    }
}

我按照建议改变了我的代码并得到了一个新的错误

“操作超时已过期”

翻译:

“事务超时已过期”

private void button3_Click(object sender, EventArgs e)
{
        try
        {
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            MailMessage mail = new MailMessage();
            SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "xxxxx");
            SmtpServer.UseDefaultCredentials = false;

        mail.From = new MailAddress("[email protected]");
        mail.To.Add(textBox4.Text);
        mail.Subject = "Attention";
        mail.Body = textBox1.Text + textBox2.Text + textBox3.Text + "est absent de " + comboBox5.SelectedValue + "a" + comboBox4.SelectedValue;

        SmtpServer.Port = 465;

        SmtpServer.EnableSsl = true;

        SmtpServer.Send(mail);

        MessageBox.Show("mail Send");

    }
    catch (Exception ex)  
    {
        MessageBox.Show(ex.ToString());
    }
}
c#
2个回答
3
投票

在调用SmtpServer.EnableSsl = true;之前,您需要设置SmtpServer.Send(mail);才能生效。

由于我对法语的了解有限,我会说这条消息要求您使用STARTTLS使用安全连接。

Google表示,当您使用端口587时,您需要使用TLS,如here所述。所以也许您考虑使用端口465,它表示使用SSL而不是587-TLS端口。

Here is another post解释了如何通过gmail发送电子邮件的示例。它还清楚地表明,在分配实际的凭证之前,您需要设置UseDefaultCredentials = false


2
投票
try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                mail.From = new MailAddress("[email protected]");
                mail.To.Add("[email protected]");
                mail.Subject = "Subject of email...";
                mail.Body = "Body of email...";
                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("gmailusername", "gmailpass");
                SmtpServer.EnableSsl = true;
                SmtpServer.Send(mail);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
© www.soinside.com 2019 - 2024. All rights reserved.