打开默认邮件客户端和附件 [英] Open default mail client along with a attachment

查看:33
本文介绍了打开默认邮件客户端和附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 WPF 应用程序(使用 c#).

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

我需要一个功能,用户可以通过电子邮件将文件(音频文件)作为附件发送.我尝试使用 Microsoft.Office.Interop.Outlook.Application 命名空间,但如果客户端计算机上未安装 Outlook,它会打开 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.

我尝试使用 System.Net.Mail 命名空间的 SmtpClient()MailMessage() 类,但它没有打开电子邮件客户端.它通过预定义的服务器发送邮件(可能是一个问题,因为我不知道我客户端的默认电子邮件域是什么.这个 link 有我需要的所有东西,而且工作正常.

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.

因此,如果我们以一种可以将其作为 .EML 文件保存到文件系统的方式扩展 System.Net.Mail.MailMessage 类.可以使用 Process.Start(filename)

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)

为了使其正常工作,我们必须在 .EML 文件中添加一行包含X-Unsent: 1"的行.此行告诉加载 .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"bool参数将这一行添加到.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);
            }
        }
    }
}

像这样使用扩展方法:

        var mailMessage = new MailMessage();
        mailMessage.From = new MailAddress("someone@yourdomain.com");
        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);

这篇关于打开默认邮件客户端和附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