Email 直接从MIME字符串向MailMessage添加附件
有很多从文件添加附件的好例子,但我想知道是否可以用与Email 直接从MIME字符串向MailMessage添加附件,email,attachment,mime,mailmessage,Email,Attachment,Mime,Mailmessage,有很多从文件添加附件的好例子,但我想知道是否可以用与AlternateView.CreateAlternateViewFromString类似的方法来实现 我分别使用IMAP从一个地址获取电子邮件的body_文本和body_标题,然后希望从另一个地址发送(通过SMTP),并进行一些更改,但附件尽可能忠实地添加。我正在使用MailMessage和SmtpClient(System.Net和System.Net.Mail) 我已经编写了代码,通过“Content-Type:”标记从BODY[TEXT
AlternateView.CreateAlternateViewFromString
类似的方法来实现
我分别使用IMAP从一个地址获取电子邮件的body_文本和body_标题,然后希望从另一个地址发送(通过SMTP),并进行一些更改,但附件尽可能忠实地添加。我正在使用MailMessage和SmtpClient(System.Net和System.Net.Mail)
我已经编写了代码,通过“Content-Type:”标记从BODY[TEXT]中提取MIME部分,并以这种方式将纯文本添加到html视图中。我在MIME中也有附件(见下面的示例),因此我似乎可以轻松地直接从“Content-Type:application/octet-stream”从MIME中提取的字符串中添加附件。目前,我只对使用应用程序/八位字节流的it感兴趣
我的代码中有一部分将其作为字符串提供,其中包含:
Content-Type: application/octet-stream; name="test_file.abc"
Content-Disposition: attachment; filename="test_file.abc"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_j7datrbn0
YWJjIGZpbGU=
或
例如:
* 53 FETCH (BODY[TEXT] {614}
--001a1140ae52fcc4690558c101ec
Content-Type: multipart/alternative; boundary="001a1140ae52fcc4630558c101ea"
--001a1140ae52fcc4630558c101ea
Content-Type: text/plain; charset="UTF-8"
Blah blah
--001a1140ae52fcc4630558c101ea
Content-Type: text/html; charset="UTF-8"
<div dir="ltr">Blah blah</div>
--001a1140ae52fcc4630558c101ea--
--001a1140ae52fcc4690558c101ec
Content-Type: application/octet-stream; name="test_file.abc"
Content-Disposition: attachment; filename="test_file.abc"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_j7datrbn0
YWJjIGZpbGU=
--001a1140ae52fcc4690558c101ec--
)
$ OK Success
*53获取(正文[文本]{614}
--001a1140ae52fcc4690558c101ec
内容类型:多部分/可选;边界=“001a1140ae52fcc4630558c101ea”
--001a1140ae52fcc4630558c101ea
内容类型:文本/普通;charset=“UTF-8”
废话
--001a1140ae52fcc4630558c101ea
内容类型:text/html;charset=“UTF-8”
废话
--001a1140ae52fcc4630558c101ea--
--001a1140ae52fcc4690558c101ec
内容类型:应用程序/八位字节流;name=“test\u file.abc”
内容配置:附件;filename=“test\u file.abc”
内容传输编码:base64
X-附件-Id:f_j7datrbn0
YWJjIGZpbGU=
--001a1140ae52fcc4690558c101ec--
)
$OK成功
我的代码的所有其他方面都可以工作,但是有没有一种简单的方法可以直接从我拥有的MIME数据添加附件。我不使用MimeKit或任何标准VS以外的库
感谢您阅读这一长长的问题。这并不是我真正想做的,但我实现了我所需要的,如下所示:
string name = find_item_by_key(attachment_content, "filename"); // looks for key="value" and returns value
string attachment_base64 = attachment_content.Substring(attachment_content.LastIndexOf("\r\n\r\n"));
attachment_base64 = attachment_base64.Trim('\r', '\n');
byte[] attachment_bytes = Convert.FromBase64String(attachment_base64);
Attachment att = new Attachment(new System.IO.MemoryStream(attachment_bytes), name);
mail.Attachments.Add(att);
大量无意义的转换正在进行,因为数据已经是Base64,mail.Attachments.Add
无疑将再次将其转换回来
string name = find_item_by_key(attachment_content, "filename"); // looks for key="value" and returns value
string attachment_base64 = attachment_content.Substring(attachment_content.LastIndexOf("\r\n\r\n"));
attachment_base64 = attachment_base64.Trim('\r', '\n');
byte[] attachment_bytes = Convert.FromBase64String(attachment_base64);
Attachment att = new Attachment(new System.IO.MemoryStream(attachment_bytes), name);
mail.Attachments.Add(att);