PEPMessage.cs
author Dean Looyengoed
Thu, 11 Feb 2016 12:54:49 +0100
changeset 413 6a780cc16111
parent 408 6d4dd640f16a
child 416 80729bc5da1b
permissions -rw-r--r--
Move all get from/sender methods to the PEPMessage.
Also standardizes UserID calculation.
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
            List<string> keywordList = new List<string>();
Dean@384
   143
            List<PEPIdentity> toList = new List<PEPIdentity>();
Dean@384
   144
            List<PEPIdentity> ccList = new List<PEPIdentity>();
Dean@384
   145
            List<PEPIdentity> bccList = new List<PEPIdentity>();
Dean@384
   146
            List<PEPAttachment> attachmentList = new List<PEPAttachment>();
Dean@384
   147
            Outlook.Account acct = null;
Dean@413
   148
            
Dean@384
   149
            // Calculate recipients
Dean@384
   150
            foreach (Outlook.Recipient r in omi.Recipients)
Dean@384
   151
            {
Dean@384
   152
                switch ((Outlook.OlMailRecipientType)r.Type)
Dean@384
   153
                {
Dean@384
   154
                    case Outlook.OlMailRecipientType.olTo:
Dean@384
   155
                        toList.Add(PEPIdentity.ToIdentity(r));
Dean@384
   156
                        break;
Dean@384
   157
                    case Outlook.OlMailRecipientType.olCC:
Dean@384
   158
                        ccList.Add(PEPIdentity.ToIdentity(r));
Dean@384
   159
                        break;
Dean@384
   160
                    case Outlook.OlMailRecipientType.olBCC:
Dean@384
   161
                        bccList.Add(PEPIdentity.ToIdentity(r));
Dean@384
   162
                        break;
Dean@384
   163
                }
Dean@384
   164
            }
Dean@384
   165
Dean@384
   166
            // Calculate text body and attachments
Dean@384
   167
            if (createWithoutContent == false)
Dean@384
   168
            {
Dean@384
   169
                // Body
Dean@384
   170
                if (omi.Body != null)
Dean@384
   171
                {
Dean@384
   172
                    bodyLong = omi.Body.Replace("\r\n", "\n");
Dean@384
   173
Dean@384
   174
                    // Force rich text into HTML
Dean@384
   175
                    if (omi.BodyFormat == Outlook.OlBodyFormat.olFormatRichText)
Dean@384
   176
                    {
Dean@384
   177
                        omi.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
Dean@384
   178
                    }
Dean@384
   179
Dean@384
   180
                    if (omi.BodyFormat == Outlook.OlBodyFormat.olFormatHTML)
Dean@384
   181
                    {
Dean@384
   182
                        bodyLongFormatted = omi.HTMLBody;
Dean@384
   183
                    }
Dean@384
   184
                }
Dean@384
   185
Dean@384
   186
                // Attachments
Dean@384
   187
                foreach (Outlook.Attachment a in omi.Attachments)
Dean@384
   188
                {
Dean@384
   189
                    try
Dean@384
   190
                    {
Dean@384
   191
                        attachmentList.Add(new PEPAttachment(a));
Dean@384
   192
                    }
Dean@384
   193
                    catch { }
Dean@384
   194
                }
Dean@384
   195
Dean@384
   196
                // Keywords
Dean@384
   197
                if (omi.Categories != null)
Dean@384
   198
                {
Dean@384
   199
                    using (RegistryKey key1 = Registry.CurrentUser.OpenSubKey("Control Panel\\International"))
Dean@384
   200
                    {
Dean@384
   201
                        delim = key1.GetValue("sList").ToString();
Dean@384
   202
                        keywordList = new List<string>(omi.Categories.Split(delim.ToCharArray()));
Dean@384
   203
                    }
Dean@384
   204
                }
Dean@384
   205
            }
Dean@384
   206
Dean@384
   207
            // Set properties
Dean@384
   208
            this._Attachments = attachmentList;
Dean@384
   209
            this._BCC = bccList;
