尝试将 eml 文件作为附件插入时出现错误 "The input is not a valid Base-64 string as it contains a non-base 64 character"
Trying to insert eml file as attachment gives error "The input is not a valid Base-64 string as it contains a non-base 64 character"
我在 c# 中有一个 MailMessage,我想将其转换为 EML 以作为附件插入到另一个系统中,但我得到的错误是
输入的 Base-64 字符串无效,因为它包含非 Base-64 字符、两个以上的填充字符或填充字符中的非法字符
没有使用textBody..我只是传入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());
}
}
此结果数据被分配给 data.EmailDataBus 并用于将电子邮件作为附件插入到第三方系统,如下所示
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 字符、两个以上的填充字符或填充字符中的非法字符
data.EmailBus 中的数据如下所示,注意我没有包括所有 base64,但页眉和页脚完好无损
原来我只需要将整个东西编码为 base64.. 这有点奇怪,因为部分数据已经是 base64.. 但我想不是全部。无论如何,现在一切正常:)附件连接正常。
byte[] emaildata = Encoding.UTF8.GetBytes(data.EmailDataBus);
string b64 = Convert.ToBase64String(emaildata);
我在 c# 中有一个 MailMessage,我想将其转换为 EML 以作为附件插入到另一个系统中,但我得到的错误是
输入的 Base-64 字符串无效,因为它包含非 Base-64 字符、两个以上的填充字符或填充字符中的非法字符
没有使用textBody..我只是传入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());
}
}
此结果数据被分配给 data.EmailDataBus 并用于将电子邮件作为附件插入到第三方系统,如下所示
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 字符、两个以上的填充字符或填充字符中的非法字符
data.EmailBus 中的数据如下所示,注意我没有包括所有 base64,但页眉和页脚完好无损
原来我只需要将整个东西编码为 base64.. 这有点奇怪,因为部分数据已经是 base64.. 但我想不是全部。无论如何,现在一切正常:)附件连接正常。
byte[] emaildata = Encoding.UTF8.GetBytes(data.EmailDataBus);
string b64 = Convert.ToBase64String(emaildata);