Send email via gmail through C# application

Viewed 883

I have a C# application where I am sending an email. The function works fine when I use my personal email to send an email. However, I have created a gmail account to use for this for a company to send emails but it does not work with that email. On the gmail account I created I have set the Access to Less Secure apps On. That is the same thing I did to get it to work with my personal email. However, it just does not work with new gmail account created. Any ideas?

Here is the code

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);
    }
}
2 Answers

A better option would be to use an apps password You shouldnt need to use less secure apps

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

Ok my bad I am tired. It is working it is just sending them all to the spam folder.

Related