Dean@384
   210
            this._CC = ccList;
Dean@384
   211
            this._Direction = CryptableMailItem.GetIsIncoming(omi) ? _pEp_msg_direction.pEp_dir_incoming : _pEp_msg_direction.pEp_dir_outgoing;
Dean@413
   212
            this._From = PEPMessage.GetFrom(omi);
Dean@384
   213
            this._ID = CryptableMailItem.GetMessageID(omi);
Dean@384
   214
            this._Keywords = keywordList;
Dean@384
   215
            this._LongMsg = bodyLong;
Dean@384
   216
            this._LongMsgFormatted = bodyLongFormatted;
Dean@384
   217
            this._OptionalFields = new List<opt_field>();
Dean@384
   218
            this._ShortMsg = omi.Subject;
Dean@384
   219
            this._To = toList;
Dean@384
   220
Dean@384
   221
            // Free resources
Dean@384
   222
            if (acct != null)
Dean@384
   223
            {
Dean@384
   224
                Marshal.ReleaseComObject(acct);
Dean@384
   225
                acct = null;
Dean@384
   226
            }
Dean@384
   227
Dean@384
   228
            return;
Dean@384
   229
        }
Dean@384
   230
Dean@384
   231
        /**************************************************************
Dean@384
   232
         * 
Dean@384
   233
         * Property Accessors
Dean@384
   234
         * 
Dean@384
   235
         *************************************************************/
Dean@384
   236
Dean@384
   237
        /// <summary>
Dean@384
   238
        /// Gets the list of attachements for this message.
Dean@384
   239
        /// </summary>
Dean@384
   240
        public List<PEPAttachment> Attachments
Dean@384
   241
        {
Dean@384
   242
            get { return (this._Attachments); }
Dean@384
   243
        }
Dean@384
   244
Dean@384
   245
        /// <summary>
Dean@387
   246
        /// Gets the list of identities to be blind carbon copied on the message.
Dean@384
   247
        /// </summary>
Dean@384
   248
        public List<PEPIdentity> BCC
Dean@384
   249
        {
Dean@384
   250
            get { return (this._BCC); }
Dean@384
   251
        }
Dean@384
   252
Dean@384
   253
        /// <summary>
Dean@384
   254
        /// Gets the list of identities to be carbon copied on the message.
Dean@384
   255
        /// </summary>
Dean@384
   256
        public List<PEPIdentity> CC
Dean@384
   257
        {
Dean@384
   258
            get { return (this._CC); }
Dean@384
   259
        }
Dean@384
   260
Dean@384
   261
        /// <summary>
Dean@384
   262
        /// Gets or sets the direction (incoming or outgoing) of the message.
Dean@384
   263
        /// </summary>
Dean@384
   264
        public _pEp_msg_direction Direction
Dean@384
   265
        {
Dean@384
   266
            get { return (this._Direction); }
Dean@384
   267
            set { this._Direction = value; }
Dean@384
   268
        }
Dean@384
   269
Dean@384
   270
        /// <summary>
Dean@384
   271
        /// Gets or sets the from identity of the message.
Dean@384
   272
        /// Warning: this value can be null.
Dean@384
   273
        /// </summary>
Dean@384
   274
        public PEPIdentity From
Dean@384
   275
        {
Dean@384
   276
            get { return (this._From); }
Dean@384
   277
            set { this._From = value; }
Dean@384
   278
        }
Dean@384
   279
Dean@384
   280
        /// <summary>
Dean@384
   281
        /// Gets or sets the ID of the message.
Dean@384
   282
        /// Warning: this value can be null.
Dean@384
   283
        /// </summary>
Dean@384
   284
        public string ID
Dean@384
   285
        {
Dean@384
   286
            get { return (this._ID); }
Dean@384
   287
            set { this._ID = value; }
Dean@384
   288
        }
Dean@384
   289
Dean@384
   290
        /// <summary>
Dean@384
   291
        /// Gets the list of keywords associated with the message.
