如何使用System.Net.Mail设置SMTP信封邮件?

时间:2020-03-05 18:50:02  来源:igfitidea点击:

当使用Cand System.Net.Mail命名空间发送电子邮件时,可以在MailMessage对象上设置"发件人"和"发件人"属性,但是这两个属性都不允许我们创建MAIL FROM和发往其中的发件人地址DATA部分彼此不同。 MAIL FROM设置为" From"属性值,如果设置为" Sender",则仅在DATA节中添加另一个头字段。这将导致"不是从[email protected]代表[email protected]",而是我们不需要的。我想念什么吗?

用例控制着代表其他人发送的新闻通讯等的NDR目的地。

我目前正在使用aspNetEmail而不是System.Net.Mail,因为它可以让我正确地执行此操作(就像大多数其他SMTP库一样)。对于aspNetEmail,这是通过使用EmailMessage.ReversePath属性来完成的。

解决方案

回答

你是这个意思吗?

//create the mail message
 MailMessage mail = new MailMessage();

 //set the addresses
 mail.From = new MailAddress("[email protected]");
 mail.To.Add("[email protected]");

 //set the content
 mail.Subject = "This is an email";
 mail.Body = "this is a sample body with html in it. <b>This is bold</b> <font color=#336699>This is blue</font>";
 mail.IsBodyHtml = true;

 //send the message
 SmtpClient smtp = new SmtpClient("127.0.0.1");
 smtp.Send(mail);

从http://www.systemnetmail.com/faq/3.1.2.aspx

回答

我刚刚找到了怎么做:

  • mail.From指定对最终用户可见的电子邮件
  • mail.Sender指定信封MAIL FROM

就这样(即使我花了一段时间才弄清楚)

回答

MailMessage.Sender将始终插入一个Sender标头(在电子邮件客户端中代表)。

如果我们在SmtpClient上使用Network传递方法,则.Sender也会更改信封中的发送者。使用" PickupDirectoryFromIis"传递方法会将其留给IIS确定信封发件人,并且IIS将使用"发件人"地址,而不是"发件人"地址。

在MSDN上也有类似的问题。