在C#VSTO添加中从现有MimeSource创建邮件 [英] Create mail from existing MimeSource in C# VSTO add on

查看:71
本文介绍了在C#VSTO添加中从现有MimeSource创建邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,

我已从现有邮件创建了一个mime源。现在在vsto中添加,我正在尝试实现一个反向函数,它将转换来自mime源的邮件。当我尝试打开任何邮件时,我想要这种行为,我想用邮件替换它,我可以使用mime
源代码。我已经处理ItemEvents_10_Event(Office.Interop.Outlook命名空间)的打开事件

I have created a mime source from an existing mail. Now in vsto add on, I am trying to implement a reverse function which will convert mail from mime source. I want this behavior when I try to open any mail, I want it to replaced by the mail, for which mime source is available to me. I have handled Open event of ItemEvents_10_Event (of Office.Interop.Outlook namespace).

现在我已经实现功能GetMailFromMime象下面这样: -

Now I have Implemented function GetMailFromMime like below :--

public MailItem GetMailFromMime(string mimeSource,MailItem oldMail)

public MailItem GetMailFromMime(string mimeSource, MailItem oldMail)

{                     var
mimeStream =
new
COMMemoryStream < span style ="font-size:9.5pt; font-family:Consolas">( Encoding .ASCII.GetBytes(mimeSource));

{                    var mimeStream = new COMMemoryStream(Encoding.ASCII.GetBytes(mimeSource));

              ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
类型 converter =
Type 。GetTypeFromCLSID( MAPIMethods 。CLSID_IConverterSession);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
object obj =
Activator 。CreateInstance(转换器);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
MAPIMethods IConverterSession
session =(
MAPIMethods < span style ="font-size:9.5pt; font-family:Consolas">。 IConverterSession )obj;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;
if (session!=
null

  ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {

         ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
uint hr = session.SetEncoding( MAPIMethods ENCODINGTYPE 。IET_QP); // IET_UNICODE

         &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; hr = session.SetSaveFormat( MAPIMethods MIMESAVETYPE 。SAVE_RFC1521);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; hr = session.MIMEToMAPI(mimeStream,( MAPIMethods IMessage )oldMail.MAPIOBJECT,
null
MAPIMethods MAPITOMIMEFLAGS .CCSF_SMTP);

                        hr = session.MIMEToMAPI(mimeStream, (MAPIMethods.IMessage)oldMail.MAPIOBJECT, null, MAPIMethods.MAPITOMIMEFLAGS.CCSF_SMTP);

      ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;& NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
if (hr!= 0)

    ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
throw
new
ArgumentException "有一些无效的COM
参数"
);

            &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; oldMail.Save();

      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
string entryID = oldMail.EntryID;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;
Marshal 。ReleaseComObject(oldMail);

   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; oldMail =
null ;

&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;    //重新加载对象

                        //reload object

              ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; Microsoft.Office.Interop.Outlook。 NameSpace
olNamespace =
Globals < span style ="font-size:9.5pt; font-family:Consolas">。DarkMail.Application.GetNamespace( " MAPI" );

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; oldMail =( MailItem )olNamespace.GetItemFromID(entryID);

   ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
string subject = oldMail.Subject;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; string body = oldMail.HTMLBody;

      ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }

   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
return oldMail;

}

这里我已将当前邮件项目作为oldMail传递。当我检查内容时(即使重新启动Outlook后),对现有邮件没有影响。如果不是使用现有邮件,而是创建新邮件并保存它,我可以在草稿中看到有新邮件
正确地从我传递的mime源创建。现在我不想使用脏技巧,比如从草稿邮件修改打开的邮件(内容按内容),然后删除草稿中存在的邮件。我们有什么好工作吗?或者如果我做任何
的事情错了,请以正确的方式提供给我。如果我能得到一个小例子,我将不胜感激。

Here I have passed current mail item as oldMail. When I check the content (even after restarting of outlook), there is no impact on existing mail. If instead of using an existing mail, I create new mail and save it, I can see in Draft that there is new mail rightly created from mime source which I have passed. Now I dont want to use dirty trick, like modify opened mail from mail of draft (content by content) and then delete mail which is present in draft. Do we have any good work around ? or if I am doing any thing wrong, please provide me right way. I will appreciate if I can get a small example as well.

谢谢

Dev

推荐答案

您不应该从Open事件中执行此操作。您可以做的最好的事情是创建一条新邮件,导入MIME流,保存它并祈祷Outlook不会覆盖MIME导入创建的数据。然后,您可以完全释放项目(Marshal.ReleaseComObject)
并使用Namespace.GetItemfromID重新打开它。

You should not do that from the Open event. The best you can do is create a new message, import the MIME stream, save it and pray that Outlook does not overwrite the data created by MIME import. You can then completely release the item (Marshal.ReleaseComObject) and reopen it using Namespace.GetItemfromID.

另一种方法是避免使用Outlook对象模型并使用扩展MAPI(C ++或Delphi)或
Redemption (其
RDO
对象系列)来创建消息并导入MIME数据。由于OOM永远不会触及该项目,因此在完成后使用Namespace.GetItemfromID打开它应显示所有导入的数据。

Another alternative is to avoid using the Outlook Object Model at all and either use Extended MAPI (C++ or Delphi) or Redemption (its RDO family of objects) to create the message and import the MIME data. Since the item would never be touched by OOM, opening it using Namespace.GetItemfromID after you are done should show all the imported data.


这篇关于在C#VSTO添加中从现有MimeSource创建邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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