Dean@384
   292
        /// </summary>
Dean@384
   293
        public List<string> Keywords
Dean@384
   294
        {
Dean@384
   295
            get { return (this._Keywords); }
Dean@384
   296
        }
Dean@384
   297
Dean@384
   298
        /// <summary>
Dean@384
   299
        /// Gets or sets the long-form (body) of the message.
Dean@384
   300
        /// This should be plain text.
Dean@384
   301
        /// Warning: this value can be null.
Dean@384
   302
        /// </summary>
Dean@384
   303
        public string LongMsg
Dean@384
   304
        {
Dean@384
   305
            get { return (this._LongMsg); }
Dean@384
   306
            set { this._LongMsg = value; }
Dean@384
   307
        }
Dean@384
   308
Dean@384
   309
        /// <summary>
Dean@384
   310
        /// Gets or sets the formatted long-form (body) of the message.
Dean@384
   311
        /// This should be HTML.
Dean@384
   312
        /// Warning: this value can be null.
Dean@384
   313
        /// </summary>
Dean@384
   314
        public string LongMsgFormatted
Dean@384
   315
        {
Dean@384
   316
            get { return (this._LongMsgFormatted); }
Dean@384
   317
            set { this._LongMsgFormatted = value; }
Dean@384
   318
        }
Dean@384
   319
Dean@384
   320
        /// <summary>
Dean@384
   321
        /// Gets the list of optional fields associated with the message.
Dean@384
   322
        /// </summary>
Dean@384
   323
        public List<opt_field> OptionalFields
Dean@384
   324
        {
Dean@384
   325
            get { return (this._OptionalFields); }
Dean@384
   326
        }
Dean@384
   327
Dean@384
   328
        /// <summary>
Dean@384
   329
        /// Gets or sets the short-form (subject) of the message.
Dean@384
   330
        /// Warning: this value can be null.
Dean@384
   331
        /// </summary>
Dean@384
   332
        public string ShortMsg
Dean@384
   333
        {
Dean@384
   334
            get { return (this._ShortMsg); }
Dean@384
   335
            set { this._ShortMsg = value; }
Dean@384
   336
        }
Dean@384
   337
Dean@384
   338
        /// <summary>
Dean@384
   339
        /// Gets the list of identities to receive the message.
Dean@384
   340
        /// </summary>
Dean@384
   341
        public List<PEPIdentity> To
Dean@384
   342
        {
Dean@384
   343
            get { return (this._To); }
Dean@384
   344
        }
Dean@384
   345
Dean@384
   346
        /**************************************************************
Dean@384
   347
         * 
Dean@384
   348
         * Methods
Dean@384
   349
         * 
Dean@384
   350
         *************************************************************/
Dean@384
   351
Dean@384
   352
        /// <summary>
Dean@384
   353
        /// Returns this pEp message as a new pEp engine text_message.
Dean@384
   354
        /// </summary>
Dean@384
   355
        /// <returns>A pEp engine identity.</returns>
Dean@384
   356
        public text_message ToCOMType()
