首页 > 解决方案 > 通过 C# 应用程序通过 gmail 发送电子邮件

问题描述

我有一个 C# 应用程序,我要在其中发送电子邮件。当我使用我的个人电子邮件发送电子邮件时,该功能工作正常。但是,我创建了一个 gmail 帐户,用于公司发送电子邮件,但它不适用于该电子邮件。在我创建的 gmail 帐户上,我已将访问不太安全的应用程序设置为开启。这与我为使其与我的个人电子邮件一起工作所做的相同。但是,它不适用于创建的新 gmail 帐户。有任何想法吗?

这是代码

public static void SendEmail(string strEmail,string strRandomPassword)
{
    try
    {
        MailMessage message = new MailMessage();
        SmtpClient smtp = new SmtpClient();
        message.From = new MailAddress("email@gmail.com");
        message.To.Add(new MailAddress(strEmail));
        message.Subject = "Password Reset";
        message.IsBodyHtml = true; //to make message body as html  
        message.Body = "Here is your key to reset your password. Key: " + strRandomPassword;
        smtp.Port = 587;
        smtp.Host = "smtp.gmail.com"; //for gmail host  
        smtp.EnableSsl = true;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new NetworkCredential("email@gmail.com", "Password");
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp.Send(message);
    }
    catch (Exception ex) 
    {
        MessageBox.Show(message + ex.Message, "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

标签: c#emailgmail

解决方案


更好的选择是使用应用程序密码 您不需要使用安全性较低的应用程序

class Program
    {
        private const string SmtpServer = "smtp.gmail.com";
        private const string MailserverLogin = "ddddd@gmail.com";
        private const string MailServerPassword = "wohpkockczwcjznp";
        private const string MailUserName = "Developer tips support";
        
        
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            
            // SmtpClient
            var client = new SmtpClient(SmtpServer)
            {
                Port = 587,
                Credentials = new System.Net.NetworkCredential(MailserverLogin, MailServerPassword),
                EnableSsl = true
            };
            
            // Specify the email sender.
            var from = new MailAddress(MailserverLogin, MailUserName, System.Text.Encoding.UTF8);
            
            // Set destinations for the email message.
            var to = new MailAddress("xxxxx@daimto.com");
            
            // Specify the message content.
            var message = new MailMessage(@from, to)
            {
                Body = "This is a test email message sent by an application. ",
                Subject = "Customer support Daimto." 
            };
            
            client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
            
            // The userState can be any object that allows your callback
            // method to identify this send operation.
            // For this example, the userToken is a string constant.
            var userState = "test message1";
            client.SendAsync(message, userState);

            
            while (!mailSent)
            { 
                Console.Write(".");
                Thread.Sleep(50);
            } 
            
            //clean up.
            message.Dispose();
            client.Dispose();

        }

        
        static bool mailSent = false;
        private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            // Get the unique identifier for this asynchronous operation.
            var token = (string) e.UserState;

            if (e.Cancelled)
            {
                Console.WriteLine("[{0}] Send canceled.", token);
            }

            if (e.Error != null)
            {
                Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
            }
            else
            {
                Console.WriteLine("Message sent.");
            }

            mailSent = true;
        }
    }

推荐阅读