简体   繁体   English

ASP.NET web 应用程序中的 SmtpClient 问题

[英]Problem with SmtpClient in ASP.NET web app

I am having an issue with SmtpClient in an ASP.NET web application.我在 ASP.NET web 应用程序中遇到 SmtpClient 问题。

I have a generic function that builds an email message and then sends it.我有一个通用的 function 构建一个 email 消息然后发送它。 The code is as follows:代码如下:

public static bool SendMessage( string fromName, string toName, string subject, string body ) {
            var smtpClient = new SmtpClient("server address here")
            {
                Port = 587,
                Credentials = new NetworkCredential("user", "pass"),
                EnableSsl = false,
            };
            var mailMessage = new MailMessage
            {
                From = new MailAddress("sender", "Testing"),
                Subject = subject,
                Body = body
            };
            mailMessage.To.Add ( new MailAddress(toName, "Valued Customer") );

            try {
                smtpClient.Send ( mailMessage );
                return true;
            }
            catch (Exception ex) {
                var error = $"ERROR :{ex.Message}";
                return false;
            }
        }

The problem is, I get the following error when I call it:问题是,当我调用它时出现以下错误:

Mailbox unavailable. The server response was: <email address being sent to> No such user here

Naturally I removed the value in the < >, but in the original error message it is the email address of the recipient.自然我删除了 < > 中的值,但在原始错误消息中它是收件人的 email 地址。 I almost think the SMTP server believes the recipient has to be a user on the system.我几乎认为 SMTP 服务器认为收件人必须是系统上的用户。

What can I try next?接下来我可以尝试什么? I even hard-coded email addresses in rather than using variables, thinking maybe there was some weird issue with that, but it didn't work.我什至硬编码 email 地址而不是使用变量,我认为这可能有一些奇怪的问题,但它没有用。

The error is telling you that the the SMTP server does not have a user with that email address (usually it has to do with security around the FROM address).该错误告诉您 SMTP 服务器没有具有该 email 地址的用户(通常与 FROM 地址周围的安全性有关)。 The SMTP server will not send email if it does not recognize the FROM address.如果 SMTP 服务器无法识别 FROM 地址,它将不会发送 email。

Solution , change your FROM .解决方法,改变你的FROM Example:例子:

var mailMessage = new MailMessage
{
   From = new MailAddress("tester", "test@adminsystem.com"),
   Subject = subject,
   Body = body
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM