• <small id='QjD8D'></small><noframes id='QjD8D'>

    <legend id='QjD8D'><style id='QjD8D'><dir id='QjD8D'><q id='QjD8D'></q></dir></style></legend>

        <tfoot id='QjD8D'></tfoot>

        • <bdo id='QjD8D'></bdo><ul id='QjD8D'></ul>

        <i id='QjD8D'><tr id='QjD8D'><dt id='QjD8D'><q id='QjD8D'><span id='QjD8D'><b id='QjD8D'><form id='QjD8D'><ins id='QjD8D'></ins><ul id='QjD8D'></ul><sub id='QjD8D'></sub></form><legend id='QjD8D'></legend><bdo id='QjD8D'><pre id='QjD8D'><center id='QjD8D'></center></pre></bdo></b><th id='QjD8D'></th></span></q></dt></tr></i><div id='QjD8D'><tfoot id='QjD8D'></tfoot><dl id='QjD8D'><fieldset id='QjD8D'></fieldset></dl></div>

        如何通过 Gmail 使用 C# 发送电子邮件

        时间:2023-10-06
          • <bdo id='3rLva'></bdo><ul id='3rLva'></ul>
            <i id='3rLva'><tr id='3rLva'><dt id='3rLva'><q id='3rLva'><span id='3rLva'><b id='3rLva'><form id='3rLva'><ins id='3rLva'></ins><ul id='3rLva'></ul><sub id='3rLva'></sub></form><legend id='3rLva'></legend><bdo id='3rLva'><pre id='3rLva'><center id='3rLva'></center></pre></bdo></b><th id='3rLva'></th></span></q></dt></tr></i><div id='3rLva'><tfoot id='3rLva'></tfoot><dl id='3rLva'><fieldset id='3rLva'></fieldset></dl></div>
              <legend id='3rLva'><style id='3rLva'><dir id='3rLva'><q id='3rLva'></q></dir></style></legend>

            • <small id='3rLva'></small><noframes id='3rLva'>

                <tfoot id='3rLva'></tfoot>
                    <tbody id='3rLva'></tbody>

                  本文介绍了如何通过 Gmail 使用 C# 发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  尝试通过我的网络服务发送电子邮件时出现错误.我尝试启用对安全性较低的应用程序的访问,禁用两步验证并通过网络浏览器登录帐户.SO上的任何解决方案都不适合我.我仍然得到:

                  I am getting an error when trying to send an e-mail through my web service. I have tried enabling access to less secure apps disabling 2-step verification and logging into the account via a web browser. None of the solutions on SO have worked for me. I am still getting:

                  错误:System.Net.Mail.SmtpException:SMTP 服务器需要安全连接或客户端未通过身份验证.服务器响应是:5.5.1 需要身份验证.

                  Error: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

                  我可以做些什么来解决这个问题?

                  What can I do to fix this issue?

                  namespace EmailService
                  {
                      public class Service1 : IService1
                      {    
                          public string SendEmail(string inputEmail, string subject, string body)
                          {
                              string returnString = "";
                              try
                              {
                                  MailMessage email = new MailMessage();
                                  SmtpClient smtp = new SmtpClient();
                                  smtp.Host = "smtp.gmail.com";
                  
                                  // set up the Gmail server
                                  smtp.EnableSsl = true;
                                  smtp.Port = 587;
                                  smtp.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword");
                                  smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                                  smtp.UseDefaultCredentials = false;
                  
                                  // draft the email
                                  MailAddress fromAddress = new MailAddress("cse445emailservice@gmail.com");
                                  email.From = fromAddress;
                                  email.To.Add(inputEmail);
                                  email.Subject = body;
                                  email.Body = body;
                  
                                  smtp.Send(email);
                  
                                  returnString = "Success! Please check your e-mail.";
                              }
                              catch(Exception ex)
                              {
                                  returnString = "Error: " + ex.ToString();
                              }
                              return returnString;
                          }
                      }
                  }
                  

                  推荐答案

                  去这里:不太安全的应用程序, 使用您的电子邮件和密码登录,用于在您的 c# 代码中发送邮件,然后选择 Turn On.

                  Just Go here : Less secure apps , Log on using your Email and Password which use for sending mail in your c# code , and choose Turn On.

                  另外,请转到此链接并点击继续 允许访问您的 Google 帐户

                  Also please go to this link and click on Continue Allow access to your Google account

                  我也稍微修改了一下:

                  public string sendit(string ReciverMail)
                  {
                      MailMessage msg = new MailMessage();
                  
                      msg.From = new MailAddress("YourMail@gmail.com");
                      msg.To.Add(ReciverMail);
                      msg.Subject = "Hello world! " + DateTime.Now.ToString();
                      msg.Body = "hi to you ... :)";
                      SmtpClient client = new SmtpClient();
                      client.UseDefaultCredentials = true;
                      client.Host = "smtp.gmail.com";
                      client.Port = 587;
                      client.EnableSsl = true;
                      client.DeliveryMethod = SmtpDeliveryMethod.Network;
                      client.Credentials = new NetworkCredential("YourMail@gmail.com", "YourPassword");
                      client.Timeout = 20000;
                      try
                      {
                         client.Send(msg);
                          return "Mail has been successfully sent!";
                      }
                      catch (Exception ex)
                      {
                          return "Fail Has error" + ex.Message;
                      }
                      finally
                      {
                         msg.Dispose();
                      }
                  }
                  

                  如果上面的代码不起作用,请尝试像下面的代码一样更改它:

                  If the above code don't work , try to change it like the following code :

                      SmtpClient client = new SmtpClient();
                      client.Host = "smtp.gmail.com";
                      client.Port = 587;
                      client.EnableSsl = true;
                      client.DeliveryMethod = SmtpDeliveryMethod.Network;
                      client.UseDefaultCredentials = false;
                      client.Credentials = new NetworkCredential("YourMail@gmail.com", "YourPassword");
                  

                  这篇关于如何通过 Gmail 使用 C# 发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 web.config 中设置多个 SMTP 设置? 下一篇:在不安装 SMTP 服务器的情况下发送邮件

                  相关文章

                  <legend id='4IyMr'><style id='4IyMr'><dir id='4IyMr'><q id='4IyMr'></q></dir></style></legend>
                    <bdo id='4IyMr'></bdo><ul id='4IyMr'></ul>

                  1. <small id='4IyMr'></small><noframes id='4IyMr'>

                  2. <tfoot id='4IyMr'></tfoot>
                    <i id='4IyMr'><tr id='4IyMr'><dt id='4IyMr'><q id='4IyMr'><span id='4IyMr'><b id='4IyMr'><form id='4IyMr'><ins id='4IyMr'></ins><ul id='4IyMr'></ul><sub id='4IyMr'></sub></form><legend id='4IyMr'></legend><bdo id='4IyMr'><pre id='4IyMr'><center id='4IyMr'></center></pre></bdo></b><th id='4IyMr'></th></span></q></dt></tr></i><div id='4IyMr'><tfoot id='4IyMr'></tfoot><dl id='4IyMr'><fieldset id='4IyMr'></fieldset></dl></div>