PEPMessage.cs
author Dean Looyengoed
Mon, 08 Feb 2016 16:43:28 +0100
changeset 390 62e3fa742dc1
parent 389 e11ca0c9f1d5
child 398 6f2f35f40e63
permissions -rw-r--r--
Add another copy method to PEPMessage allowing for copies without content.
Dean@384
     1
´╗┐using Microsoft.Win32;
Dean@384
     2
using pEpCOMServerAdapterLib;
Dean@384
     3
using System;
Dean@384
     4
using System.Collections.Generic;
Dean@384
     5
using System.IO;
Dean@384
     6
using System.Runtime.InteropServices;
Dean@384
     7
using Outlook = Microsoft.Office.Interop.Outlook;
Dean@384
     8
Dean@384
     9
namespace pEp
Dean@384
    10
{
Dean@384
    11
    /// <summary>
Dean@389
    12
    /// Class for a completely in-memory message based on the pEp engine text_message.
Dean@384
    13
    /// </summary>
Dean@389
    14
    public class PEPMessage : Interfaces.ICopy<PEPMessage>
Dean@384
    15
    {
Dean@384
    16
        private List<PEPAttachment> _Attachments;
Dean@384
    17
        private List<PEPIdentity>   _BCC;
Dean@384
    18
        private List<PEPIdentity>   _CC;
Dean@384
    19
        private _pEp_msg_direction  _Direction;
Dean@384
    20
        private PEPIdentity         _From;
Dean@384
    21
        private string              _ID;
Dean@384
    22
        private List<string>        _Keywords;
Dean@384
    23
        private string              _LongMsg;
Dean@384
    24
        private string              _LongMsgFormatted;
Dean@384
    25
        private List<opt_field>     _OptionalFields;
Dean@384
    26
        private string              _ShortMsg;
Dean@384
    27
        private List<PEPIdentity>   _To;
Dean@384
    28
Dean@384
    29
        /**************************************************************
Dean@384
    30
         * 
Dean@384
    31
         * Constructors
Dean@384
    32
         * 
Dean@384
    33
         *************************************************************/
Dean@384
    34
Dean@384
    35
        /// <summary>
Dean@384
    36
        /// Default constructor.
Dean@384
    37
        /// </summary>
Dean@384
    38
        public PEPMessage()
Dean@384
    39
        {
Dean@384
    40
            this._Attachments = new List<PEPAttachment>();
Dean@384
    41
            this._BCC = new List<PEPIdentity>();
Dean@384
    42
            this._CC = new List<PEPIdentity>();
Dean@384
    43
            this._Direction = _pEp_msg_direction.pEp_dir_incoming;
Dean@384
    44
            this._From = null;
Dean@384
    45
            this._ID = null;
Dean@384
    46
            this._Keywords = new List<string>();
Dean@384
    47
            this._LongMsg = null;
Dean@384
    48
            this._LongMsgFormatted = null;
Dean@384
    49
            this._OptionalFields = new List<opt_field>();
Dean@384
    50
            this._ShortMsg = null;
Dean@384
    51
            this._To = new List<PEPIdentity>();
Dean@384
    52
        }
Dean@384
    53
Dean@384
    54
        /// <summary>
Dean@384
    55
        /// Constructs a new message from the given pEp engine text_message.
Dean@384
    56
        /// </summary>
Dean@384
    57
        /// <param name="msg">The text_message to construct from.</param>
Dean@384
    58
        public PEPMessage(text_message msg)
Dean@384
    59
        {
Dean@384
    60
            // Attachments
Dean@384
    61
            this._Attachments = new List<PEPAttachment>();
Dean@384
    62
            if (msg.attachments != null)
Dean@384
    63
            {
Dean@384
    64
                for (int i = 0; i < msg.attachments.Length; i++)
Dean@384
    65
                {
Dean@384
    66
                    this._Attachments.Add(new PEPAttachment((blob)msg.attachments.GetValue(i)));
Dean@384
    67
                }
Dean@384
    68
            }
Dean@384
    69
Dean@384
    70
            // BCC
Dean@384
    71
            this._BCC = new List<PEPIdentity>();
Dean@384
    72
            if (msg.bcc != null)
Dean@384
    73
            {
Dean@384
    74
                for (int i = 0; i < msg.bcc.Length; i++)
Dean@384
    75
                {
Dean@384
    76
                    this._BCC.Add(new PEPIdentity((pEp_identity_s)msg.bcc.GetValue(i)));
Dean@384
    77
                }
Dean@384
    78
            }
Dean@384
    79
Dean@384
    80
            // CC
Dean@384
    81
            this._CC = new List<PEPIdentity>();
Dean@384
    82
            if (msg.cc != null)
Dean@384
    83
            {
Dean@384
    84
                for (int i = 0; i < msg.cc.Length; i++)
Dean@384
    85
                {
Dean@384
    86
                    this._CC.Add(new PEPIdentity((pEp_identity_s)msg.cc.GetValue(i)));
Dean@384
    87
                }
Dean@384
    88
            }
Dean@384
    89
Dean@384
    90
            this._Direction = msg.dir;
Dean@384
    91
            this._From = new PEPIdentity(msg.from);
Dean@384
    92
            this._ID = msg.id;
Dean@384
    93
Dean@384
    94
            // Keywords
Dean@384
    95
            this._Keywords = new List<string>();
Dean@384
    96
            if (msg.keywords != null)
Dean@384
    97
            {
Dean@384
    98
                for (int i = 0; i < msg.keywords.Length; i++)
Dean@384
    99
                {
Dean@384
   100
                    this._Keywords.Add((string)msg.keywords.GetValue(i));
Dean@384
   101
                }
Dean@384
   102
            }
Dean@384
   103
Dean@384
   104
            this._LongMsg = msg.longmsg;
Dean@384
   105
            this._LongMsgFormatted = msg.longmsg_formatted;
Dean@384
   106
Dean@384
   107
            // Optional fields
Dean@384
   108
            this._OptionalFields = new List<opt_field>();
Dean@384
   109
            if (msg.opt_fields != null)
Dean@384
   110
            {
Dean@384
   111
                for (int i = 0; i < msg.opt_fields.Length; i++)
Dean@384
   112
                {
Dean@384
   113
                    this._OptionalFields.Add((opt_field)msg.opt_fields.GetValue(i));
Dean@384
   114
                }
Dean@384
   115
            }
Dean@384
   116
Dean@384
   117
            this._ShortMsg = msg.shortmsg;
Dean@384
   118
Dean@384
   119
            // To
Dean@384
   120
            this._To = new List<PEPIdentity>();
Dean@384
   121
            if (msg.to != null)
Dean@384
   122
            {
Dean@384
   123
                for (int i = 0; i < msg.to.Length; i++)
Dean@384
   124
                {
Dean@384
   125
                    this._To.Add(new PEPIdentity((pEp_identity_s)msg.to.GetValue(i)));
Dean@384
   126
                }
Dean@384
   127
            }
Dean@384
   128
        }
Dean@384
   129
Dean@384
   130
        /// <summary>
Dean@384
   131
        /// Contructs a new message from the given outlook mail item.
Dean@384
   132
        /// </summary>
Dean@384
   133
        /// <param name="omi">The outlook mail item to create the message from.</param>
Dean@384
   134
        /// <param name="createWithoutContent">Whether the outlook mail item should be created with
Dean@384
   135
        /// any content such as text body and attachments.</param>
Dean@384
   136
        public PEPMessage(Outlook.MailItem omi,
Dean@384
   137
                          bool createWithoutContent = false)
Dean@384
   138
        {
Dean@384
   139
            string delim;
Dean@384
   140
            string bodyLong = null;
Dean@384
   141
            string bodyLongFormatted = null;
Dean@384
   142
            PEPIdentity from = null;
Dean@384
   143
            List<string> keywordList = new List<string>();
Dean@384
   144
            List<PEPIdentity> toList = new List<PEPIdentity>();
Dean@384
   145
            List<PEPIdentity> ccList = new List<PEPIdentity>();
Dean@384
   146
            List<PEPIdentity> bccList = new List<PEPIdentity>();
Dean@384
   147
            List<PEPAttachment> attachmentList = new List<PEPAttachment>();
Dean@384
   148
            Outlook.Account acct = null;
Dean@384
   149
Dean@384
   150
            ///////////////////////////////////////////////////////////
Dean@384
   151
            // Calculate From
Dean@384
   152
            ///////////////////////////////////////////////////////////
Dean@384
   153
Dean@384
   154
            if (CryptableMailItem.GetIsIncoming(omi))
Dean@384
   155
            {
Dean@384
   156
                if (omi.Sender != null)
Dean@384
   157
                {
Dean@384
   158
                    from = new PEPIdentity();
Dean@384
   159
                    from.Address = omi.Sender.Address;
Dean@384
   160
                    from.UserID = omi.Sender.ID;
Dean@384
   161
                    from.Username = omi.Sender.Name;
Dean@384
   162
                }
Dean@384
   163
                else
Dean@384
   164
                {
Dean@384
   165
                    from = new PEPIdentity();
Dean@384
   166
                    from.Address = omi.SenderEmailAddress;
Dean@384
   167
                    from.Username = omi.SenderName;
Dean@384
   168
                }
Dean@384
   169
            }
Dean@384
   170
            else // Outgoing
Dean@384
   171
            {
Dean@384
   172
                if (omi.SendUsingAccount != null)
Dean@384
   173
                {
Dean@384
   174
                    from = new PEPIdentity();
Dean@384
   175
                    from.Address = omi.SendUsingAccount.SmtpAddress;
Dean@384
   176
                    from.UserID = omi.SendUsingAccount.CurrentUser.EntryID;
Dean@384
   177
                    from.Username = omi.SendUsingAccount.CurrentUser.Name;
Dean@384
   178
                }
Dean@384
   179
                else
Dean@384
   180
                {
Dean@384
   181
                    // Attempt to get the first account (note: index starts at 1)
Dean@384
   182
                    if (Globals.ThisAddIn.Application.Session.Accounts.Count >= 1)
Dean@384
   183
                    {
Dean@384
   184
                        acct = Globals.ThisAddIn.Application.Session.Accounts[1];
Dean@384
   185
Dean@384
   186
                        from = new PEPIdentity();
Dean@384
   187
                        from.Address = acct.SmtpAddress;
Dean@384
   188
                        from.UserID = acct.CurrentUser.EntryID;
Dean@384
   189
                        from.Username = acct.CurrentUser.Name;
Dean@384
   190
                    }
Dean@384
   191
                    else
Dean@384
   192
                    {
Dean@384
   193
                        throw (new Exception("No sending account for mail item."));
Dean@384
   194
                    }
Dean@384
   195
                }
Dean@384
   196
            }
Dean@384
   197
Dean@384
   198
            ///////////////////////////////////////////////////////////
Dean@384
   199
            // Calculate recipients
Dean@384
   200
            ///////////////////////////////////////////////////////////
Dean@384
   201
Dean@384
   202
            foreach (Outlook.Recipient r in omi.Recipients)
Dean@384
   203
            {
Dean@384
   204
                switch ((Outlook.OlMailRecipientType)r.Type)
Dean@384
   205
                {
Dean@384
   206
                    case Outlook.OlMailRecipientType.olTo:
Dean@384
   207
                        toList.Add(PEPIdentity.ToIdentity(r));
Dean@384
   208
                        break;
Dean@384
   209
                    case Outlook.OlMailRecipientType.olCC:
Dean@384
   210
                        ccList.Add(PEPIdentity.ToIdentity(r));
Dean@384
   211
                        break;
Dean@384
   212
                    case Outlook.OlMailRecipientType.olBCC:
Dean@384
   213
                        bccList.Add(PEPIdentity.ToIdentity(r));
Dean@384
   214
                        break;
Dean@384
   215
                }
Dean@384
   216
            }
Dean@384
   217
Dean@384
   218
            ///////////////////////////////////////////////////////////
Dean@384
   219
            // Calculate text body and attachments
Dean@384
   220
            ///////////////////////////////////////////////////////////
Dean@384
   221
Dean@384
   222
            if (createWithoutContent == false)
Dean@384
   223
            {
Dean@384
   224
                // Body
Dean@384
   225
                if (omi.Body != null)
Dean@384
   226
                {
Dean@384
   227
                    bodyLong = omi.Body.Replace("\r\n", "\n");
Dean@384
   228
Dean@384
   229
                    // Force rich text into HTML
Dean@384
   230
                    if (omi.BodyFormat == Outlook.OlBodyFormat.olFormatRichText)
Dean@384
   231
                    {
Dean@384
   232
                        omi.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
Dean@384
   233
                    }
Dean@384
   234
Dean@384
   235
                    if (omi.BodyFormat == Outlook.OlBodyFormat.olFormatHTML)
Dean@384
   236
                    {
Dean@384
   237
                        bodyLongFormatted = omi.HTMLBody;
Dean@384
   238
                    }
Dean@384
   239
                }
Dean@384
   240
Dean@384
   241
                // Attachments
Dean@384
   242
                foreach (Outlook.Attachment a in omi.Attachments)
Dean@384
   243
                {
Dean@384
   244
                    try
Dean@384
   245
                    {
Dean@384
   246
                        attachmentList.Add(new PEPAttachment(a));
Dean@384
   247
                    }
Dean@384
   248
                    catch { }
Dean@384
   249
                }
Dean@384
   250
Dean@384
   251
                // Keywords
Dean@384
   252
                if (omi.Categories != null)
Dean@384
   253
                {
Dean@384
   254
                    using (RegistryKey key1 = Registry.CurrentUser.OpenSubKey("Control Panel\\International"))
Dean@384
   255
                    {
Dean@384
   256
                        delim = key1.GetValue("sList").ToString();
Dean@384
   257
                        keywordList = new List<string>(omi.Categories.Split(delim.ToCharArray()));
Dean@384
   258
                    }
Dean@384
   259
                }
Dean@384
   260
            }
Dean@384
   261
Dean@384
   262
            ///////////////////////////////////////////////////////////
Dean@384
   263
            // Set properties
Dean@384
   264
            ///////////////////////////////////////////////////////////
Dean@384
   265
Dean@384
   266
            this._Attachments = attachmentList;
Dean@384
   267
            this._BCC = bccList;
Dean@384
   268
            this._CC = ccList;
Dean@384
   269
            this._Direction = CryptableMailItem.GetIsIncoming(omi) ? _pEp_msg_direction.pEp_dir_incoming : _pEp_msg_direction.pEp_dir_outgoing;
Dean@384
   270
            this._From = from;
Dean@384
   271
            this._ID = CryptableMailItem.GetMessageID(omi);
Dean@384
   272
            this._Keywords = keywordList;
Dean@384
   273
            this._LongMsg = bodyLong;
Dean@384
   274
            this._LongMsgFormatted = bodyLongFormatted;
Dean@384
   275
            this._OptionalFields = new List<opt_field>();
Dean@384
   276
            this._ShortMsg = omi.Subject;
Dean@384
   277
            this._To = toList;
Dean@384
   278
Dean@384
   279
            ///////////////////////////////////////////////////////////
Dean@384
   280
            // Free resources
Dean@384
   281
            ///////////////////////////////////////////////////////////
Dean@384
   282
Dean@384
   283
            if (acct != null)
Dean@384
   284
            {
Dean@384
   285
                Marshal.ReleaseComObject(acct);
Dean@384
   286
                acct = null;
Dean@384
   287
            }
Dean@384
   288
Dean@384
   289
            return;
Dean@384
   290
        }
Dean@384
   291
Dean@384
   292
        /**************************************************************
Dean@384
   293
         * 
Dean@384
   294
         * Property Accessors
Dean@384
   295
         * 
Dean@384
   296
         *************************************************************/
Dean@384
   297
Dean@384
   298
        /// <summary>
Dean@384
   299
        /// Gets the list of attachements for this message.
Dean@384
   300
        /// </summary>
Dean@384
   301
        public List<PEPAttachment> Attachments
Dean@384
   302
        {
Dean@384
   303
            get { return (this._Attachments); }
Dean@384
   304
        }
Dean@384
   305
Dean@384
   306
        /// <summary>
Dean@387
   307
        /// Gets the list of identities to be blind carbon copied on the message.
Dean@384
   308
        /// </summary>
Dean@384
   309
        public List<PEPIdentity> BCC
Dean@384
   310
        {
Dean@384
   311
            get { return (this._BCC); }
Dean@384
   312
        }
Dean@384
   313
Dean@384
   314
        /// <summary>
Dean@384
   315
        /// Gets the list of identities to be carbon copied on the message.
Dean@384
   316
        /// </summary>
Dean@384
   317
        public List<PEPIdentity> CC
Dean@384
   318
        {
Dean@384
   319
            get { return (this._CC); }
Dean@384
   320
        }
Dean@384
   321
Dean@384
   322
        /// <summary>
Dean@384
   323
        /// Gets or sets the direction (incoming or outgoing) of the message.
Dean@384
   324
        /// </summary>
Dean@384
   325
        public _pEp_msg_direction Direction
Dean@384
   326
        {
Dean@384
   327
            get { return (this._Direction); }
Dean@384
   328
            set { this._Direction = value; }
Dean@384
   329
        }
Dean@384
   330
Dean@384
   331
        /// <summary>
Dean@384
   332
        /// Gets or sets the from identity of the message.
Dean@384
   333
        /// Warning: this value can be null.
Dean@384
   334
        /// </summary>
Dean@384
   335
        public PEPIdentity From
Dean@384
   336
        {
Dean@384
   337
            get { return (this._From); }
Dean@384
   338
            set { this._From = value; }
Dean@384
   339
        }
Dean@384
   340
Dean@384
   341
        /// <summary>
Dean@384
   342
        /// Gets or sets the ID of the message.
Dean@384
   343
        /// Warning: this value can be null.
Dean@384
   344
        /// </summary>
Dean@384
   345
        public string ID
Dean@384
   346
        {
Dean@384
   347
            get { return (this._ID); }
Dean@384
   348
            set { this._ID = value; }
Dean@384
   349
        }
Dean@384
   350
Dean@384
   351
        /// <summary>
Dean@384
   352
        /// Gets the list of keywords associated with the message.
Dean@384
   353
        /// </summary>
Dean@384
   354
        public List<string> Keywords
Dean@384
   355
        {
Dean@384
   356
            get { return (this._Keywords); }
Dean@384
   357
        }
Dean@384
   358
Dean@384
   359
        /// <summary>
Dean@384
   360
        /// Gets or sets the long-form (body) of the message.
Dean@384
   361
        /// This should be plain text.
Dean@384
   362
        /// Warning: this value can be null.
Dean@384
   363
        /// </summary>
Dean@384
   364
        public string LongMsg
Dean@384
   365
        {
Dean@384
   366
            get { return (this._LongMsg); }
Dean@384
   367
            set { this._LongMsg = value; }
Dean@384
   368
        }
Dean@384
   369
Dean@384
   370
        /// <summary>
Dean@384
   371
        /// Gets or sets the formatted long-form (body) of the message.
Dean@384
   372
        /// This should be HTML.
Dean@384
   373
        /// Warning: this value can be null.
Dean@384
   374
        /// </summary>
Dean@384
   375
        public string LongMsgFormatted
Dean@384
   376
        {
Dean@384
   377
            get { return (this._LongMsgFormatted); }
Dean@384
   378
            set { this._LongMsgFormatted = value; }
Dean@384
   379
        }
Dean@384
   380
Dean@384
   381
        /// <summary>
Dean@384
   382
        /// Gets the list of optional fields associated with the message.
Dean@384
   383
        /// </summary>
Dean@384
   384
        public List<opt_field> OptionalFields
Dean@384
   385
        {
Dean@384
   386
            get { return (this._OptionalFields); }
Dean@384
   387
        }
Dean@384
   388
Dean@384
   389
        /// <summary>
Dean@384
   390
        /// Gets or sets the short-form (subject) of the message.
Dean@384
   391
        /// Warning: this value can be null.
Dean@384
   392
        /// </summary>
Dean@384
   393
        public string ShortMsg
Dean@384
   394
        {
Dean@384
   395
            get { return (this._ShortMsg); }
Dean@384
   396
            set { this._ShortMsg = value; }
Dean@384
   397
        }
Dean@384
   398
Dean@384
   399
        /// <summary>
Dean@384
   400
        /// Gets the list of identities to receive the message.
Dean@384
   401
        /// </summary>
Dean@384
   402
        public List<PEPIdentity> To
Dean@384
   403
        {
Dean@384
   404
            get { return (this._To); }
Dean@384
   405
        }
Dean@384
   406
Dean@384
   407
        /**************************************************************
Dean@384
   408
         * 
Dean@384
   409
         * Methods
Dean@384
   410
         * 
Dean@384
   411
         *************************************************************/
Dean@384
   412
Dean@384
   413
        /// <summary>
Dean@384
   414
        /// Returns this pEp message as a new pEp engine text_message.
Dean@384
   415
        /// </summary>
Dean@384
   416
        /// <returns>A pEp engine identity.</returns>
Dean@384
   417
        public text_message ToCOMType()
Dean@384
   418
        {
Dean@384
   419
            List<blob> attachments = new List<blob>();
Dean@384
   420
            List<pEp_identity_s> bcc = new List<pEp_identity_s>();
Dean@384
   421
            List<pEp_identity_s> cc = new List<pEp_identity_s>();
Dean@384
   422
            List<pEp_identity_s> to = new List<pEp_identity_s>();
Dean@384
   423
            text_message result = new text_message();
Dean@384
   424
Dean@388
   425
            // Convert attachments
Dean@384
   426
            for (int i = 0; i < this._Attachments.Count; i++)
Dean@384
   427
            {
Dean@384
   428
                attachments.Add(this._Attachments[i].ToCOMType());
Dean@384
   429
            }
Dean@388
   430
            
Dean@388
   431
            // Convert BCC
Dean@384
   432
            for (int i = 0; i < this._BCC.Count; i++)
Dean@384
   433
            {
Dean@384
   434
                bcc.Add(this._BCC[i].ToCOMType());
Dean@384
   435
            }
Dean@388
   436
            
Dean@388
   437
            // Convert CC
Dean@384
   438
            for (int i = 0; i < this._CC.Count; i++)
Dean@384
   439
            {
Dean@384
   440
                cc.Add(this._CC[i].ToCOMType());
Dean@384
   441
            }
Dean@388
   442
            
Dean@388
   443
            // Convert To
Dean@388
   444
            for (int i = 0; i < this._To.Count; i++)
Dean@388
   445
            {
Dean@388
   446
                to.Add(this._To[i].ToCOMType());
Dean@388
   447
            }
Dean@388
   448
Dean@388
   449
            // Set properties
Dean@388
   450
            result.attachments = attachments.ToArray();
Dean@388
   451
            result.bcc = bcc.ToArray();
Dean@384
   452
            result.cc = cc.ToArray();
Dean@384
   453
            result.dir = this._Direction;
Dean@384
   454
            result.from = (this._From == null ? new pEp_identity_s() : this._From.ToCOMType());
Dean@384
   455
            result.id = this._ID;
Dean@384
   456
            result.keywords = this._Keywords.ToArray();
Dean@384
   457
            result.longmsg = this._LongMsg;
Dean@384
   458
            result.longmsg_formatted = this._LongMsgFormatted;
Dean@384
   459
            result.opt_fields = this._OptionalFields.ToArray();
Dean@384
   460
            result.shortmsg = this._ShortMsg;
Dean@384
   461
            result.to = to.ToArray();
Dean@384
   462
Dean@384
   463
            return (result);
Dean@384
   464
        }
Dean@384
   465
Dean@384
   466
        /// <summary>
Dean@389
   467
        /// Gets a deep copy of the object and all it's data.
Dean@389
   468
        /// </summary>
Dean@389
   469
        /// <returns>The deep copy of the object.</returns>
Dean@389
   470
        public PEPMessage Copy()
Dean@389
   471
        {
Dean@390
   472
            return (this.Copy(false));
Dean@390
   473
        }
Dean@390
   474
Dean@390
   475
        /// <summary>
Dean@390
   476
        /// Gets a copy of the PEPMessage with or without data.
Dean@390
   477
        /// </summary>
Dean@390
   478
        /// <param name="createWithoutContent">Whether the copy should be created without
Dean@390
   479
        /// text body and attachments.</param>
Dean@390
   480
        /// <returns>The copy of the PEPMessage.</returns>
Dean@390
   481
        public PEPMessage Copy(bool createWithoutContent = false)
Dean@390
   482
        {
Dean@389
   483
            PEPMessage copy = new PEPMessage();
Dean@389
   484
Dean@389
   485
            // Attachments
Dean@389
   486
            copy.Attachments.Clear();
Dean@390
   487
            if (createWithoutContent == false)
Dean@389
   488
            {
Dean@390
   489
                for (int i = 0; i < this._Attachments.Count; i++)
Dean@390
   490
                {
Dean@390
   491
                    copy.Attachments.Add(this._Attachments[i].Copy());
Dean@390
   492
                }
Dean@389
   493
            }
Dean@389
   494
Dean@389
   495
            // BCC
Dean@389
   496
            copy.BCC.Clear();
Dean@389
   497
            for (int i = 0; i < this._BCC.Count; i++)
Dean@389
   498
            {
Dean@389
   499
                copy.BCC.Add(this._BCC[i].Copy());
Dean@389
   500
            }
Dean@389
   501
Dean@389
   502
            // CC
Dean@389
   503
            copy.CC.Clear();
Dean@389
   504
            for (int i = 0; i < this._CC.Count; i++)
Dean@389
   505
            {
Dean@389
   506
                copy.CC.Add(this._CC[i].Copy());
Dean@389
   507
            }
Dean@389
   508
Dean@389
   509
            copy.Direction = this._Direction;
Dean@389
   510
            copy.From = (this._From == null ? null : this._From.Copy());
Dean@389
   511
            copy.ID = this._ID;
Dean@389
   512
Dean@389
   513
            // Keywords
Dean@389
   514
            copy.Keywords.Clear();
Dean@389
   515
            for (int i = 0; i < this._Keywords.Count; i++)
Dean@389
   516
            {
Dean@389
   517
                copy.Keywords.Add(this._Keywords[i]);
Dean@389
   518
            }
Dean@390
   519
            
Dean@390
   520
            // Body
Dean@390
   521
            if (createWithoutContent == false)
Dean@390
   522
            {
Dean@390
   523
                copy.LongMsg = this._LongMsg;
Dean@390
   524
                copy.LongMsgFormatted = this._LongMsgFormatted;
Dean@390
   525
            }
Dean@390
   526
            else
Dean@390
   527
            {
Dean@390
   528
                copy.LongMsg = null;
Dean@390
   529
                copy.LongMsgFormatted = null;
Dean@390
   530
            }
Dean@389
   531
Dean@389
   532
            // OptionalFields
Dean@389
   533
            copy.OptionalFields.Clear();
Dean@389
   534
            for (int i = 0; i < this._OptionalFields.Count; i++)
Dean@389
   535
            {
Dean@389
   536
                copy.OptionalFields.Add(this._OptionalFields[i]);
Dean@389
   537
            }
Dean@389
   538
Dean@389
   539
            copy.ShortMsg = this._ShortMsg;
Dean@389
   540
Dean@389
   541
            // To
Dean@389
   542
            copy.To.Clear();
Dean@389
   543
            for (int i = 0; i < this._To.Count; i++)
Dean@389
   544
            {
Dean@389
   545
                copy.To.Add(this._To[i].Copy());
Dean@389
   546
            }
Dean@389
   547
Dean@389
   548
            return (copy);
Dean@389
   549
        }
Dean@389
   550
Dean@389
   551
        /// <summary>
Dean@384
   552
        /// Applies this pEp message's data to the given Outlook item.
Dean@384
   553
        /// </summary>
Dean@384
   554
        /// <param name="omi">The Outlook mail item to apply this pEp message's data to.</param>
Dean@384
   555
        public void ApplyTo(Outlook.MailItem omi)
Dean@384
   556
        {
Dean@384
   557
            string tempDir;
Dean@384
   558
            string tempFile;
Dean@384
   559
            Outlook.Attachment attachment;
Dean@384
   560
Dean@384
   561
            // Set the subject
Dean@384
   562
            omi.Subject = this._ShortMsg;
Dean@384
   563
Dean@384
   564
            // Set the body
Dean@384
   565
            if (string.IsNullOrWhiteSpace(this._LongMsgFormatted))
Dean@384
   566
            {
Dean@384
   567
                omi.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;
Dean@384
   568
                omi.Body = this._LongMsg;
Dean@384
   569
            }
Dean@384
   570
            else
Dean@384
   571
            {
Dean@384
   572
                omi.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
Dean@384
   573
                omi.HTMLBody = this._LongMsgFormatted;
Dean@384
   574
            }
Dean@384
   575
Dean@384
   576
            // Remove any previous attachments
Dean@384
   577
            while (omi.Attachments.Count > 0)
Dean@384
   578
            {
Dean@384
   579
                omi.Attachments.Remove(1);
Dean@384
   580
            }
Dean@384
   581
Dean@384
   582
            // Create temp directory for attachments
Dean@384
   583
            tempDir = Path.GetTempFileName() + ".dir";
Dean@384
   584
            Directory.CreateDirectory(tempDir);
Dean@384
   585
Dean@384
   586
            // Add new attachments
Dean@384
   587
            for (int i = 0; i < this._Attachments.Count; i++)
Dean@384
   588
            {
Dean@384
   589
                // Write current attachment to disk
Dean@384
   590
                tempFile = tempDir + "\\";
Dean@384
   591
                if (string.IsNullOrWhiteSpace(this._Attachments[i].FileName))
Dean@384
   592
                {
Dean@384
   593
                    tempFile += "attachment" + i.ToString();
Dean@384
   594
                }
Dean@384
   595
                else
Dean@384
   596
                {
Dean@384
   597
                    tempFile += this._Attachments[i].FileName;
Dean@384
   598
                }
Dean@384
   599
                File.WriteAllBytes(tempFile, this._Attachments[i].Data);
Dean@384
   600
Dean@384
   601
                // Create a new attachment from the file on disk
Dean@384
   602
                attachment = omi.Attachments.Add(tempFile);
Dean@384
   603
Dean@384
   604
                if (string.IsNullOrWhiteSpace(this._Attachments[i].MIMEType))
Dean@384
   605
                {
Dean@384
   606
                    // Undefined
Dean@384
   607
                    PEPAttachment.SetMIMEType(attachment, "application/octet-stream");
Dean@384
   608
                }
Dean@384
   609
                else
Dean@384
   610
                {
Dean@384
   611
                    PEPAttachment.SetMIMEType(attachment, this._Attachments[i].MIMEType);
Dean@384
   612
                }
Dean@384
   613
Dean@384
   614
                // Release attachment
Dean@384
   615
                if (attachment != null)
Dean@384
   616
                {
Dean@384
   617
                    Marshal.ReleaseComObject(attachment);
Dean@384
   618
                    attachment = null;
Dean@384
   619
                }
Dean@384
   620
            }
Dean@384
   621
Dean@384
   622
            // Delete temp directory for attachments
Dean@384
   623
            Directory.Delete(tempDir, true);
Dean@384
   624
Dean@384
   625
            // Optional fields
Dean@384
   626
            for (int i = 0; i < this._OptionalFields.Count; i++)
Dean@384
   627
            {
Dean@384
   628
                omi.PropertyAccessor.SetProperty(ThisAddIn.PR_OPT_FIELD + this._OptionalFields[i].name,
Dean@384
   629
                                                 this._OptionalFields[i].value);
Dean@384
   630
            }
Dean@384
   631
Dean@384
   632
            return;
Dean@384
   633
        }
Dean@384
   634
Dean@384
   635
    }
Dean@384
   636
}