【问题标题】:sending an email from an forms Application (why dosen't it work)从表单应用程序发送电子邮件(为什么不起作用)
【发布时间】: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


【解决方案1】:

我使用以下方法从 C# Forms 应用程序发送电子邮件,它可以工作。 也许您应该尝试添加 smtp.UseDefaultCredentials = false 属性并设置 Credentials 属性?

// Create our new mail message
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("toAddress");
mailMessage.From = new MailAddress("fromAddress");
mailMessage.Subject = "subject";
mailMessage.Body = "body";

// Set the IsBodyHTML option if it is HTML email
mailMessage.IsBodyHtml = true;

// Enter SMTP client information
SmtpClient smtpClient = new SmtpClient("mail.server.address");
NetworkCredential basicCredential = new NetworkCredential("username", "password"); 
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
smtpClient.Send(mailMessage);

如果使用 Gmail,您可以在此处https://support.google.com/a/answer/176600?hl=en 找到 SMTP 服务器信息。看起来地址是smtp.gmail.com,端口是465(SSL)或587(TLS)。我建议您先尝试使用默认端口 25 以确保您的电子邮件能够通过,然后根据需要调整到其他端口。

【讨论】:

  • 我已经尝试使用您的代码和这行中的“NetworkCredentials”:NetworkCredential basicCredential = new NetworkCredential("username", "password");是一个错误我该怎么做?我觉得我错过了一些非常明显的东西,这让我非常困扰。
  • 您可能需要以下导入...使用 System.Security.Cryptography;使用 System.Net;使用 System.Net.Mail;使用 System.Net.Mime;
  • 当我点击按钮时出现错误。我认为这与端口有关。我应该使用什么端口?
  • @bananalord 错误说明了什么?你能把它粘贴到一个pastebin吗?
  • 这里是整个代码和错误的截图(我认为我的问题是在“mail.server.address”中我把端口放在了正确的位置?)gyazo.com/85d63a324d2822bd4b7f13ca49f04b65
猜你喜欢
  • 1970-01-01
  • 2022-11-14
  • 2016-09-18
  • 2012-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-23
相关资源
最近更新 更多