我需要更改消息(smtpClient 对象)中的
"message.From.Add(New MailboxAddress(sendername, senderemail)"
已经准备好另一个功能了
"message.From.Add(New MailboxAddress(sendername, othersenderemail)"
有什么建议吗?
非常感谢
我的猜测是,您想问的是如何从
[email protected]
发送电子邮件,但发件人标题为[email protected]
(或类似的)。
您有 2 个选择:
您可以将
message.Sender
设置为您实际想要发送的地址,同时将 message.From
设置为您希望消息显示的发件人地址。这具有在消息中添加 Sender: My Name <[email protected]>
标头的副作用,该标头可能是也可能不是您关心的内容。
您可以使用
SmtpClient.Send(MimeMessage message, MailboxAddress sender, IEnumerable<MailboxAddress> recipeints, CancellationToken cancellationToken)
API,它不会更改 MimeMessage 的标头。
例如:
var message = new MimeMessage ();
message.From.Add (new MailboxAddress (string.Empty, "[email protected]"));
message.To.Add (new MailboxAddress ("Customer Name", "[email protected]"));
message.Subject = "Your order is on its way!";
message.Body = GenerateMessageBody ();
using (var client = new SmtpClient ()) {
client.Connect ("smtp.mycompany.com", 587, SecureSocketOptions.StartTls);
client.Authenticate ("[email protected]", "password");
var sender = new MailboxAddress (string.Empty, "[email protected]");
// This API is new in MimeKit v3.0, but will collect the unique email
// addresses in the To, Cc, and Bcc fields into 1 collection.
var recipients = message.GetRecipients (true);
// Send the message from your account w/o having to modify the From header.
client.Send (message, sender, recipients);
client.Disconnect (true);
}