Zancrew
Zancrew

Reputation: 57

Why do I get an error by trying to send a mail with c#?

I'm trying to send an e-mail using C#, but I get this recurring error :

Blockquote .

Can you explain me what's wrong with my code ?

Here it is :

SmtpClient client = new SmtpClient("smtp.gmail.com");  


client.Port = 587;
client.EnableSsl = true;
client.Timeout = 100000;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("[email protected]", "myPassword");  


MailMessage msg = new MailMessage();
msg.To.Add("[email protected]");
msg.From = new MailAddress("[email protected]");
msg.Subject = "My subject";
msg.Body = "My body";  

client.Send(msg);  

MessageBox.Show("Message sent !");

Upvotes: 1

Views: 255

Answers (3)

Md Farid Uddin Kiron
Md Farid Uddin Kiron

Reputation: 22419

I have encountered same before.

You are getting this error because you haven't set ON on Less secure app access for [email protected] as you are using Gmail SMTP port.

Reason:

Your email has no remotely access permission. You have to configure it. Suppose you want to sent email from [email protected] so you have set that permission NO to this account.

How To Set:

You could try like below

enter image description here

Or can open that tab from this link directly Less secure app access

Update:

As per your comment this is for you which has working perfectly since the beginning of my career

public object SendMail(string fromEmail, string toEmail, string mailSubject, string mailBody, string senderName, string senderPass, string attacmmentLocationPath)
        {
            try
            {
                MailMessage mail = new MailMessage();
                //Must be change before using 
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress(fromEmail);
                mail.To.Add(toEmail);
                mail.Subject = mailSubject;
                mail.Body = mailBody;
                mail.IsBodyHtml = true;
               // mail.Attachments.Add(new Attachment(@attacmmentLocationPath));

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential(senderName, senderPass);
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);

                return true;
            }
            catch (Exception ex)
            {

                return ex;
            }
        }

Hope that would help.

Upvotes: 3

user6215098
user6215098

Reputation:

An SMTP client may log in using an authentication mechanism chosen among those supported by the SMTP servers.

Upvotes: 0

E.J. Brennan
E.J. Brennan

Reputation: 46839

This code works for gmail, it is very similar to yours with slight differences, but if you try this, and it doesn't work for you, the issue is not the code - perhaps some other network related issue that you are going to need to fix first:

            using (var msg = new MailMessage())
            {
                msg.From = new MailAddress("[email protected]");
                msg.To.Add("[email protected]");
                msg.Subject = subject;
                msg.Body = body;
                msg.IsBodyHtml = true;

                using (var smtp = new SmtpClient())
                {
                    smtp.Host = "smtp.gmail.com";
                    smtp.Port = 587;
                    smtp.EnableSsl = true;
                    smtp.Credentials = new NetworkCredential("[email protected]","password");

                    smtp.Send(msg);
                }

            }

Upvotes: 1

Related Questions