如何使用system.net.mail发送非替代的多个部分

时间:2015-01-05 22:27:41

标签: c# mime multipart system.net.mail mailmessage

我有多部分/混合体的电子邮件,其中包含带有Html部分和相关图像部分的嵌套多部分/相关部分。有没有办法将这些电子邮件构建为System.Net.Mail的MailMessage实例?

这不是候补问题。

  1. 多部分/混合 1.1。多/相关 1.1.1。 text / html的 1.1.2。图像/ GIF 1.2。多/相关 1.2.1。 text / html的 1.2.2。图像/ GIF
  2. 1.1.1与1.1.2有关。但它与1.2.2无关。 1.1.1和1.2.1是单独的html主体。

    我在下面粘贴了一条示例消息。

    Message-ID: <mhtml-11@dsv.su.se>
    Date: Wed, 04 Apr 2000 04:18:00 +0200
    From: MHTML <mhtml@dsv.su.se>
    MIME-Version: 1.0
    To: mhtml@dsv.su.se
    Subject: Test message no. 11
    Content-Type: multipart/mixed; boundary="==boundary-2"
    
    --==boundary-2
    Content-Type: multipart/related; boundary="==boundary-1"; type="text/html"
    Content-Location: http://www.dsv.su.se/jpalme/mimetest/
    
    Text displayed only to non-MIME-compliant mailers
    --==boundary-1
    Content-Type: text/html; charset=us-ascii
    Content-Transfer-Encoding: 7bit
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
    <HTML>
    <HEAD><TITLE>Test message no. 11 first part</TITLE>
    </HEAD>
    <BODY>
    <H1>This is test message no. 11 first HTML part</H1>
    
    <H2>Here comes the red test image:</H2>
    <IMG SRC="http://www.dsv.su.se/jpalme/mimetest/red-test-image.gif" 
    BORDER=0 HEIGHT=32 WIDTH=117 ALT="red test image">
    
    <P>This is the last line of this test message.
    </BODY></HTML>
    
    --==boundary-1
    Content-Type: image/gif
    Content-ID: <image1.mhtml-11@dsv.su.se>
    Content-Location: http://www.dsv.su.se/jpalme/mimetest/red-test-image.gif
    Content-Transfer-Encoding: base64
    Content-Disposition: inline; filename="red-test-image.gif"
    
    R0lGODlhdQAgAPcAAP//////zP//mf//Zv//M///AP/M///MzP/Mmf/MZv/MM//MAP+Z//+Z
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iwocOGIyJKnEixosWLGDNq3MixY0aBFKuJrBZypMiIJ0ekrLhSpUmSKEdObMkSpsuY
    OG/qTMnzJUWQHoMKHUq0KEagRpMqXaoUaU6dG2lKlOqRKtOkTq9q3VrV5sd/XMOKZZp1rNmz
    GsuiXct2hNq2cMVmXdkzZ12LLe/ehYrXpsy/MPUGHvw04lzCdhFbzasYMd+aUxsnnrzTq1uw
    cTN3tVrxrebPWDGDHr3UM+nTHE2jXn1RNevXEl3Dfi179urDJrte5BzVcknNhyNHZiyzJnGv
    uWMuppu7uHLkyV1Kxe1ccOGZ0Cn/xshcu8/K2g2LQ8bJGPJj4eh3+/WNHb118PAtBn8aXTrn
    6s7tl2QP9b399fhNN55tbe31FYEITlRbgqAtyCBwAz5I20MUVmjhhRgyFBAAOw==
    --==boundary-1--
    --==boundary-2
    Content-Type: multipart/related; boundary="==boundary-1"; type="text/html"
    Content-Location: http://www.dsv.su.se/jpalme/mimetest/
    
    Text displayed only to non-MIME-compliant mailers
    --==boundary-1
    Content-Type: text/html; charset=us-ascii
    Content-Transfer-Encoding: 7bit
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
    <HTML>
    <HEAD><TITLE>Test message no. 11 second part</TITLE>
    </HEAD>
    <BODY>
    <H1>This is test message no. 11 second HTML part</H1>
    
    <H2>Here comes the yellow test image:</H2>
    <IMG SRC="cid:image2.mhtml-11@dsv.su.se" BORDER=0 HEIGHT=32 WIDTH=152
    ALT="yellow test image">
    
    <P>This is the last line of this test message.
    </BODY></HTML>
    
    --==boundary-1
    Content-Type: image/gif
    Content-ID: <image2.mhtml-11@dsv.su.se>
    Content-Transfer-Encoding: base64
    Content-Disposition: inline; filename="yellow-test-image.gif"
    
    R0lGODlhmAAgAPcAAP//////zP//mf//Zv//M///AP/M///MzP/Mmf/MZv/MM//MAP+Z//+Z
    zP+Zmf+ZZv+ZM/+ZAP9m//9mzP9mmf9mZv9mM/9mAP8z//8zzP8zmf8zZv8zM/8zAP8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FcGxUlU5s6MJEUqXcq0qdOnGpNCnUq1qlWlUm0K7emz502qX0WGvTo1K9mz
    aNOO7Wg2rdu3cDG2jUu3Ltm5dvPqZYp3r9+/G/u6/LmVp0vDMosO7Xo4sWPCGwmfTPm4cGXF
    XC0KbowZcdHLiNd6Br3YcUbIpC0vLi33H+DXbo+G3Ay7tl7atnPHxa27N1revoNXBS68uFPi
    xpOLRK68OVvXHFlfpIwz81PRHrGb9ku7tOyv4Kmnbu4avvxM8RbF50T//ajs1tnVW96OWnLO
    015VW9def/791fPBFx9nBPbXGH/5RXaegqHp1xloz5XEVXsFogchfueZl55oBpp2n1H2ccSc
    c4BJ59GIJKZYEYoqqshiiyS+CGNzEdVo44045qhjQwEBADs=
    --==boundary-1--
    --==boundary-2--
    

1 个答案:

答案 0 :(得分:0)

您可以使用一些免费的库,查看MailMessagesExamples.zip: http://www.lumisoft.ee/lswww/download/downloads/Examples/

或谷歌其他一些图书馆。

Mime非常复杂,从字符串撰写有效的消息很难,内部有一些unicode字符。