如何从 windows 表单发送 gmail

How to send gmail from windows forms

我想要的是使用 C# winforms 创建一个软件,可以自动向各种人员列表发送电子邮件。

我试过的是这个实现:

try
{
    SmtpClient client = new SmtpClient("smtp.gmail.com");
    client.Port = 587;
    client.EnableSsl = true;
    client.Timeout = 100000;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Credentials = new NetworkCredential(
        "yourid@gmail.com", "yourgmailpassword");
    MailMessage msg = new MailMessage();
    msg.To.Add(textBox_To.Text);
    msg.From = new MailAddress("yourid@gmail.com");
    msg.Subject = textBox_Subject.Text;
    msg.Body = textBox_Message.Text;
    Attachment data = new Attachment(textBox_Attachment.Text);
    msg.Attachments.Add(data);
    client.Send(msg);
    MessageBox.Show("Successfully Sent Message.");
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

但问题是,Google 不允许这种方法。
当我打开我的 gmail 帐户时,我收到一封“阻止可疑登录”的电子邮件。

还有其他方法吗?

这是 google 的安全功能。为了解决这个问题,google 给了你两个选择

Change account access for less secure apps

To help keep Google Apps users' accounts secure, we may block less secure apps from accessing Google Apps accounts. As a Google Apps user, you will see a "Password incorrect" error when trying to sign in. If this is the case, you have two options:

  • Option 1: Upgrade to a more secure app that uses the most up to date security measures. All Google products, like Gmail, use the latest security measures.
  • Option 2: Change your settings to allow less secure apps to access your account. We don't recommend this option because it might make it easier for someone to break into your account. If you want to allow access anyway, follow these steps:
    1. Go to the "Less secure apps" section in My Account.
    2. Next to "Access for less secureapps," select Turn on. (Note to Google Apps users: This setting is hidden if your administrator has locked less secure app account access.)

Link: https://support.google.com/accounts/answer/6010255?hl=en