授权SMTP在电子邮件者中

发布于 2025-02-07 13:15:24 字数 2258 浏览 0 评论 0 原文

我需要在应用程序中编写一堂课,以发送电子邮件以进行授权proccess。我认为我的代码还可以,但是在汇编程序中,始终提出一个例外“ SMTP 5.7.0”。我读到,它与Google帐户中的安全设置有关,但是当我尝试更改它时,Google在官方Gmail网站上显示: “为了帮助确保您的帐户安全,从2022年5月30日起,Google不再支持使用第三方应用程序或设备,这些应用程序要求您仅使用用户名和密码登录Google帐户。”

您现在如何从应用程序中的Google帐户发送邮件?我试图将电子邮件更改为Onet,但这也不起作用。

    public static class EmailSender
    {
        public static bool Send
            (string from, string to, string subject, string name, string body, string fromPass)
        {
            byte[] encodedByteFrom = Encoding.Default.GetBytes(from);
            from = UTF8Encoding.Default.GetString(encodedByteFrom);

            byte[] encoddedByteTo = Encoding.Default.GetBytes(to);
            to = UTF8Encoding.Default.GetString(encoddedByteTo);

            byte[] encodedByteSubject = Encoding.Default.GetBytes(subject);
            subject = UTF8Encoding.Default.GetString(encodedByteSubject);

            byte[] encodedByteName = Encoding.Default.GetBytes(name);
            name = UTF8Encoding.Default.GetString(encodedByteName);

            byte[] encodedByteBody = Encoding.Default.GetBytes(body);
            body = UTF8Encoding.Default.GetString(encodedByteBody);

            byte[] encodedByteFromPass = Encoding.Default.GetBytes(fromPass);
            fromPass = UTF8Encoding.Default.GetString(encodedByteFromPass);

            MailMessage message = new MailMessage();

            message.From = new MailAddress(from);
            message.To.Add(new MailAddress(to));
            message.Subject = subject;
            message.Body = body;

            SmtpClient client = new SmtpClient();
            client.Credentials = new NetworkCredential(from, fromPass);
            client.EnableSsl = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            client.Port = 587;
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true;

            try
            {
                client.Send(message);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show
                            (ex.Message, "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                return false;
            }
        }
    }

I need to write a class sending emails for authorization proccess in registration in the application. I think that my code is okay, but during compilation program always throw an exception "SMTP 5.7.0". I read, that it is related with security settings in google account, but when I'm trying to change that, google shows on official gmail website a message:
"To help keep your account secure, from May 30, 2022, ​​Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password."

How do you sending your mails from google accounts in your apps right now? I was trying to change email to onet, but it also does not work.

    public static class EmailSender
    {
        public static bool Send
            (string from, string to, string subject, string name, string body, string fromPass)
        {
            byte[] encodedByteFrom = Encoding.Default.GetBytes(from);
            from = UTF8Encoding.Default.GetString(encodedByteFrom);

            byte[] encoddedByteTo = Encoding.Default.GetBytes(to);
            to = UTF8Encoding.Default.GetString(encoddedByteTo);

            byte[] encodedByteSubject = Encoding.Default.GetBytes(subject);
            subject = UTF8Encoding.Default.GetString(encodedByteSubject);

            byte[] encodedByteName = Encoding.Default.GetBytes(name);
            name = UTF8Encoding.Default.GetString(encodedByteName);

            byte[] encodedByteBody = Encoding.Default.GetBytes(body);
            body = UTF8Encoding.Default.GetString(encodedByteBody);

            byte[] encodedByteFromPass = Encoding.Default.GetBytes(fromPass);
            fromPass = UTF8Encoding.Default.GetString(encodedByteFromPass);

            MailMessage message = new MailMessage();

            message.From = new MailAddress(from);
            message.To.Add(new MailAddress(to));
            message.Subject = subject;
            message.Body = body;

            SmtpClient client = new SmtpClient();
            client.Credentials = new NetworkCredential(from, fromPass);
            client.EnableSsl = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            client.Port = 587;
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true;

            try
            {
                client.Send(message);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show
                            (ex.Message, "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                return false;
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍白女子 2025-02-14 13:15:24

您需要创建一个具有16个字符的应用程序密码,该密码如下所述:

然后在这里使用它:

client.Credentials = new NetworkCredential(from, YourApplicationPassword);

我解决了yandex的同样问题。转到您的yandex邮件 - >所有设置 - >安全设置。然后单击应用程序密码链接。单击创建按钮并使用该密码。

如果您不必使用Gmail,则可以使用Yandex帐户尝试。因为众所周知,Gmail有一些新的安全限制。

You need to create an Application Password which has 16 characters as described here:

https://support.google.com/mail/answer/185833?hl=en-GB

Then use it here:

client.Credentials = new NetworkCredential(from, YourApplicationPassword);

I solved the same problem with Yandex. Go to your Yandex Mail -> All Settings -> Security Settings. Then click App Passwords link. Click create button and use that password.

If you don't have to use Gmail you can try that with a Yandex account. Because as I know Gmail has some new security limitations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文