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

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

问题描述

我在一个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.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天全站免登陆