Dean@384
   357
        {
Dean@384
   358
            List<blob> attachments = new List<blob>();
Dean@384
   359
            List<pEp_identity_s> bcc = new List<pEp_identity_s>();
Dean@384
   360
            List<pEp_identity_s> cc = new List<pEp_identity_s>();
Dean@384
   361
            List<pEp_identity_s> to = new List<pEp_identity_s>();
Dean@384
   362
            text_message result = new text_message();
Dean@384
   363
Dean@388
   364
            // Convert attachments
Dean@384
   365
            for (int i = 0; i < this._Attachments.Count; i++)
Dean@384
   366
            {
Dean@384
   367
                attachments.Add(this._Attachments[i].ToCOMType());
Dean@384
   368
            }
Dean@398
   369
Dean@388
   370
            // Convert BCC
Dean@384
   371
            for (int i = 0; i < this._BCC.Count; i++)
Dean@384
   372
            {
Dean@384
   373
                bcc.Add(this._BCC[i].ToCOMType());
Dean@384
   374
            }
Dean@398
   375
Dean@388
   376
            // Convert CC
Dean@384
   377
            for (int i = 0; i < this._CC.Count; i++)
Dean@384
   378
            {
Dean@384
   379
                cc.Add(this._CC[i].ToCOMType());
Dean@384
   380
            }
Dean@398
   381
Dean@388
   382
            // Convert To
Dean@388
   383
            for (int i = 0; i < this._To.Count; i++)
Dean@388
   384
            {
Dean@388
   385
                to.Add(this._To[i].ToCOMType());
Dean@388
   386
            }
Dean@388
   387
Dean@388
   388
            // Set properties
Dean@388
   389
            result.attachments = attachments.ToArray();
Dean@388
   390
            result.bcc = bcc.ToArray();
Dean@384
   391
            result.cc = cc.ToArray();
Dean@384
   392
            result.dir = this._Direction;
Dean@384
   393
            result.from = (this._From == null ? new pEp_identity_s() : this._From.ToCOMType());
Dean@384
   394
            result.id = this._ID;
Dean@384
   395
            result.keywords = this._Keywords.ToArray();
Dean@384
   396
            result.longmsg = this._LongMsg;
Dean@384
   397
            result.longmsg_formatted = this._LongMsgFormatted;
Dean@384
   398
            result.opt_fields = this._OptionalFields.ToArray();
Dean@384
   399
            result.shortmsg = this._ShortMsg;
Dean@384
   400
            result.to = to.ToArray();
Dean@384
   401
Dean@384
   402
            return (result);
Dean@384
   403
        }
Dean@384
   404
Dean@384
   405
        /// <summary>
Dean@389
   406
        /// Gets a deep copy of the object and all it's data.
Dean@389
   407
        /// </summary>
Dean@389
   408
        /// <returns>The deep copy of the object.</returns>
Dean@389
   409
        public PEPMessage Copy()
Dean@389
   410
        {
Dean@390
   411
            return (this.Copy(false));
Dean@390
   412
        }
Dean@390
   413
Dean@390
   414
        /// <summary>
Dean@390
   415
        /// Gets a copy of the PEPMessage with or without data.
Dean@390
   416
        /// </summary>
Dean@390
   417
        /// <param name="createWithoutContent">Whether the copy should be created without
Dean@390
   418
        /// text body and attachments.</param>
Dean@390
   419
        /// <returns>The copy of the PEPMessage.</returns>
Dean@390
   420
        public PEPMessage Copy(bool createWithoutContent = false)
