从C#程序发送邮件时访问被拒绝 [英] Access denied when sending a mail from C# programm

查看:147
本文介绍了从C#程序发送邮件时访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个第三方加载项,以在名为M-Files的程序中运行.附件的目的是在SMTP服务器的帮助下发送邮件.我在DevelMail.com中创建了一个伪造的SMTP服务器,仅用于测试.从浏览器测试SMTP服务器是可以的,但是当我运行代码时,它给了我以下错误.

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


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

以下是代码:

  MailAddress adressFrom = new MailAddress("notification@mfiles.no","M-Files Notification Add-on");MailAddress adressTo = new MailAddress("majdnakhleh@live.no");MailMessage消息=新的MailMessage(adressFrom,adressTo);message.Subject =正在运行M文件附件";字符串htmlString = @< html>< body>< p>尊敬的客户</p>< p>这是通过使用元数据属性中编写的mailadress发送给您的通知!< p>顺祝商祺!-M-Files/br</p></body></html>;message.Body = htmlString;SmtpClient客户端=新的SmtpClient();client.Host ="smtp.develmail.com";client.Port = 587;client.Credentials =新的System.Net.NetworkCredential("myUserName","myPassword");client.EnableSsl = true;client.Send(message); 

解决方案

问题原因:

通常,使用 SMTP 的电子邮件发送选项遇到拒绝访问因为应该有一个发件人电子邮件,该电子邮件必须允许远程访问.从发件人电子邮件发送 SMTP 请求时它检查是否允许远程访问.如果没有,那你总是收到拒绝访问消息.

解决方案:

例如,假设您要使用 Gmail SMTP 发送电子邮件,在这种情况下,您必须启用允许安全性较低的应用:打开

如何设置

您只需浏览此链接

代码段:

 公共静态对象SendMail(字符串fromEmail,字符串toEmail,字符串mailSubject,字符串mailBody,字符串senderName,字符串senderPass,字符串attacmmentLocationPath){尝试{MailMessage邮件= new MailMessage();//使用gmail smtp之前必须进行更改SmtpClient SmtpServer =新的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 =新的System.Net.NetworkCredential(senderName,senderPass);//输入您先前配置的credentailsSmtpServer.EnableSsl = true;SmtpServer.Send(邮件);返回true;}抓住(前例外){退货}} 

注意: 请确保 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#程序发送邮件时访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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