问题描述
我正在开发一个第三方插件以在名为 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("[email protected]", "M-Files Notification Add-on");MailAddress adressTo = new MailAddress("[email protected]");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("[email protected]", "M-Files Notification Add-on");
MailAddress adressTo = new MailAddress("[email protected]");
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:
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;
}
}
Hope that would help.
这篇关于从 C# 程序发送邮件时访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!