如何禁用中继并将我的代码用作经过身份验证的客户端?

发布于 2025-01-03 12:44:12 字数 1612 浏览 0 评论 0原文

所以有很多关于我遇到的错误的帖子。但大多数都与 IIS 打交道。

邮箱不可用。服务器响应为:5.7.1 无法中继

我已将代码移植到控制台,看看我是否仍然收到此错误,我确实收到了。即使这是一个控制台应用程序,它似乎仍然使用 ISS 作为中继或其他东西?

我已经与 Exchange 管理员交谈过,他不会打开邮件服务器进行任何中继,也不会允许我的服务器这样做。所以我想让我的 C# 代码作为常规的经过身份验证的客户端发送电子邮件,就像 Outlook 一样。

如果我将其发送给我自己域内的某人,则下面的代码有效,但如果收件人位于域外,则无效。

如何禁用中继并使用我的代码作为经过身份验证的客户端,以便我可以向域外的人发送电子邮件?

代码:

static void Main(string[] args)
{
    Console.WriteLine("Sending Mail...");
    try
    {
        SmtpClient _SMTPServer = new SmtpClient("companymailserver", 587);
        _SMTPServer.Credentials = new System.Net.NetworkCredential("myusername", "mypassword");

        MailMessage _mailMessage = new MailMessage();
        _mailMessage.To.Add(new MailAddress("[email protected]"));
        _mailMessage.From = new MailAddress("[email protected]");
        _mailMessage.IsBodyHtml = false;
        _mailMessage.Subject = "This is a test";
        _mailMessage.Body = "This is the body";

        _SMTPServer.Send(_mailMessage);
    }
    catch (Exception err)
    {
        Console.WriteLine("error: " + err.Message);
    }
    Console.WriteLine("Press any key to continue...");
    Console.Read();
} 

更新 #1

奇怪的是,无论如何我输入了什么凭据,却收到相同的“无法中继”错误。好像它默认中继并忽略我的 SmtpClient 设置。

So there are many posts on this error i'm getting. But most deal with IIS.

Mailbox unavailable. The server response was: 5.7.1 Unable to relay

I have ported my code to console to see if i still get this error, and i do. It would seem as though it's still using ISS as a relay or something even though this is a console application?

I have spoken to the Exchange Admin and he is not going to open up the mail server for any relaying or allow my server to do so. So i would like to make my C# code send out the email as a regular authenticated client as if it were outlook.

My code below works if i'm sending it to someone within my own domain but not if the recipient is outside the domain.

How can i disable the relaying and use my code as an authenticated client so i can send emails to people outside my domain?

CODE:

static void Main(string[] args)
{
    Console.WriteLine("Sending Mail...");
    try
    {
        SmtpClient _SMTPServer = new SmtpClient("companymailserver", 587);
        _SMTPServer.Credentials = new System.Net.NetworkCredential("myusername", "mypassword");

        MailMessage _mailMessage = new MailMessage();
        _mailMessage.To.Add(new MailAddress("[email protected]"));
        _mailMessage.From = new MailAddress("[email protected]");
        _mailMessage.IsBodyHtml = false;
        _mailMessage.Subject = "This is a test";
        _mailMessage.Body = "This is the body";

        _SMTPServer.Send(_mailMessage);
    }
    catch (Exception err)
    {
        Console.WriteLine("error: " + err.Message);
    }
    Console.WriteLine("Press any key to continue...");
    Console.Read();
} 

UPDATE #1

The strange thing is no matter what credentials i put i get the same 'Unable to relay' error. As if it's defaulting to relay and ignoring my SmtpClient settings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笔落惊风雨 2025-01-10 12:44:12

我猜身份验证失败了。尝试包含域名。

_SMTPServer.Credentials = 
    new System.Net.NetworkCredential("myusername", "mypassword", "mydomain");

另外,请务必将 UseDefaultCredentials 设置为 false

_SMTPServer.UseDefaultCredentials = false;

I'm guessing that the authentication failed. Try including the domain name.

_SMTPServer.Credentials = 
    new System.Net.NetworkCredential("myusername", "mypassword", "mydomain");

Also, be sure to set UseDefaultCredentials to false

_SMTPServer.UseDefaultCredentials = false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文