从 C# 程序发送邮件时访问被拒绝
Access denied when sending a mail from C# programm
我正在开发 运行 的第三方插件,名为 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 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:
例如,您想要使用 Gmail SMTP
发送电子邮件,在这种情况下您必须启用 Allow less secure apps: ON
How To Set
您只需浏览此 link Less secure app access
并将其转换为 ON
查看屏幕截图
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.
希望对您有所帮助。
我正在开发 运行 的第三方插件,名为 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 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
encounteredAccess denied
because there should have asender email
which required to allow remote access. WhenSMTP
request sent from the sender email it checks whether there is remote access allowed. If no, then you always gotAccess denied
message.
Solution:
例如,您想要使用 Gmail SMTP
发送电子邮件,在这种情况下您必须启用 Allow less secure apps: ON
How To Set
您只需浏览此 link Less secure app access
并将其转换为 ON
查看屏幕截图
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.
希望对您有所帮助。