本文介绍了为什么通过尝试使用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 !");

推荐答案

我以前也遇到过.

原因:

如何设置:

您可以尝试以下操作

更新:

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

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#发送邮件会出现错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 23:20
查看更多