Dean@390
   421
        {
Dean@389
   422
            PEPMessage copy = new PEPMessage();
Dean@389
   423
Dean@389
   424
            // Attachments
Dean@389
   425
            copy.Attachments.Clear();
Dean@390
   426
            if (createWithoutContent == false)
Dean@389
   427
            {
Dean@390
   428
                for (int i = 0; i < this._Attachments.Count; i++)
Dean@390
   429
                {
Dean@390
   430
                    copy.Attachments.Add(this._Attachments[i].Copy());
Dean@390
   431
                }
Dean@389
   432
            }
Dean@389
   433
Dean@389
   434
            // BCC
Dean@389
   435
            copy.BCC.Clear();
Dean@389
   436
            for (int i = 0; i < this._BCC.Count; i++)
Dean@389
   437
            {
Dean@389
   438
                copy.BCC.Add(this._BCC[i].Copy());
Dean@389
   439
            }
Dean@389
   440
Dean@389
   441
            // CC
Dean@389
   442
            copy.CC.Clear();
Dean@389
   443
            for (int i = 0; i < this._CC.Count; i++)
Dean@389
   444
            {
Dean@389
   445
                copy.CC.Add(this._CC[i].Copy());
Dean@389
   446
            }
Dean@389
   447
Dean@389
   448
            copy.Direction = this._Direction;
Dean@389
   449
            copy.From = (this._From == null ? null : this._From.Copy());
Dean@389
   450
            copy.ID = this._ID;
Dean@389
   451
Dean@389
   452
            // Keywords
Dean@389
   453
            copy.Keywords.Clear();
Dean@389
   454
            for (int i = 0; i < this._Keywords.Count; i++)
Dean@389
   455
            {
Dean@389
   456
                copy.Keywords.Add(this._Keywords[i]);
Dean@389
   457
            }
Dean@398
   458
Dean@390
   459
            // Body
Dean@390
   460
            if (createWithoutContent == false)
Dean@390
   461
            {
Dean@390
   462
                copy.LongMsg = this._LongMsg;
Dean@390
   463
                copy.LongMsgFormatted = this._LongMsgFormatted;
Dean@390
   464
            }
Dean@390
   465
            else
Dean@390
   466
            {
Dean@390
   467
                copy.LongMsg = null;
Dean@390
   468
                copy.LongMsgFormatted = null;
Dean@390
   469
            }
Dean@389
   470
Dean@389
   471
            // OptionalFields
Dean@389
   472
            copy.OptionalFields.Clear();
Dean@389
   473
            for (int i = 0; i < this._OptionalFields.Count; i++)
Dean@389
   474
            {
Dean@389
   475
                copy.OptionalFields.Add(this._OptionalFields[i]);
Dean@389
   476
            }
Dean@389
   477
Dean@389
   478
            copy.ShortMsg = this._ShortMsg;
Dean@389
   479
Dean@389
   480
            // To
Dean@389
   481
            copy.To.Clear();
Dean@389
   482
            for (int i = 0; i < this._To.Count; i++)
Dean@389
   483
            {
Dean@389
   484
                copy.To.Add(this._To[i].Copy());
Dean@389
   485
            }
Dean@389
   486
Dean@389
   487
            return (copy);
Dean@389
   488
        }
Dean@389
   489
Dean@389
   490
        /// <summary>
Dean@384
   491
        /// Applies this pEp message's data to the given Outlook item.
Dean@384
   492
        /// </summary>
Dean@384
   493
        /// <param name="omi">The Outlook mail item to apply this pEp message's data to.</param>
Dean@384
   494
        public void ApplyTo(Outlook.MailItem omi)
