【问题标题】:How to send email from the c# program?如何从 c# 程序发送电子邮件?
【发布时间】:2015-06-28 17:50:53
【问题描述】:

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

" Le server SMTP requiert une connexion sécurisée ou le client n'était pas authentifié. La réponse du server était : 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("oussabdalla@gmail.com", "xxx");
        mail.From = new MailAddress("oussabdalla@gmail.com");
        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());
    }
}

我根据建议更改了我的代码并得到了一个新错误

“Le délai d'attente de l'opération a expiré”

翻译:

“事务超时已过期”

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("oussabdalla@gmail.com", "xxxxx");
            SmtpServer.UseDefaultCredentials = false;

        mail.From = new MailAddress("oussabdalla@gmail.com");
        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#


【解决方案1】:
try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                mail.From = new MailAddress("fromeamilp@gmail.com");
                mail.To.Add("toemail@gmail.com");
                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());
            }

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】:

    • 那是新错误“Le server SMTP 需要 une connexion sécurisée ou le client n'était pas authentifié。La réponse du serverur était : 5.5.1 Authentication required. Learn more at"
    • 对不起,我的法语到此结束^.^请翻译成英文。
    • hhhh 很高兴:p
    • 这是新错误“SMTP 服务器需要安全连接或客户端未通过身份验证服务器响应是:.. 5.5.1 需要身份验证了解更多信息”
    • 记住我说过的话:Google 假定端口 587 上的连接是 TLS 安全的。你想使用 SSL,所以使用 465。
    猜你喜欢
    • 2020-01-20
    • 2015-06-07
    • 1970-01-01
    • 2012-03-08
    • 2010-09-26
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多