简体   繁体   English

如何从我的 C# windows 应用程序发送 email?

[英]How do I send an email from my C# windows application?

Im working on a Windows app where the user has to enter a password.我正在开发一个 Windows 应用程序,用户必须在其中输入密码。 I also have a "Forgot Password" link.我还有一个“忘记密码”链接。 on the window. When that's clicked, I have the user enter their email address and click a Submit button.在 window 上。单击后,我让用户输入他们的 email 地址,然后单击“提交”按钮。 Every time they enter an email address and click the button, I get the error message:每次他们输入 email 地址并单击按钮时,我都会收到错误消息:

SmtpException has occured: The SMTP server requires a secure connection or the client was not authenticated.发生 SmtpException:SMTP 服务器需要安全连接或客户端未通过身份验证。 The server response was: 5.7.0 Authentication Required.服务器响应为:5.7.0 需要身份验证。

The code I'm using is:我正在使用的代码是:

try
{
    Cursor = Cursors.WaitCursor;

    MailAddress from = new MailAddress("bgatt64@gmail.com", "Bob Gatto");
    MailAddress to = new MailAddress("bgatto64@yahoo.com", "Bob Gatto");

    MailMessage eMsg = new MailMessage(from, to);
    eMsg.Subject = "Your Password Renewal Request";
    eMsg.IsBodyHtml = true;
    eMsg.Body = "This is the body.";

    SmtpClient eClient = new SmtpClient("smtp.gmail.com", 587);
    eClient.EnableSsl = true;
    eClient.UseDefaultCredentials = true;gmail

    // The following email and password used is that of my own gmail email
    // that I use for my own personal email.

    eClient.Credentials = new System.Net.NetworkCredential("<MyOwnEmail@gmail.com>", "<MyPassword>");
    eClient.Send(eMsg);
}
catch (SmtpException ex)
{
    throw new ApplicationException("SmtpException has occurred: " + ex.Message);
}
catch (Exception ex)
{
    throw ex;
}

What else needs to be done?还有什么需要做的?

You cannot use your plain google account password to authenticate to Gmail SMTP server anymore as Google requires two-step authentication now.您不能再使用普通的谷歌帐户密码来验证 Gmail SMTP 服务器,因为谷歌现在需要两步验证。 You'll need to use the App Password instead:您需要改用应用密码

在此处输入图像描述

You'll get the string with the 4 groups of characters.您将获得包含 4 组字符的字符串。 Now you need to use it in your code:现在您需要在代码中使用它:

eClient.Credentials = new System.Net.NetworkCredential("<MyOwnEmail@gmail.com>", "<App Password>");

You can find more info Here您可以在此处找到更多信息

After the removal of less secure apps you can no longer use your actual google password to connect to the smpt server you need to create an apps password.删除安全性较低的应用程序后,您将无法再使用实际的谷歌密码连接到您需要创建应用程序密码的 smpt 服务器。

How to create a Apps Password for connecting to Google's SMTP server.如何创建用于连接到 Google 的 SMTP 服务器的 Apps 密码。

using System;
using System.Net;
using System.Net.Mail;

namespace GmailSmtpConsoleApp
{
    class Program
    {
        private const string To = "[redacted]@gmail.com";
        private const string From = "[redacted]@gmail.com";
        
        private const string GoogleAppPassword = "";
        
        private const string Subject = "Test email";
        private const string Body = "<h1>Hello</h1>";
        
        
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            
            var smtpClient = new SmtpClient("smtp.gmail.com")
            {
                Port = 587,
                Credentials = new NetworkCredential(From , GoogleAppPassword),
                EnableSsl = true,
            };
            var mailMessage = new MailMessage
            {
                From = new MailAddress(From),
                Subject = Subject,
                Body = Body,
                IsBodyHtml = true,
            };
            mailMessage.To.Add(To);

            smtpClient.Send(mailMessage);
        }
    }
}

Note: if you are trying to connect users then you could also use Xoauth2 but using an apps password is far easer solution.注意:如果您尝试连接用户,那么您也可以使用 Xoauth2,但使用应用程序密码是更简单的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从C#应用程序发送电子邮件? - How do I send an email message from my C# application? 如何在C#中从Windows应用程序发送电子邮件? - How to send Email from windows application in c#? 如何从WinRT / Windows应用商店应用程序发送电子邮件? - How do I send an email from a WinRT/Windows Store application? 如何从Windows Phone 8应用程序发送电子邮件? - How do I send an email from a Windows Phone 8 application? 如何在不泄露密码的情况下从C#发送电子邮件? - How do I send email from C# without revealing my password? 如何从C#Windows应用程序将参数传递到HTML电子邮件模板中以发送电子邮件 - How to pass parameters into an HTML email template from C# Windows application to Send an email 如何在C#for .NET中从Windows窗体应用程序发送命令提示符参数? - How Do I Send Command Prompt Arguments from a Windows Form Application in C# for .NET? 如何将远程 web 主机上的 php 文件中的数据发送到我计算机上的 c# 应用程序? - how do i send data from a php file on a remote web host to a c# application on my computer? 如何从 C# 应用程序发送签名的电子邮件? - How do I send signed emails from C# application? 如何从C#Windows窗体应用程序的数据库中检索信息 - How do I retrieve information from database for my C# Windows Form Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM