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

两盒软妹~` 提交于 2021-01-28 18:17:55

问题


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

.

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("myEmailAdress@gmail.com", "myPassword");  


MailMessage msg = new MailMessage();
msg.To.Add("receiver@gmail.com");
msg.From = new MailAddress("sender@gmail.com");
msg.Subject = "My subject";
msg.Body = "My body";  

client.Send(msg);  

MessageBox.Show("Message sent !");

回答1:


I have encountered same before.

You are getting this error because you haven't set ON on Less secure app access for sender@gmail.com 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 sender@gmail.com so you have set that permission NO to this account.

How To Set:

You could try like below

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.




回答2:


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("fromaddress@gmail.com");
                msg.To.Add("toaddress@gmail.com");
                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("xxx@gmail.com","password");

                    smtp.Send(msg);
                }

            }



回答3:


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



来源:https://stackoverflow.com/questions/60523276/why-do-i-get-an-error-by-trying-to-send-a-mail-with-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!