发送SMTP请求的身份验证错误

时间:2017-09-08 21:07:03

标签: c# .net email authentication smtpclient

我正在编写一个C#电子邮件应用程序,目前正在设计用于Gmail,因此我可以测试身份验证。但是我收到一个似乎没有多大意义的错误,因为我将SSL设置为true并使用用户和密码。

错误消息: System.Net.Mail.SmtpException:'SMTP服务器需要安全连接,或者客户端未经过身份验证。服务器响应为:5.5.1需要身份验证。在'

了解更多信息
using System;
using System.Net;
using System.Net.Mail;

namespace EmailSendingProgram
{
    public class EmailSendingClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Type your Gmail Address: ");
            string from = Console.ReadLine();

            Console.WriteLine("Type your Gmail Password: ");
            string password = Console.ReadLine();

            Console.WriteLine("Type the Email Address you wish to send to: ");
            string to = Console.ReadLine();

            Console.WriteLine("Type the Subject of the email: ");
            string subject = Console.ReadLine();

            Console.WriteLine("Type or paste in your email (either text or HTML): ");
            string body = Console.ReadLine();

            EmailSendingClass email = new EmailSendingClass();
            email.Send(from, password, to, subject, body);
        }

        /// handles sending the email
        /// hardcoded to work with gmail just change the CreateSmtpClient from 
        /// "smtp.gmail.com", 587 to whatever you want to use
        public void Send(string from, string password, string to, string subject, string body)
        { 
            var message = CreateMailMessage(from, to, subject, body);

            // Host and Port setup for use with Gmail
            using (SmtpClient client = CreateSmtpClient("smtp.gmail.com", 587, from, password))
            {
                client.Send(message);
            }
        }

        /// Defines the SmtpClient for the mail message 
        /// setups up the network credentials to send the email
        private SmtpClient CreateSmtpClient(string host, int port, string from, string password)
        {
            SmtpClient client = null;

            client = new SmtpClient()
            {
                Host = host,
                Port = port,
                EnableSsl = true,
                Credentials = new NetworkCredential(from, password)
            };

            return client;
        }

        /// defines what is contained in the email and where it will be sent
        /// also makes all messages send as HTML
        private MailMessage CreateMailMessage(string from, string to, string subject, string body)
        {
            MailMessage message = null;

            message = new MailMessage(from, to)
            {
                Subject = subject,
                Body = body
            };

            message.IsBodyHtml = true;
            return message;
        }
    }
}

我知道使用身份验证时出现了错误的内容,但我似乎无法弄明白具体是什么。所以任何帮助都会受到赞赏。

2 个答案:

答案 0 :(得分:0)

我遇到了由TLS 1.2支持问题引起的类似问题。如果是这种情况,最简单的解决方案可能是针对.NET 4.6.1或更高版本。其他建议可以在herehere找到。

答案 1 :(得分:0)

问题不是该计划,而是Gmail拒绝从“不太安全的应用”登录

我只需点击保护您的帐户按钮,并允许此帐户从不太安全的应用程序登录。

enter image description here