尝试将 eml 文件作为附件插入会给出错误"The input is not a valid Base-64 string as it contains a non-base 64 character



我有一个 c# 中的邮件,我想将其转换为 EML 以作为附件插入另一个系统,但我得到的错误是

输入不是有效的 Base-64 字符串,因为它包含非 base 64 字符、两个以上的填充字符或填充字符中的非法字符

未使用文本正文。.我只是在 html 中传递

public Task<string> CreateEml(string @from, string to, string subject, string htmlBody, string textBody, List<Attachment> attachments = null)
{
using (var message = new MailMessage())
{               
message.To.Add(new MailAddress(to));
message.From = new MailAddress(@from);
//message.Bcc.Add(new MailAddress(bcc));
message.Subject = subject;
message.Body = htmlBody;
message.IsBodyHtml = true;
message.BodyEncoding = System.Text.Encoding.UTF8;
if (attachments != null)
{
foreach (var item in attachments)
{
message.Attachments.Add(item);
}
}
if (!string.IsNullOrEmpty(textBody))
{
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream))
{
writer.Write(textBody);
message.AlternateViews.Add(new AlternateView(stream));
var eml = message.ToEml();
return Task.FromResult(eml);
}
}
return Task.FromResult(message.ToEml());
}
}

有一个扩展方法 ToEml() 可以做到这一点

public static string ToEml(this MailMessage message)
{
var assembly = typeof(SmtpClient).Assembly;
var mailWriterType = assembly.GetType("System.Net.Mail.MailWriter");
using (var memoryStream = new MemoryStream())
{
// Get reflection info for MailWriter contructor
var mailWriterContructor = mailWriterType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(Stream) }, null);
// Construct MailWriter object with our FileStream
var mailWriter = mailWriterContructor.Invoke(new object[] { memoryStream });
// Get reflection info for Send() method on MailMessage
var sendMethod = typeof(MailMessage).GetMethod("Send", BindingFlags.Instance | BindingFlags.NonPublic);
// Call method passing in MailWriter
sendMethod.Invoke(message, BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { mailWriter, true, true }, null);
// Finally get reflection info for Close() method on our MailWriter
var closeMethod = mailWriter.GetType().GetMethod("Close", BindingFlags.Instance | BindingFlags.NonPublic);
// Call close method
closeMethod.Invoke(mailWriter, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { }, null);
return Encoding.ASCII.GetString(memoryStream.ToArray());
}
}    

此结果数据将分配给数据。电子邮件数据总线,用于将电子邮件作为附件插入第三方系统,如下所示

attachment.FileInfo[0] = new FileInfoClass
{
Description = emailDescription,
Category = "EMAIL",
FileName = "NewBusiness.eml",
Data = data.EmailDataBus,
};
using (var webService = new ConneXion())
{
webService.Url = CloudConfigurationManager.GetSetting("tam.webservice.url");
var response = webService.InsertAttachment(ref attachment);

我收到的错误消息是

输入不是有效的 Base-64 字符串,因为它包含非 base 64 字符、两个以上的填充字符或填充字符中的非法字符

数据中的数据。EmailBus 看起来像这样,请注意我没有包括所有的 base64,但页眉和页脚完好无损

https://plnkr.co/edit/n71EFK2gHRj6Nn4HX1Sn?p=preview

事实证明,我只需要将整个事情编码为 base64..,这有点奇怪,因为部分数据已经是 base64 .. 但我想不是全部。无论如何,现在一切都:)依恋很好。

byte[] emaildata = Encoding.UTF8.GetBytes(data.EmailDataBus);
string b64 = Convert.ToBase64String(emaildata);

相关内容

  • 没有找到相关文章

最新更新