Dean@384
   495
        {
Dean@384
   496
            string tempDir;
Dean@384
   497
            string tempFile;
Dean@384
   498
            Outlook.Attachment attachment;
Dean@408
   499
            Outlook.Recipient newRecipient;
Dean@408
   500
Dean@408
   501
            // Remove all recipients
Dean@408
   502
            while (omi.Recipients.Count > 0)
Dean@408
   503
            {
Dean@408
   504
                omi.Recipients.Remove(1);
Dean@408
   505
            }
Dean@408
   506
Dean@408
   507
            for (int i = 0; i < this._BCC.Count; i++)
Dean@408
   508
            {
Dean@408
   509
                newRecipient = omi.Recipients.Add(this._BCC[i].Address);
Dean@408
   510
                newRecipient.Type = (int)Outlook.OlMailRecipientType.olBCC;
Dean@408
   511
Dean@408
   512
                Marshal.ReleaseComObject(newRecipient);
Dean@408
   513
            }
Dean@408
   514
Dean@408
   515
            for (int i = 0; i < this._CC.Count; i++)
Dean@408
   516
            {
Dean@408
   517
                newRecipient = omi.Recipients.Add(this._CC[i].Address);
Dean@408
   518
                newRecipient.Type = (int)Outlook.OlMailRecipientType.olCC;
Dean@408
   519
Dean@408
   520
                Marshal.ReleaseComObject(newRecipient);
Dean@408
   521
            }
Dean@408
   522
Dean@408
   523
            for (int i = 0; i < this._To.Count; i++)
Dean@408
   524
            {
Dean@408
   525
                newRecipient = omi.Recipients.Add(this._To[i].Address);
Dean@408
   526
                newRecipient.Type = (int)Outlook.OlMailRecipientType.olTo;
Dean@408
   527
Dean@408
   528
                Marshal.ReleaseComObject(newRecipient);
Dean@408
   529
            }
Dean@408
   530
Dean@408
   531
            omi.Recipients.ResolveAll();
Dean@384
   532
Dean@384
   533
            // Set the subject
Dean@384
   534
            omi.Subject = this._ShortMsg;
Dean@384
   535
Dean@384
   536
            // Set the body
Dean@384
   537
            if (string.IsNullOrWhiteSpace(this._LongMsgFormatted))
Dean@384
   538
            {
Dean@384
   539
                omi.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;
Dean@384
   540
                omi.Body = this._LongMsg;
Dean@384
   541
            }
Dean@384
   542
            else
Dean@384
   543
            {
Dean@384
   544
                omi.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
Dean@384
   545
                omi.HTMLBody = this._LongMsgFormatted;
Dean@384
   546
            }
Dean@384
   547
Dean@384
   548
            // Remove any previous attachments
Dean@384
   549
            while (omi.Attachments.Count > 0)
Dean@384
   550
            {
Dean@384
   551
                omi.Attachments.Remove(1);
Dean@384
   552
            }
Dean@384
   553
Dean@384
   554
            // Create temp directory for attachments
Dean@384
   555
            tempDir = Path.GetTempFileName() + ".dir";
Dean@384
   556
            Directory.CreateDirectory(tempDir);
Dean@384
   557
Dean@384
   558
            // Add new attachments
Dean@384
   559
            for (int i = 0; i < this._Attachments.Count; i++)
Dean@384
   560
            {
Dean@384
   561
                // Write current attachment to disk
Dean@384
   562
                tempFile = tempDir + "\\";
Dean@384
   563
                if (string.IsNullOrWhiteSpace(this._Attachments[i].FileName))
Dean@384
   564
                {
Dean@384
   565
                    tempFile += "attachment" + i.ToString();
Dean@384
   566
                }
Dean@384
   567
                else
Dean@384
   568
                {
Dean@384
   569
                    tempFile += this._Attachments[i].FileName;
Dean@384
   570
                }
Dean@384
   571
                File.WriteAllBytes(tempFile, this._Attachments[i].Data);
Dean@384
   572
Dean@384
   573
                // Create a new attachment from the file on disk
Dean@384
   574
                attachment = omi.Attachments.Add(tempFile);
Dean@384
   575
Dean@384
   576
                if (string.IsNullOrWhiteSpace(this._Attachments[i].MIMEType))
Dean@384
   577
                {
Dean@384
   578
                    // Undefined
Dean@384
   579
                    PEPAttachment.SetMIMEType(attachment, "application/octet-stream");
Dean@384
   580
                }
Dean@384
   581
                else
Dean@384
   582
                {
Dean@384
   583
                    PEPAttachment.SetMIMEType(attachment, this._Attachments[i].MIMEType);
Dean@384
   584
                }
Dean@384
   585
Dean@384
   586
                // Release attachment
Dean@384
   587
                if (attachment != null)
Dean@384
   588
                {
Dean@384
   589
                    Marshal.ReleaseComObject(attachment);
Dean@384
   590
                    attachment = null;
Dean@384
   591
                }
Dean@384
   592
            }
Dean@384
   593
Dean@384
   594
            // Delete temp directory for attachments
Dean@384
   595
            Directory.Delete(tempDir, true);
Dean@384
   596
Dean@384
   597
            // Optional fields
Dean@384
   598
            for (int i = 0; i < this._OptionalFields.Count; i++)
Dean@384
   599
            {
Dean@384
   600
                omi.PropertyAccessor.SetProperty(ThisAddIn.PR_OPT_FIELD + this._OptionalFields[i].name,
Dean@384
   601
                                                 this._OptionalFields[i].value);
Dean@384
   602
            }
Dean@384
   603
Dean@384
   604
            return;
Dean@384
   605
        }
Dean@384
   606
Dean@398
   607
        /// <summary>
