使用c#发送电子邮件

问题描述 投票:0回答:1

我正在尝试使用C#发送电子邮件,但出现以下错误。

邮箱不可用。服务器响应为:中继访问被拒绝。请验证。

我不确定为什么会收到此错误。在这里,我正在使用smtp2go第三方发送此电子邮件。

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("mail.smtp2go.com");

mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test Email";
mail.Body = "Report";

//Attachment attachment = new Attachment(filename);
//mail.Attachments.Add(attachment);

SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("me", "password");
//SmtpServer.EnableSsl = true;

SmtpServer.Send(mail);
c# smtpclient
1个回答
2
投票

我已经尝试过您的代码,并且可以正常工作。但是以我为例,要使用smtp服务器,发件人(发件人的电子邮件地址)必须使用相同的域进行身份验证。(但gmail可以用于此Send emails from a different address or alias

因此,如果您的SMTP服务器连接到smtp2go.com,请尝试以下操作。

SmtpClient SmtpServer = new SmtpClient("mail.smtp2go.com");
mail.From = new MailAddress("[email protected]");

或者,如果您需要使用smtp2go的服务,最好使用rest API


这里在使用gmail时通过您的评论进行更新。

需要Gmail secure app access。这就是为什么代码不起作用的原因。因此,有两个选择。

1。更新您的Gmail帐户安全性

(从这里开始的想法:C# - 이메일 발송방법

转到hereturn on“缺少安全的应用程序访问权限”。完成此操作后,您的代码将正常工作。(有效)

2。使用“ .NET的Google API客户端库。”

我认为这不是那么容易,请检查一下,我找到了与此here相关的答案

© www.soinside.com 2019 - 2024. All rights reserved.