代码之家  ›  专栏  ›  技术社区  ›  Eric Z Beard

如何使用System.Net.MAIL设置来自的SMTP信封邮件?

  •  12
  • Eric Z Beard  · 技术社区  · 16 年前

    使用C#和System.Net.Mail命名空间发送电子邮件时,可以在MailMessage对象上设置“发件人”和“发件人”属性,但这两个属性都不允许您使进入数据节的邮件发件人和发件人地址彼此不同。MAIL FROM设置为“FROM”属性值,如果设置为“Sender”,则仅在数据节中添加另一个头字段。这将导致“代表A@B.COM从X@Y.COM发送”,这不是您想要的。我遗漏了什么吗?

    用例控制代表其他人发送的时事通讯等的NDR目的地。

    我正在使用 aspNetEmail 而不是System.Net.Mail,因为它允许我正确地执行此操作(与大多数其他SMTP库一样)。对于aspNetEmail,这是使用EmailMessage.ReversePath属性完成的。

    4 回复  |  直到 16 年前
        1
  •  7
  •   bzlm    16 年前

    MailMessage.Sender 将始终插入 Sender 标题(解释为 代表 在您的电子邮件客户端中)。

    如果你使用 Network 上的传递方法 SmtpClient , .Sender 也将更改信封中的发件人。使用 PickupDirectoryFromIis 传递方法将由IIS确定信封发件人,而IIS将使用 From 地址,不是 发件人 地址。

    There's a similar question on MSDN here.

        2
  •  4
  •   Romhein    16 年前

    我刚发现怎么做:

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

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

        3
  •  2
  •   Corben Leek    8 年前

    如果添加以下行,则返回路径和回复邮件头将在邮件头中设置。

    Dim strReplyTo As String = "email@domain.tld"
    message.ReplyToList.Add(strReplyTo)
    message.Headers.Add("Return-Path", strReplyTo)
    

    如果单击“回复”,则将电子邮件设置为回复地址

        4
  •  1
  •   George Mauer    16 年前

    你是说这个吗?:

    //create the mail message
     MailMessage mail = new MailMessage();
    
     //set the addresses
     mail.From = new MailAddress("me@mycompany.com");
     mail.To.Add("you@yourcompany.com");
    
     //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