C#
在.NETCore2.2框架之上。
我能够解析请求并获得每条消息的MIME正文。但是,我不知道如何使用正文的内容创建文件。
下面是原始MIMI消息的开头示例
我尝试将正文作为字符串写入文件,但没有成功
string body = GetMimeBody(message);
File.WriteAllText("image_from_string" + MimeTypeMap.GetExtension(contentType), bytes);
byte[]
像这样,但仍然不起作用
byte[] bytes = Encoding.ASCII.GetBytes(body);
File.WriteAllBytes("image_from_ascii_bytes" + MimeTypeMap.GetExtension(contentType), bytes);
byte[] bytes = Encoding.Default.GetBytes(body);
File.WriteAllBytes("image_from_default_bytes" + MimeTypeMap.GetExtension(contentType), bytes);
byte[] bytes = Encoding.UTF8.GetBytes(body);
File.WriteAllBytes("image_from_utf8_bytes" + MimeTypeMap.GetExtension(contentType), bytes);
“不工作”是指图像无法正确打开。照片查看器显示“图像似乎已损坏或损坏。”
更新
下面是代码和解析部分
var responseContentType = response.Content.Headers.GetValues("Content-Type").FirstOrDefault();
string splitter = string.Format("--{0}", GetBoundary(responseContentType));
string content = await response.Content.ReadAsStringAsync();
var messages = content.Split(splitter, StringSplitOptions.RemoveEmptyEntries);
foreach (var message in messages)
{
var mimiParts = message.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
if (mimiParts.Length == 0)
{
continue;
}
string contentId = Str.GetValue("Content-ID", mimiParts, ':');
string objectId = Str.GetValue("Object-ID", mimiParts, ':');
string contentType = Str.GetValue("Content-Type", mimiParts, ':');
if (string.IsNullOrWhiteSpace(contentId) || string.IsNullOrWhiteSpace(objectId) || string.IsNullOrWhiteSpace(contentType))
{
continue;
}
string body = mimiParts[mimiParts.Length - 1];
var filename = string.Format("{0}_{1}{2}", contentId, objectId, MimeTypeMap.GetExtension(contentType));
var decoded = System.Net.WebUtility.HtmlDecode(data);
File.WriteAllText("image_from_html_decoded_bytes" + filename, decoded);
}
public class Str
{
public static string GetValue(string startWith, string[] lines, char splitter = '=')
{
foreach (var line in lines)
{
var value = line.Trim();
if (!value.StartsWith(startWith, StringComparison.CurrentCultureIgnoreCase) || !line.Contains(splitter))
{
continue;
}
return value.Split(splitter)[1].Trim();
}
return string.Empty;
}
}
下面是一个屏幕截图,显示了
mimiParts
变量
更新2
MimeKit
var responseContentType = response.Content.Headers.GetValues("Content-Type").FirstOrDefault();
if (!ContentType.TryParse(responseContentType, out ContentType documentContentType))
{
return;
}
var stream = await response.Content.ReadAsStreamAsync();
MimeEntity entity = MimeEntity.Load(documentContentType, stream);
Multipart messages = entity as Multipart;
if (messages == null)
{
throw new Exception("Unable to cast entity to Multipart");
}
foreach (MimeEntity message in messages)
{
string contentId = message.Headers["Content-ID"];
string objectId = message.Headers["Object-ID"];
string contentType = message.Headers["Content-Type"];
if (string.IsNullOrWhiteSpace(contentId) || string.IsNullOrWhiteSpace(objectId) || string.IsNullOrWhiteSpace(contentType))
{
continue;
}
var filename = string.Format("{0}_{1}{2}", contentId, objectId, MimeTypeMap.GetExtension(contentType));
message.WriteTo(filename);
}