Dean@398
   608
        /// Recursivley converts all "BCC", "CC", and "To" identities into a 'flat' list of any members.
Dean@398
   609
        /// This will remove groups (hierarchy) and convert a group into it's members.
Dean@398
   610
        /// </summary>
Dean@398
   611
        public void FlattenAllRecipientIdentities()
Dean@398
   612
        {
Dean@398
   613
            this._BCC = PEPIdentity.ToFlatList(this._BCC);
Dean@398
   614
            this._CC = PEPIdentity.ToFlatList(this._CC);
Dean@398
   615
            this._To = PEPIdentity.ToFlatList(this._To);
Dean@398
   616
Dean@398
   617
            return;
Dean@398
   618
        }
Dean@413
   619
Dean@413
   620
        /**************************************************************
Dean@413
   621
         * 
Dean@413
   622
         * Static Methods
Dean@413
   623
         * 
Dean@413
   624
         *************************************************************/
Dean@413
   625
Dean@413
   626
        /// <summary>
Dean@413
   627
        /// Gets the from/sender address from the given outlook mail item.
Dean@413
   628
        /// Warning: This can return null.
Dean@413
   629
        /// </summary>
Dean@413
   630
        /// <param name="omi">The outlook mail item to get the from/sender address from.</param>
Dean@413
   631
        /// <returns>The from/sender address, otherwise null.</returns>
Dean@413
   632
        public static string GetFromAddress(Outlook.MailItem omi)
Dean@413
   633
        {
Dean@413
   634
            string address = null;
Dean@413
   635
                
Dean@413
   636
            if (CryptableMailItem.GetIsIncoming(omi))
Dean@413
   637
            {
Dean@413
   638
                if ((omi.Sender != null) &&
Dean@413
   639
                    (omi.Sender.Address != null))
Dean@413
   640
                {
Dean@413
   641
                    address = omi.Sender.Address;
Dean@413
   642
                }
Dean@413
   643
                else
Dean@413
   644
                {
Dean@413
   645
                    address = omi.SenderEmailAddress;
Dean@413
   646
                }
Dean@413
   647
            }
Dean@413
   648
            else // Outgoing
Dean@413
   649
            {
Dean@413
   650
                if (omi.SendUsingAccount != null)
Dean@413
   651
                {
Dean@413
   652
                    address = omi.SendUsingAccount.SmtpAddress;
Dean@413
   653
                }
Dean@413
   654
                        
Dean@413
   655
                // Fallback to current user if nothing
Dean@413
   656
                if (string.IsNullOrWhiteSpace(address))
Dean@413
   657
                {
Dean@413
   658
                    try
Dean@413
   659
                    {
Dean@413
   660
                        address = Globals.ThisAddIn.Application.Session.CurrentUser.Address;
Dean@413
   661
                    }
Dean@413
   662
                    catch { }
Dean@413
   663
                }
Dean@413
   664
            }
Dean@413
   665
Dean@413
   666
            return (address);
Dean@413
   667
        }
Dean@413
   668
Dean@413
   669
        /// <summary>
Dean@413
   670
        /// Gets the from/sender user name from the given outlook mail item.
Dean@413
   671
        /// Warning: This can return null.
Dean@413
   672
        /// </summary>
Dean@413
   673
        /// <param name="omi">The outlook mail item to get the from/sender name from.</param>
Dean@413
   674
        /// <returns>The from/sender user name, otherwise null.</returns>
Dean@413
   675
        public static string GetFromUsername(Outlook.MailItem omi)
