从 <> 空发件人地址发送电子邮件

Send email from the <> null sender address

Send-MailMessage
 -From "from@example.com"
 -To "to@example.com"
 -Subject "Subject" `
 -SmtpServer "localhost" -Body "Test Message"

我正在使用 Send-MailMessage 发送电子邮件,相当于使用 System.Net.Mail.MailMessage.

的 powershell

有什么方法可以从空发件人地址发送电子邮件吗?如果没有,我可以使用任何 powershell/.NET 替代品。

发件人是必填项,不能省略。 null、"" 等是不可接受的,因为 Send-MailMessage 验证发件人是电子邮件地址。


<> Null sender address: "The sender address is the email address that will receive email about delivery problems (mailing lists change this but not the From: email header so that they, and not the people sending to them, get messages about delivery problems). A special null sender address (MAIL FROM:<>) is used to signal that no one cares and no bounce notifications should be sent. Null senders are used when sending bounce messages themselves, and sometimes at other times."

您不能使用 SmtpClient 和 MailMessage 执行此操作,因此使用此或 Send-MailMessage 是不可能的。不幸的是,这意味着您要么找到支持它的第三方库,要么自己编写代码以直接通过 SMTP 发送。

您最好使用单元测试来测试系统的这一部分。

手动使用 telnet

open localhost 25

HELO Foo
MAIL FROM: <>   <--- the null address

自动化。

实际上不是完整的工作代码,但应该给人以 GetFile 和 SmtpStream 是流的一般印象。解决方案是通过直接与 SMTP 协议交互来提供预先创建的 .eml 文件。

        GivenAClientIsConnectedToAnAgentInTheWAITINGState();

        SmtpStream.WriteAsciiString("MAIL FROM:<>{0}", Environment.NewLine);
        SmtpStream.ReceiveAsAsciiString();

        SmtpStream.WriteAsciiString("RCPT TO:<{0}>{1}",mailboxAddress, Environment.NewLine);
        SmtpStream.ReceiveAsAsciiString();

        SmtpStream.WriteAsciiString("DATA{0}", Environment.NewLine);
        SmtpStream.ReceiveAsAsciiString();
        SmtpStream.SendEmail(_fileRepository.GetFile("NullSenderMessage.eml"));
        SmtpStream.SendDataTerminator();
        SmtpStream.ReceiveAsAsciiString();
        SmtpStream.WriteAsciiString("QUIT");