我正在尝试从WPF应用程序向默认Windows 10邮件客户端发送一封带有附件的电子邮件。 MailUtility类准备要发送的邮件
public static class MailUtility
{
//Extension method for MailMessage to save to a file on disk
private static void Save(this MailMessage message, string filename, bool addUnsentHeader = true)
{
using (var filestream = File.Open(filename, FileMode.Create))
{
if (addUnsentHeader)
{
var binaryWriter = new BinaryWriter(filestream);
//Write the Unsent header to the file so the mail client knows this mail must be presented in "New message" mode
binaryWriter.Write(System.Text.Encoding.UTF8.GetBytes("X-Unsent: 1" + Environment.NewLine));
}
var assembly = typeof(SmtpClient).Assembly;
var mailWriterType = assembly.GetType("System.Net.Mail.MailWriter");
// 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[] { filestream });
// Get reflection info for Send() method on MailMessage
var sendMethod = typeof(MailMessage).GetMethod("Send", BindingFlags.Instance | BindingFlags.NonPublic);
sendMethod.Invoke(message, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { 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);
}
}
public static string PrepareEmlFile(string subject, string fromAddress, IEnumerable<string> toAddresses, IList<IAttachment> attachments, string body)
{
if (string.IsNullOrWhiteSpace(fromAddress))
{
fromAddress = "GebenSieIhre@Adressee.in";
}
var mailMessage = new MailMessage
{
From = new MailAddress(fromAddress),
Subject = subject,
IsBodyHtml = true,
Body = body
};
if (toAddresses != null)
{
foreach (var address in toAddresses.Where(s => string.IsNullOrWhiteSpace(s) == false))
{
mailMessage.To.Add(address);
}
}
if (attachments != null)
{
foreach (var attachment in attachments)
{
Attachment attachmentRaw = null;
if (attachment.FileData != null)
{
var ms = new MemoryStream(attachment.FileData);
attachmentRaw = new Attachment(ms, attachment.FileName, attachment.MimeType);
}
else if (File.Exists(attachment.FileName))
{
attachmentRaw = new Attachment(attachment.FileName, attachment.MimeType);
}
mailMessage.Attachments.Add(attachmentRaw);
}
}
var tempFile = Path.GetTempFileName();
var filename = Path.ChangeExtension(tempFile, "eml");
mailMessage.Save(filename);
mailMessage.Dispose();
return filename;
}
}
发送邮件的方法
public void SendViaSystemDefautClient(string subject, string fromAddress, IEnumerable<string> toAddress, IList<IAttachment> attachments, string body = "")
{
try
{
var prepareEmlFile = MailUtility.PrepareEmlFile(subject, fromAddress, toAddress, attachments, body);
Process.Start(prepareEmlFile);
}
catch (Exception e)
{
Logger.Error(e,"Failed to send e-mail using system default mail client.");
MessageHub.PushExceptionMessage(LangToXaml.ErrorSendingLetter);
}
}
执行代码后,将启动默认的Windows电子邮件,其中包含有关发送者和接收者以及附件的信息。我无法编辑此数据,也无法发送此消息