PEPMessage.cs
changeset 543 ba430eec9ea3
parent 518 09e4791fe4c8
child 545 fb6131233988
equal deleted inserted replaced
542:7b2e02efdf23 543:ba430eec9ea3
   493         /// <param name="omi">The Outlook mail item to apply this pEp message's data to.</param>
   493         /// <param name="omi">The Outlook mail item to apply this pEp message's data to.</param>
   494         public void ApplyTo(Outlook.MailItem omi)
   494         public void ApplyTo(Outlook.MailItem omi)
   495         {
   495         {
   496             string tempDir;
   496             string tempDir;
   497             string tempFile;
   497             string tempFile;
   498             Outlook.Attachment attachment;
   498             Outlook.Attachment newAttachment = null;
   499             Outlook.Recipient newRecipient;
   499             Outlook.Attachments attachments = null;
   500 
   500             Outlook.Recipient newRecipient = null;
   501             // Remove all recipients
   501             Outlook.Recipients recipients = null;
   502             while (omi.Recipients.Count > 0)
   502             Outlook.Account currAccount = null;
   503             {
   503             Outlook.Account sendUsingAccount = null;
   504                 omi.Recipients.Remove(1);
   504             Outlook.Accounts accounts = null;
   505             }
   505             Outlook.PropertyAccessor properties = null;
   506 
   506 
   507             // Set recipients
   507             try
   508             for (int i = 0; i < this._BCC.Count; i++)
   508             {
   509             {
   509                 // Remove all recipients
   510                 if (this._BCC[i].Address != null)
   510                 recipients = omi.Recipients;
   511                 {
   511                 while (recipients.Count > 0)
   512                     newRecipient = omi.Recipients.Add(this._BCC[i].Address);
   512                 {
   513                     newRecipient.Type = (int)Outlook.OlMailRecipientType.olBCC;
   513                     recipients.Remove(1);
   514 
   514                 }
   515                     Marshal.ReleaseComObject(newRecipient);
   515 
   516                 }
   516                 // Set recipients
   517             }
   517                 for (int i = 0; i < this._BCC.Count; i++)
   518 
   518                 {
   519             for (int i = 0; i < this._CC.Count; i++)
   519                     if (this._BCC[i].Address != null)
   520             {
   520                     {
   521                 if (this._CC[i].Address != null)
   521                         newRecipient = recipients.Add(this._BCC[i].Address);
   522                 {
   522                         newRecipient.Type = (int)Outlook.OlMailRecipientType.olBCC;
   523                     newRecipient = omi.Recipients.Add(this._CC[i].Address);
   523 
   524                     newRecipient.Type = (int)Outlook.OlMailRecipientType.olCC;
   524                         Marshal.ReleaseComObject(newRecipient);
   525 
   525                         newRecipient = null;
   526                     Marshal.ReleaseComObject(newRecipient);
   526                     }
   527                 }
   527                 }
   528             }
   528 
   529 
   529                 for (int i = 0; i < this._CC.Count; i++)
   530             for (int i = 0; i < this._To.Count; i++)
   530                 {
   531             {
   531                     if (this._CC[i].Address != null)
   532                 if (this._To[i].Address != null)
   532                     {
   533                 {
   533                         newRecipient = recipients.Add(this._CC[i].Address);
   534                     newRecipient = omi.Recipients.Add(this._To[i].Address);
   534                         newRecipient.Type = (int)Outlook.OlMailRecipientType.olCC;
   535                     newRecipient.Type = (int)Outlook.OlMailRecipientType.olTo;
   535 
   536 
   536                         Marshal.ReleaseComObject(newRecipient);
   537                     Marshal.ReleaseComObject(newRecipient);
   537                         newRecipient = null;
   538                 }
   538                     }
   539             }
   539                 }
   540 
   540 
   541             omi.Recipients.ResolveAll();
   541                 for (int i = 0; i < this._To.Count; i++)
   542 
   542                 {
   543             /* Set sender
   543                     if (this._To[i].Address != null)
   544              * Note that if fails, will be empty which eventually will use the default send account
   544                     {
   545              * If the send using account is already populated, this cannot be re-set.
   545                         newRecipient = recipients.Add(this._To[i].Address);
   546              * So far this doesn't appear to be an issue as it occurs when applying unencrypted data to a mirror.
   546                         newRecipient.Type = (int)Outlook.OlMailRecipientType.olTo;
   547              * However, the mirror SendUsingAccount is already correct at this point and doesn't need to be set.
   547 
   548              */
   548                         Marshal.ReleaseComObject(newRecipient);
   549             if ((this._From != null) &&
   549                         newRecipient = null;
   550                 (this._From.Address != null) &&
   550                     }
   551                 (omi.SendUsingAccount == null))
   551                 }
   552             {
   552 
   553                 for (int i = 1; i <= Globals.ThisAddIn.Application.Session.Accounts.Count; i++)
   553                 recipients.ResolveAll();
   554                 {
   554 
   555                     Outlook.Account currAccount = Globals.ThisAddIn.Application.Session.Accounts[i];
   555                 /* Set sender
   556 
   556                  * Note that if fails, will be empty which eventually will use the default send account
   557                     if ((currAccount.SmtpAddress != null) &&
   557                  * If the send using account is already populated, this cannot be re-set.
   558                         (currAccount.SmtpAddress.ToUpper() == this._From.Address.ToUpper()))
   558                  * So far this doesn't appear to be an issue as it occurs when applying unencrypted data to a mirror.
   559                     {
   559                  * However, the mirror SendUsingAccount is already correct at this point and doesn't need to be set.
   560                         omi.SendUsingAccount = currAccount;
   560                  */
       
   561                 sendUsingAccount = omi.SendUsingAccount;
       
   562 
       
   563                 if ((this._From != null) &&
       
   564                     (this._From.Address != null) &&
       
   565                     (sendUsingAccount == null))
       
   566                 {
       
   567                     accounts = Globals.ThisAddIn.Application.Session.Accounts;
       
   568 
       
   569                     // Note: Index starts at 1
       
   570                     for (int i = 1; i <= accounts.Count; i++)
       
   571                     {
       
   572                         currAccount = accounts[i];
       
   573 
       
   574                         if ((currAccount.SmtpAddress != null) &&
       
   575                             (currAccount.SmtpAddress.ToUpper() == this._From.Address.ToUpper()))
       
   576                         {
       
   577                             /* Try to set the SendUsingAccount
       
   578                              * This will fail if the mail item is already marked as sent or the SendUsingAccount is not null, etc...
       
   579                              * This property should also ideally be set before a mail item is saved.
       
   580                              */
       
   581                             try
       
   582                             {
       
   583                                 sendUsingAccount = currAccount;
       
   584                             }
       
   585                             catch { }
       
   586 
       
   587                             Marshal.ReleaseComObject(currAccount);
       
   588                             currAccount = null;
       
   589 
       
   590                             break;
       
   591                         }
   561 
   592 
   562                         Marshal.ReleaseComObject(currAccount);
   593                         Marshal.ReleaseComObject(currAccount);
   563                         currAccount = null;
   594                         currAccount = null;
   564 
   595                     }
   565                         break;
   596                 }
   566                     }
   597 
   567 
   598                 // Set the subject
       
   599                 omi.Subject = this._ShortMsg;
       
   600 
       
   601                 // Set the body
       
   602                 if (string.IsNullOrWhiteSpace(this._LongMsgFormatted))
       
   603                 {
       
   604                     omi.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;
       
   605                     omi.Body = this._LongMsg;
       
   606                 }
       
   607                 else
       
   608                 {
       
   609                     omi.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
       
   610                     omi.HTMLBody = this._LongMsgFormatted;
       
   611                 }
       
   612 
       
   613                 // Remove any previous attachments
       
   614                 attachments = omi.Attachments;
       
   615                 while (attachments.Count > 0)
       
   616                 {
       
   617                     attachments.Remove(1);
       
   618                 }
       
   619 
       
   620                 // Create temp directory for attachments
       
   621                 tempDir = Path.GetTempFileName() + ".dir";
       
   622                 Directory.CreateDirectory(tempDir);
       
   623 
       
   624                 // Add new attachments
       
   625                 for (int i = 0; i < this._Attachments.Count; i++)
       
   626                 {
       
   627                     // Write current attachment to disk
       
   628                     tempFile = tempDir + "\\";
       
   629                     if (string.IsNullOrWhiteSpace(this._Attachments[i].FileName))
       
   630                     {
       
   631                         tempFile += "attachment" + i.ToString();
       
   632                     }
       
   633                     else
       
   634                     {
       
   635                         tempFile += this._Attachments[i].FileName;
       
   636                     }
       
   637                     File.WriteAllBytes(tempFile, this._Attachments[i].Data);
       
   638 
       
   639                     // Create a new attachment from the file on disk
       
   640                     newAttachment = attachments.Add(tempFile);
       
   641 
       
   642                     if (string.IsNullOrWhiteSpace(this._Attachments[i].MIMEType))
       
   643                     {
       
   644                         // Undefined
       
   645                         PEPAttachment.SetMIMEType(newAttachment, "application/octet-stream");
       
   646                     }
       
   647                     else
       
   648                     {
       
   649                         PEPAttachment.SetMIMEType(newAttachment, this._Attachments[i].MIMEType);
       
   650                     }
       
   651 
       
   652                     Marshal.ReleaseComObject(newAttachment);
       
   653                     newAttachment = null;
       
   654                 }
       
   655 
       
   656                 // Delete temp directory for attachments
       
   657                 Directory.Delete(tempDir, true);
       
   658 
       
   659                 // Optional fields
       
   660                 properties = omi.PropertyAccessor;
       
   661                 for (int i = 0; i < this._OptionalFields.Count; i++)
       
   662                 {
       
   663                     properties.SetProperty(ThisAddIn.PR_OPT_FIELD + this._OptionalFields[i].name,
       
   664                                            this._OptionalFields[i].value);
       
   665                 }
       
   666             }
       
   667             finally
       
   668             {
       
   669                 if (newAttachment != null)
       
   670                 {
       
   671                     Marshal.ReleaseComObject(newAttachment);
       
   672                     newAttachment = null;
       
   673                 }
       
   674 
       
   675                 if (attachments != null)
       
   676                 {
       
   677                     Marshal.ReleaseComObject(attachments);
       
   678                     attachments = null;
       
   679                 }
       
   680 
       
   681                 if (newRecipient != null)
       
   682                 {
       
   683                     Marshal.ReleaseComObject(newRecipient);
       
   684                     newRecipient = null;
       
   685                 }
       
   686 
       
   687                 if (recipients != null)
       
   688                 {
       
   689                     Marshal.ReleaseComObject(recipients);
       
   690                     recipients = null;
       
   691                 }
       
   692 
       
   693                 if (currAccount != null)
       
   694                 {
   568                     Marshal.ReleaseComObject(currAccount);
   695                     Marshal.ReleaseComObject(currAccount);
   569                     currAccount = null;
   696                     currAccount = null;
   570                 }
   697                 }
   571             }
   698 
   572 
   699                 if (sendUsingAccount != null)
   573             // Set the subject
   700                 {
   574             omi.Subject = this._ShortMsg;
   701                     Marshal.ReleaseComObject(sendUsingAccount);
   575 
   702                     sendUsingAccount = null;
   576             // Set the body
   703                 }
   577             if (string.IsNullOrWhiteSpace(this._LongMsgFormatted))
   704 
   578             {
   705                 if (accounts != null)
   579                 omi.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;
   706                 {
   580                 omi.Body = this._LongMsg;
   707                     Marshal.ReleaseComObject(accounts);
   581             }
   708                     accounts = null;
   582             else
   709                 }
   583             {
   710 
   584                 omi.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
   711                 if (properties != null)
   585                 omi.HTMLBody = this._LongMsgFormatted;
   712                 {
   586             }
   713                     Marshal.ReleaseComObject(properties);
   587 
   714                     properties = null;
   588             // Remove any previous attachments
   715                 }
   589             while (omi.Attachments.Count > 0)
   716             }
   590             {
   717 
   591                 omi.Attachments.Remove(1);
       
   592             }
       
   593 
       
   594             // Create temp directory for attachments
       
   595             tempDir = Path.GetTempFileName() + ".dir";
       
   596             Directory.CreateDirectory(tempDir);
       
   597 
       
   598             // Add new attachments
       
   599             for (int i = 0; i < this._Attachments.Count; i++)
       
   600             {
       
   601                 // Write current attachment to disk
       
   602                 tempFile = tempDir + "\\";
       
   603                 if (string.IsNullOrWhiteSpace(this._Attachments[i].FileName))
       
   604                 {
       
   605                     tempFile += "attachment" + i.ToString();
       
   606                 }
       
   607                 else
       
   608                 {
       
   609                     tempFile += this._Attachments[i].FileName;
       
   610                 }
       
   611                 File.WriteAllBytes(tempFile, this._Attachments[i].Data);
       
   612 
       
   613                 // Create a new attachment from the file on disk
       
   614                 attachment = omi.Attachments.Add(tempFile);
       
   615 
       
   616                 if (string.IsNullOrWhiteSpace(this._Attachments[i].MIMEType))
       
   617                 {
       
   618                     // Undefined
       
   619                     PEPAttachment.SetMIMEType(attachment, "application/octet-stream");
       
   620                 }
       
   621                 else
       
   622                 {
       
   623                     PEPAttachment.SetMIMEType(attachment, this._Attachments[i].MIMEType);
       
   624                 }
       
   625 
       
   626                 // Release attachment
       
   627                 if (attachment != null)
       
   628                 {
       
   629                     Marshal.ReleaseComObject(attachment);
       
   630                     attachment = null;
       
   631                 }
       
   632             }
       
   633 
       
   634             // Delete temp directory for attachments
       
   635             Directory.Delete(tempDir, true);
       
   636             
       
   637             // Optional fields
       
   638             for (int i = 0; i < this._OptionalFields.Count; i++)
       
   639             {
       
   640                 omi.PropertyAccessor.SetProperty(ThisAddIn.PR_OPT_FIELD + this._OptionalFields[i].name,
       
   641                                                  this._OptionalFields[i].value);
       
   642             }
       
   643             
       
   644             return;
   718             return;
   645         }
   719         }
   646 
   720 
   647         /// <summary>
   721         /// <summary>
   648         /// Recursivley converts all "BCC", "CC", and "To" identities into a 'flat' list of any members.
   722         /// Recursivley converts all "BCC", "CC", and "To" identities into a 'flat' list of any members.