为什么通过尝试使用C#发送邮件会出现错误? [英] Why do I get an error by trying to send a mail with c#?

查看:57
本文介绍了为什么通过尝试使用C#发送邮件会出现错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#发送电子邮件,但出现此重复出现的错误:

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 ?

这是:

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 !");

推荐答案

我以前也遇到过.

您收到此错误的原因是,您没有按原样设置 sender@gmail.com 缺乏安全的应用访问权限上的 ON 使用Gmail SMTP 端口.

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.

原因:

您的电子邮件没有远程访问权限.您必须进行配置它.假设您要从 sender@gmail.com 发送电子邮件,因此您已为此帐户设置该权限 NO .

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.

如何设置:

您可以尝试以下操作

或者可以直接通过此链接打开该选项卡 访问应用程序较少安全

更新:

根据您的评论,这是给您的,自从我职业生涯开始以来就一直运转良好

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

希望会有所帮助.

这篇关于为什么通过尝试使用C#发送邮件会出现错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