与附件一起打开默认邮件客户端

编程入门 行业动态 更新时间:2024-10-27 01:33:38
本文介绍了与附件一起打开默认邮件客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在一个WPF应用程序的工作(使用C#)。

Hi I am working on a WPF application (using c#).

我需要有一个功能,用户可以通过电子邮件发送文件(音频文件)作为附件。 我试图使用 Microsoft.Office.Interop.Outlook.Application 命名空间,但它打开Outlook并不会工作,如果后市没有客户端的计算机上安装。

I need to have a functionality where users can send files (audio files) as attachments via email. I tried using Microsoft.Office.Interop.Outlook.Application namespace but it opens outlook and wont work if outlook is not installed on the client's computer.

我使用试过 SmtpClient()和 MAILMESSAGE() System.Net.Mail 命名空间,但它不是开放的电子邮件客户端的类。 它通过预定义的服务器发送邮件(可能是一个问题,因为我不知道我的客户端的默认电子邮件域是这的链接拥有所有我需要的东西和它的工作的罚款。

I tried using SmtpClient() and MailMessage() classes of System.Net.Mail namespace but its not opening email client. Its sending a mail through predefined server (might be a problem since I don't know what my client's default email domain is. This link has all the things I need and its working fine.

但是,他们用DllImport属性并有使用此方法可能产生的(从我能理解)的许多问题。我不知道管理和非托管代码,所以我不能够理解的问题是什么想法。是否确定要遵循在上面的链接的例子。如果没有,为什么?

But there they used DllImport attribute and there are many issues that may arise (from what I can understand) from using this method. I have no idea about managed and un-managed code so I am not able to understand what the problem is. Is it OK to follow the example in the above link. If not why?

您能告诉或提供关于如何处理我的问题链接

Can you tell or provide links on how to approach my problem

推荐答案

我们可以利用的,大多数电子邮件客户端支持要加载的.eml文件格式的事实。

We can make use of the fact that most email clients support the .EML file format to be loaded.

所以,如果我们的方式延长System.Net.Mail.MailMessage类,它可以被保存到文件系统作为一个.EML文件所得文件可以使用的Process.Start(文件名默认邮件客户端被打开)

So if we Extend the System.Net.Mail.MailMessage Class in a way that it can be saved to the filesystem as an .EML file. The resulting file can be opened with the default mail client using Process.Start(filename)

有关这个正常工作,我们必须添加包含行X-未发送:1.eml文件。这行告诉电子邮件客户端加载.eml文件的邮件必须在新邮件模式呈现。

For this to work properly we have to add a line containing "X-Unsent: 1" to the .EML file. This line tells the email client loading the .EML file the message must be presented in "New message" mode.

使用扩展方法的addUnsentHeader布尔参数该行添加到.eml文件

Use the "addUnsentHeader" bool parameter of the extension method to add this line to the .EML file

扩展方法是这样的:

using System; using System.IO; using System.Net.Mail; using System.Reflection; namespace Fsolutions.Fbase.Common.Mail { public static class MailUtility { //Extension method for MailMessage to save to a file on disk public 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); } } } }

使用像这样的扩展方法:

Use the extension method like this:

var mailMessage = new MailMessage(); mailMessage.From = new MailAddress("someone@yourdomain"); mailMessage.Subject = "Your subject here"; mailMessage.IsBodyHtml = true; mailMessage.Body = "<span style='font-size: 12pt; color: red;'>My HTML formatted body</span>"; mailMessage.Attachments.Add(new Attachment("C://Myfile.pdf")); var filename = "C://Temp/mymessage.eml"; //save the MailMessage to the filesystem mailMessage.Save(filename); //Open the file with the default associated application registered on the local machine Process.Start(filename);

更多推荐

与附件一起打开默认邮件客户端

本文发布于:2023-11-23 11:17:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621258.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   附件   邮件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!