【问题标题】:Send email via gmail through C# application [closed]通过 C# 应用程序通过 gmail 发送电子邮件 [关闭]
【发布时间】:2021-09-20 19:06:27
【问题描述】:

我有一个要发送电子邮件的 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# email gmail


    【解决方案1】:

    更好的选择是使用apps password 你不应该需要使用不太安全的应用程序

    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;
            }
        }
    

    【讨论】:

    • 是的,我看到了一些类似的选项。我只是不知道该怎么做。
    • 如果用户安装了 2fa,apps 密码也是您唯一的选择。
    【解决方案2】:

    好吧,我很累。它正在工作,只是将它们全部发送到垃圾邮件文件夹。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-07
      • 2023-03-19
      • 2013-01-13
      • 2013-05-28
      • 2016-05-16
      • 2017-10-06
      相关资源
      最近更新 更多