Aleksandar Hristov
Aleksandar Hristov

Reputation: 33

Can't send Email with SMTP and gmail account on C# .NET

I'm trying to send emails for account confirmation, but I get a problem on the SMTP side. enter image description here

I saw someone had this problem before and they fixed it by enabling "Less secure apps to sign in" but google removed that as a feature. enter image description here

EmailSender.cs


public bool SendEmail(string userEmail, string confirmationLink)
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress("[email protected]");
            mailMessage.To.Add(new MailAddress(userEmail));

            mailMessage.Subject = "Confirm your email";
            mailMessage.IsBodyHtml = true;
            mailMessage.Body = confirmationLink;

            var emailPass = _config["emailPassword"];

            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential("[email protected]", emailPass);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Host = "smtp.gmail.com";
            client.UseDefaultCredentials = true;
            client.EnableSsl = true;
            client.Port = 587;
            

            try
            {
                client.Send(mailMessage);
                Console.WriteLine("Sent email!");
                return true;
            }
            catch (SmtpFailedRecipientException ex)
            {
                // log exception
            }
            return false;
        }

I tried to change UseDefaultCredentials to false and changing ports but nothing seems to be working.

Upvotes: 0

Views: 1415

Answers (2)

Ahsan Ehtesham
Ahsan Ehtesham

Reputation: 156

Using the following piece of code:

using System;
using System.Net;
using System.Net.Mail;

namespace EmailApp
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            String SendMailFrom = "Sender Email";
            String SendMailTo = "Reciever Email";
            String SendMailSubject = "Email Subject";
            String SendMailBody = "Email Body";

            try
            {
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com",587);
                SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
                MailMessage email = new MailMessage();
                // START
                email.From = new MailAddress(SendMailFrom);
                email.To.Add(SendMailTo);
                email.CC.Add(SendMailFrom);
                email.Subject = SendMailSubject;
                email.Body = SendMailBody;
                //END
                SmtpServer.Timeout = 5000;
                SmtpServer.EnableSsl = true;
                SmtpServer.UseDefaultCredentials = false;
                SmtpServer.Credentials = new NetworkCredential(SendMailFrom, "Google App Password");
                SmtpServer.Send(email);

                Console.WriteLine("Email Successfully Sent");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadKey();
            }

        }
    }
}

SmtpServer.UseDefaultCredentials is set to false, furthermore, you can read the further steps here: https://www.techaeblogs.live/2022/06/how-to-send-email-using-gmail.html

Upvotes: 0

Hanan
Hanan

Reputation: 13

Try the following:

public bool SendEmail(string userEmail, string confirmationLink)
{
    MailMessage mailMessage = new MailMessage();
    mailMessage.From = new MailAddress("[email protected]");
    mailMessage.To.Add(new MailAddress(userEmail));

    mailMessage.Subject = "Confirm your email";
    mailMessage.IsBodyHtml = true;
    mailMessage.Body = confirmationLink;

    var emailPass = _config["emailPassword"];

    SmtpClient client = new SmtpClient();
    client.Credentials = new System.Net.NetworkCredential("[email protected]", emailPass);
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.Host = "smtp.gmail.com";
    client.Port = 587;
    client.EnableSsl = true;

    try
    {
        client.Send(mailMessage);
        Console.WriteLine("Sent email!");
        return true;
    }
    catch (SmtpFailedRecipientException ex)
    {
        // log exception
    }
    return false;
}

client.UseDefaultCredentials = true; is removed as it is not necessary and might cause problems. client.EnableSsl = true; is moved to after setting the client.Port property.

Upvotes: 1

Related Questions