<tfoot id='jXAL1'></tfoot>
  • <small id='jXAL1'></small><noframes id='jXAL1'>

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

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

        从 C# 程序发送邮件时访问被拒绝

        时间:2023-10-05
            <tbody id='IZrFM'></tbody>
          <i id='IZrFM'><tr id='IZrFM'><dt id='IZrFM'><q id='IZrFM'><span id='IZrFM'><b id='IZrFM'><form id='IZrFM'><ins id='IZrFM'></ins><ul id='IZrFM'></ul><sub id='IZrFM'></sub></form><legend id='IZrFM'></legend><bdo id='IZrFM'><pre id='IZrFM'><center id='IZrFM'></center></pre></bdo></b><th id='IZrFM'></th></span></q></dt></tr></i><div id='IZrFM'><tfoot id='IZrFM'></tfoot><dl id='IZrFM'><fieldset id='IZrFM'></fieldset></dl></div>

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

            • <legend id='IZrFM'><style id='IZrFM'><dir id='IZrFM'><q id='IZrFM'></q></dir></style></legend>
                <tfoot id='IZrFM'></tfoot>
                  <bdo id='IZrFM'></bdo><ul id='IZrFM'></ul>
                  本文介绍了从 C# 程序发送邮件时访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在开发一个第三方插件以在名为 M-Files 的程序中运行.插件的目的是在 SMTP 服务器的帮助下发送邮件.我在 DevelMail.com 中创建了一个假的 SMTP 服务器,只是为了测试.从浏览器测试 SMTP 服务器有效,但是当我运行代码时,它给了我以下错误.

                  交易失败.服务器响应为:5.7.1 客户端主机被拒绝:访问被拒绝


                  以下是 SMTP 信息:
                  主机: smtp.develmail.com
                  SMTP 端口: 25
                  TLS/SSL 端口:465
                  STARTTLS 端口: 587
                  身份验证类型: LOGIN、CRAM-MD5

                  代码如下:

                  MailAddress adressFrom = new MailAddress("notification@mfiles.no", "M-Files Notification Add-on");MailAddress adressTo = new MailAddress("majdnakhleh@live.no");MailMessage message = new MailMessage(adressFrom, adressTo);message.Subject = "M-Files 插件正在运行";字符串 htmlString = @"<html><身体><p>尊敬的客户</p><p>这是使用写入元数据属性中的邮件地址发送给您的通知!.</p><p>此致,<br>-M-Files</br></p></身体></html>";message.Body = htmlString;SmtpClient 客户端 = 新 SmtpClient();client.Host = "smtp.develmail.com";客户端.端口 = 587;client.Credentials = new System.Net.NetworkCredential("myUserName", "myPassword");客户端.EnableSsl = true;客户端.发送(消息);

                  解决方案

                  问题原因:

                  <块引用>

                  通常,使用 SMTP 的电子邮件发送选项遇到 Access denied因为应该有一个 sender email 需要允许远程访问.当 SMTP 请求从发件人电子邮件发送它检查是否允许远程访问.如果没有,那么你总是收到 Access denied 消息.

                  解决方案:

                  例如,假设您想使用 Gmail SMTP 发送电子邮件,在这种情况下您必须启用 Allow less secure apps: ON

                  如何设置

                  您可以简单地浏览此链接

                  代码片段:

                   public static object SendMail(string fromEmail, string toEmail, string mailSubject, string mailBody, string senderName, string senderPass, string attacmmentLocationPath){尝试{MailMessage 邮件 = 新的 MailMessage();//在使用非gmail smtp之前必须更改SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");mail.From = new MailAddress(fromEmail);mail.To.Add(toEmail);mail.Subject = 邮件主题;邮件.正文 = 邮件正文;mail.IsBodyHtml = true;SmtpServer.Port = 587;SmtpServer.Credentials = new System.Net.NetworkCredential(senderName, senderPass);//输入你之前配置的credentailsSmtpServer.EnableSsl = true;SmtpServer.Send(邮件);返回真;}捕捉(例外前){返回前;}}

                  <块引用>

                  注意: 确保 fromEmail(senderName, senderPass) 应该是与凭证相同的电子邮件.

                  希望这会有所帮助.

                  I'm developing a third-party add-on to run in a program called M-Files. The purpose of the add-on is to send a mail with the help of an SMTP server. I created a fake SMTP server in DevelMail.com just for testing. Testing the SMTP server from a browser works but when i run the code it gives me the following error.

                  Transaction failed. The server response was: 5.7.1 Client host rejected: Access denied


                  Here are the SMTP information:
                  Host: smtp.develmail.com
                  SMTP Port: 25
                  TLS/SSL Port: 465
                  STARTTLS Port : 587
                  Auth types: LOGIN, CRAM-MD5

                  Here is the code:

                  MailAddress adressFrom = new MailAddress("notification@mfiles.no", "M-Files Notification Add-on");
                  MailAddress adressTo = new MailAddress("majdnakhleh@live.no");
                  MailMessage message = new MailMessage(adressFrom, adressTo);
                  
                  message.Subject = "M-Files Add-on running";
                  string htmlString = @"<html>
                                      <body>
                                      <p> Dear customer</p>
                                      <p> This is a notification sent to you by using a mailadress written in a metadata property!.</p>
                                      <p> Sincerely,<br>- M-Files</br></p>
                                      </body>
                                      </html>
                                      ";
                  message.Body = htmlString;
                  
                  SmtpClient client = new SmtpClient();
                  client.Host = "smtp.develmail.com";
                  client.Port = 587;
                  client.Credentials = new System.Net.NetworkCredential("myUserName", "myPassword");
                  client.EnableSsl = true;
                  client.Send(message);
                  

                  解决方案

                  Reason for the Issue:

                  Usually, email sending option using SMTP encountered Access denied because there should have a sender email which required to allow remote access. When SMTP request sent from the sender email it checks whether there is remote access allowed. If no, then you always got Access denied message.

                  Solution:

                  For example let's say, you want to send email using Gmail SMTP in that case you do have to enable Allow less secure apps: ON

                  How To Set

                  You can simply browse this link Less secure app access and turn that to ON

                  See the screen shot

                  Code Snippet:

                      public static 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 other than gmail smtp
                              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;
                  
                              SmtpServer.Port = 587;
                              SmtpServer.Credentials = new System.Net.NetworkCredential(senderName, senderPass);//Enter the credentails from you have configured earlier
                              SmtpServer.EnableSsl = true;
                  
                              SmtpServer.Send(mail);
                  
                              return true;
                          }
                          catch (Exception ex)
                          {
                  
                              return ex;
                          }
                      }
                  

                  Note: Make sure, fromEmail and (senderName, senderPass) should be same email with the credential.

                  Hope that would help.

                  这篇关于从 C# 程序发送邮件时访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:是否有用于通过代理发送电子邮件的 .NET 库? 下一篇:如何从电子邮件地址获取 SMTP 服务器

                  相关文章

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

                      <small id='eWQ6D'></small><noframes id='eWQ6D'>

                      <tfoot id='eWQ6D'></tfoot>

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