帮助无法发送电子邮件的代码 [英] Help with code that fails to send email

查看:115
本文介绍了帮助无法发送电子邮件的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码无法发送邮件

            email = tbFROM.Text;

            passw = tbPASSWORD.Text;

            string smtpAddress =" smtp.gmail.com" ;;

            int portNumber = 587;

            bool enableSSL = true;

            string emailFromAddress = email; //发件人电子邮件地址  

            string password = passw; //发件人密码  

            string emailToAddress = email; //收件人电子邮件地址  

            string subject =" Hello";

            string body ="你好,这是使用gmail的电子邮件发送测试。" ;;

           使用(MailMessage mail = new MailMessage())

            {

                mail.From = new MailAddress(emailFromAddress);

                mail.To.Add(emailToAddress);

                mail.Subject = subject;

                mail.Body = body;

                mail.IsBodyHtml = true;

                //mail.Attachments.Add(new Attachment(" D:\\TestFile.txt")); // - 取消注释以发送任何附件  

               使用(SmtpClient smtp = new SmtpClient(smtpAddress,portNumber))

                {

                    smtp.Credentials = new NetworkCredential(emailFromAddress,password);

                    smtp.EnableSsl = enableSSL;

                    smtp.Send(mail);

                }¥b $ b            }

            email = tbFROM.Text;
            passw = tbPASSWORD.Text;
            string smtpAddress = "smtp.gmail.com";
            int portNumber = 587;
            bool enableSSL = true;
            string emailFromAddress = email; //Sender Email Address  
            string password = passw; //Sender Password  
            string emailToAddress = email; //Receiver Email Address  
            string subject = "Hello";
            string body = "Hello, This is Email sending test using gmail.";
            using (MailMessage mail = new MailMessage())
            {
                mail.From = new MailAddress(emailFromAddress);
                mail.To.Add(emailToAddress);
                mail.Subject = subject;
                mail.Body = body;
                mail.IsBodyHtml = true;
                //mail.Attachments.Add(new Attachment("D:\\TestFile.txt"));//--Uncomment this to send any attachment  
                using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
                {
                    smtp.Credentials = new NetworkCredential(emailFromAddress, password);
                    smtp.EnableSsl = enableSSL;
                    smtp.Send(mail);
                }
            }

以下一个也无法发送电子邮件

one below also fails to send email

            email = tbFROM.Text;

            passw = tbPASSWORD.Text;

            // MailMessage mail = new MailMessage(tbFROM.Text,tbTO.Text,tbSUBJECT.Text,rtbBODY.Text);

            MailMessage mail = new MailMessage(电子邮件,电子邮件,电子邮件,电子邮件);

            SmtpClient smtpClient =新的SmtpClient("smtp.gmail.com");

            smtpClient.Port = 587;

            //smtpClient.Credentials = new NetworkCredential(tbUSERNAME.Text,tbPASSWORD.Text);

            smtpClient.Credentials = new NetworkCredential(email,passw);

            smtpClient.EnableSsl = true;

            smtpClient.Send(mail);

            email = tbFROM.Text;
            passw = tbPASSWORD.Text;
            //MailMessage mail = new MailMessage(tbFROM.Text,tbTO.Text,tbSUBJECT.Text,rtbBODY.Text);
            MailMessage mail = new MailMessage(email,email,email,email);
            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
            smtpClient.Port = 587;
            //smtpClient.Credentials = new NetworkCredential(tbUSERNAME.Text,tbPASSWORD.Text);
            smtpClient.Credentials = new NetworkCredential(email,passw);
            smtpClient.EnableSsl = true;
            smtpClient.Send(mail);

google无法回复...如何发送电子邮件?

推荐答案

您应该做的第一件事是提供在发送电子邮件失败时收到的错误消息。如果未显示错误消息(应该是),请订阅

SendCompleted事件
并检查e.Error。

The first thing you should do is provide the error message received on failure to send an email. If an error message is not presented (which it should be) subscribe to SendCompleted event and inspect e.Error.

登录您的Google电子邮件帐户
关注页面
,如果"安全性较低的应用访问权限"关闭,尝试将此设置设置为开,然后重新尝试发送电子邮件。

Login to your Google email account to the follow page, if "Less secure app access" is off, try setting this setting to on then reattempt to send an email.

另请参阅"允许安全性较低的应用程序有什么危险"。

See also, "what are the dangers of allowing less secure apps".

https://security.stackexchange.com/questions/66025/what-are-the-dangers-of-allowing-less-secure-apps-to-access-my-google-account


这篇关于帮助无法发送电子邮件的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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