错误,使用smtp发送gmail [英] error, send gmail with smtp

查看:151
本文介绍了错误,使用smtp发送gmail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码用于发送gmail
:

i have this code for send gmail
:

public int SendUserMail(string fromad, string toad, string body, string header, string subjectcontent)
        {
            int result = 0;
            MailMessage usermail = Mailbodplain(fromad, toad, body, header, subjectcontent);
            SmtpClient client = new SmtpClient();
            //Add the Creddentials- use your own email id and password
            client.Credentials = new System.Net.NetworkCredential("mailFrom@gmail.com ", "PWD");

            client.Host = "smtp.gmail.com";
            client.Port = 587;
            client.EnableSsl = true;
            try
            {
                client.Send(usermail);
                result = 1;
            }
            catch (Exception ex)
            {
                result = 0;
            } // end try 

            return result;

        }









public MailMessage Mailbodplain(string fromad, string toad, string body, string header, string subjectcontent)
       {
           System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
           try
           {
               string from = fromad;
               string to = toad;
               mail.To.Add(to);
               mail.From = new MailAddress(from, header, System.Text.Encoding.UTF8);
               mail.Subject = subjectcontent;
               mail.SubjectEncoding = System.Text.Encoding.UTF8;
               mail.Body = body;
               //Attachment att = new Attachment("G:\\code.txt");
               //newmsg.Attachments.Add(att);
               mail.BodyEncoding = System.Text.Encoding.UTF8;
               mail.IsBodyHtml = true;
               mail.Priority = MailPriority.High;
           }
           catch (Exception ex)
           {
               throw;
           }
           return mail;
       }



但运行后,我出现此错误:
smtpexeception被抓到

SMTP服务器需要安全连接,或者客户端未通过身份验证.服务器响应为:5.5.1需要身份验证.在
了解更多信息 请帮助我



but after run,i have this error :
smtpexeception was caught

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
please help me

推荐答案

只是一个引起我注意的问题:您残酷地滥用异常.返回值是被异常取代的技术.您只是不需要此int返回结果.您只需要让异常在堆栈中传播即可.它们是专门为传播而设计的,而不是为了在各处捕获它们而设计的.您实际上禁用了异常机制.

基本上,您应该只在每个线程的堆栈顶部捕获异常(ASP.NET中通常不使用线程,因此只有一个).在某些更特殊的情况下,除了此规则的某些例外.

另请参阅我过去的答案:
当我运行应用程序时,例外是捕获了如何处理此问题? [扔. .then ... rethrowing [ ^ ],
在类库(dll)中处理异常 [ ^ ].

—SA
Just one problem which caught my eye: you brutally abuse exception. Return value is the technique superseded by exceptions. You just don''t need this int return result. You just need to let exception propagate up the stack. They are designed exactly for propagation, not for catching them here and there. You practically disable exception mechanism.

Basically, you should catch exceptions only on the very top of stack in each thread (threads are usually not used in ASP.NET, so you have just one). With some exceptions from this rule in some more special cases.

Please also see my past answers:
When i run an application an exception is caught how to handle this?[^],
throw . .then ... rethrowing[^],
Handling exceptions in class library (dll)[^].

—SA


这是您第五次发布此问题,但您没有做任何事情来调查建议给您的问题.请检查您的凭据对您尝试使用的SMTP服务器有效,而不是仅重新发布相同的问题.如果其他所有方法均失败,则请与服务器所有者联系并寻求帮助.
This is the 5th time you have posted this question and you have not done anything to investigate the issues suggested to you. Please check your credentials are valid for the SMTP server you are trying to use, rather than just reposting this same question. If all else fails then contact the server''s owners and ask them for help.


请参考以下网址,以使用gmail发送电子邮件.

使用Google Apps发送邮件 [
Please refer following url to send email using gmail.

Send mail using Google Apps[^]

Hoe this may help you...


这篇关于错误,使用smtp发送gmail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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