Dean@413
   676
        {
Dean@413
   677
            string username = null;
Dean@413
   678
Dean@413
   679
            if (CryptableMailItem.GetIsIncoming(omi))
Dean@413
   680
            {
Dean@413
   681
                if (omi.Sender != null &&
Dean@413
   682
                    omi.Sender.Name != null)
Dean@413
   683
                {
Dean@413
   684
                    username = omi.Sender.Name;
Dean@413
   685
                }
Dean@413
   686
                else
Dean@413
   687
                {
Dean@413
   688
                    username = omi.SenderName;
Dean@413
   689
                }
Dean@413
   690
            }
Dean@413
   691
            else // Outgoing
Dean@413
   692
            {
Dean@413
   693
                if (omi.SenderName != null)
Dean@413
   694
                {
Dean@413
   695
                    username = omi.SenderName;
Dean@413
   696
                }
Dean@413
   697
                
Dean@413
   698
                // Fallback to current user if nothing
Dean@413
   699
                if (string.IsNullOrWhiteSpace(username))
Dean@413
   700
                {
Dean@413
   701
                    try
Dean@413
   702
                    {
Dean@413
   703
                        username = Globals.ThisAddIn.Application.Session.CurrentUser.Name;
Dean@413
   704
                    }
Dean@413
   705
                    catch { }
Dean@413
   706
                }
Dean@413
   707
            }
Dean@413
   708
Dean@413
   709
            return (username);
Dean@413
   710
        }
Dean@413
   711
Dean@413
   712
        /// <summary>
Dean@413
   713
        /// Gets the from/sender pEp identity from the given outlook mail item.
Dean@413
   714
        /// </summary>
Dean@413
   715
        /// <param name="omi">The outlook mail item to get the from/sender identity from.</param>
Dean@413
   716
        /// <returns>The from/sender identity.</returns>
Dean@413
   717
        public static PEPIdentity GetFrom(Outlook.MailItem omi)
Dean@413
   718
        {
Dean@413
   719
            PEPIdentity from = new PEPIdentity();
Dean@413
   720
Dean@413
   721
            if (CryptableMailItem.GetIsIncoming(omi))
Dean@413
   722
            {
Dean@413
   723
                if (omi.Sender != null)
Dean@413
   724
                {
Dean@413
   725
                    from = new PEPIdentity();
Dean@413
   726
                    from.Address = omi.Sender.Address;
Dean@413
   727
                    from.Username = omi.Sender.Name;
Dean@413
   728
                    from.UserID = Globals.ThisAddIn.GetUserID(from.Address, from.Username);
Dean@413
   729
                }
Dean@413
   730
                else
Dean@413
   731
                {
Dean@413
   732
                    from = new PEPIdentity();
Dean@413
   733
                    from.Address = omi.SenderEmailAddress;
Dean@413
   734
                    from.Username = omi.SenderName;
Dean@413
   735
                    from.UserID = Globals.ThisAddIn.GetUserID(from.Address, from.Username);
Dean@413
   736
                }
Dean@413
   737
            }
Dean@413
   738
            else // Outgoing
Dean@413
   739
            {
Dean@413
   740
                if (omi.SendUsingAccount != null)
Dean@413
   741
                {
Dean@413
   742
                    from = new PEPIdentity();
Dean@413
   743
                    from.Address = omi.SendUsingAccount.SmtpAddress;
Dean@413
   744
                    from.Username = omi.SendUsingAccount.CurrentUser.Name;
Dean@413
   745
                    from.UserID = Globals.ThisAddIn.GetUserID(from.Address, from.Username);
Dean@413
   746
                }
Dean@413
   747
                else
Dean@413
   748
                {
Dean@413
   749
                    // Fallback to current user
Dean@413
   750
                    if (Globals.ThisAddIn.Application.Session.CurrentUser != null)
Dean@413
   751
                    {
Dean@413
   752
                        from = new PEPIdentity();
Dean@413
   753
                        from.Address = Globals.ThisAddIn.Application.Session.CurrentUser.Address;
Dean@413
   754
                        from.Username = Globals.ThisAddIn.Application.Session.CurrentUser.Name;
Dean@413
   755
                        from.UserID = Globals.ThisAddIn.GetUserID(from.Address, from.Username);
Dean@413
   756
                    }
Dean@413
   757
                }
Dean@413
   758
            }
Dean@413
   759
Dean@413
   760
            return (from);
Dean@413
   761
        }
Dean@384
   762
    }
Dean@384
   763
}