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

Access denied when sending a mail from C# programm(从 C# 程序发送邮件时访问被拒绝)
本文介绍了从 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# 程序发送邮件时访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)