PEPMessage.cs
changeset 543 ba430eec9ea3
parent 518 09e4791fe4c8
child 545 fb6131233988
     1.1 --- a/PEPMessage.cs	Fri Feb 26 16:52:50 2016 +0100
     1.2 +++ b/PEPMessage.cs	Fri Feb 26 17:45:27 2016 +0100
     1.3 @@ -495,152 +495,226 @@
     1.4          {
     1.5              string tempDir;
     1.6              string tempFile;
     1.7 -            Outlook.Attachment attachment;
     1.8 -            Outlook.Recipient newRecipient;
     1.9 +            Outlook.Attachment newAttachment = null;
    1.10 +            Outlook.Attachments attachments = null;
    1.11 +            Outlook.Recipient newRecipient = null;
    1.12 +            Outlook.Recipients recipients = null;
    1.13 +            Outlook.Account currAccount = null;
    1.14 +            Outlook.Account sendUsingAccount = null;
    1.15 +            Outlook.Accounts accounts = null;
    1.16 +            Outlook.PropertyAccessor properties = null;
    1.17  
    1.18 -            // Remove all recipients
    1.19 -            while (omi.Recipients.Count > 0)
    1.20 +            try
    1.21              {
    1.22 -                omi.Recipients.Remove(1);
    1.23 +                // Remove all recipients
    1.24 +                recipients = omi.Recipients;
    1.25 +                while (recipients.Count > 0)
    1.26 +                {
    1.27 +                    recipients.Remove(1);
    1.28 +                }
    1.29 +
    1.30 +                // Set recipients
    1.31 +                for (int i = 0; i < this._BCC.Count; i++)
    1.32 +                {
    1.33 +                    if (this._BCC[i].Address != null)
    1.34 +                    {
    1.35 +                        newRecipient = recipients.Add(this._BCC[i].Address);
    1.36 +                        newRecipient.Type = (int)Outlook.OlMailRecipientType.olBCC;
    1.37 +
    1.38 +                        Marshal.ReleaseComObject(newRecipient);
    1.39 +                        newRecipient = null;
    1.40 +                    }
    1.41 +                }
    1.42 +
    1.43 +                for (int i = 0; i < this._CC.Count; i++)
    1.44 +                {
    1.45 +                    if (this._CC[i].Address != null)
    1.46 +                    {
    1.47 +                        newRecipient = recipients.Add(this._CC[i].Address);
    1.48 +                        newRecipient.Type = (int)Outlook.OlMailRecipientType.olCC;
    1.49 +
    1.50 +                        Marshal.ReleaseComObject(newRecipient);
    1.51 +                        newRecipient = null;
    1.52 +                    }
    1.53 +                }
    1.54 +
    1.55 +                for (int i = 0; i < this._To.Count; i++)
    1.56 +                {
    1.57 +                    if (this._To[i].Address != null)
    1.58 +                    {
    1.59 +                        newRecipient = recipients.Add(this._To[i].Address);
    1.60 +                        newRecipient.Type = (int)Outlook.OlMailRecipientType.olTo;
    1.61 +
    1.62 +                        Marshal.ReleaseComObject(newRecipient);
    1.63 +                        newRecipient = null;
    1.64 +                    }
    1.65 +                }
    1.66 +
    1.67 +                recipients.ResolveAll();
    1.68 +
    1.69 +                /* Set sender
    1.70 +                 * Note that if fails, will be empty which eventually will use the default send account
    1.71 +                 * If the send using account is already populated, this cannot be re-set.
    1.72 +                 * So far this doesn't appear to be an issue as it occurs when applying unencrypted data to a mirror.
    1.73 +                 * However, the mirror SendUsingAccount is already correct at this point and doesn't need to be set.
    1.74 +                 */
    1.75 +                sendUsingAccount = omi.SendUsingAccount;
    1.76 +
    1.77 +                if ((this._From != null) &&
    1.78 +                    (this._From.Address != null) &&
    1.79 +                    (sendUsingAccount == null))
    1.80 +                {
    1.81 +                    accounts = Globals.ThisAddIn.Application.Session.Accounts;
    1.82 +
    1.83 +                    // Note: Index starts at 1
    1.84 +                    for (int i = 1; i <= accounts.Count; i++)
    1.85 +                    {
    1.86 +                        currAccount = accounts[i];
    1.87 +
    1.88 +                        if ((currAccount.SmtpAddress != null) &&
    1.89 +                            (currAccount.SmtpAddress.ToUpper() == this._From.Address.ToUpper()))
    1.90 +                        {
    1.91 +                            /* Try to set the SendUsingAccount
    1.92 +                             * This will fail if the mail item is already marked as sent or the SendUsingAccount is not null, etc...
    1.93 +                             * This property should also ideally be set before a mail item is saved.
    1.94 +                             */
    1.95 +                            try
    1.96 +                            {
    1.97 +                                sendUsingAccount = currAccount;
    1.98 +                            }
    1.99 +                            catch { }
   1.100 +
   1.101 +                            Marshal.ReleaseComObject(currAccount);
   1.102 +                            currAccount = null;
   1.103 +
   1.104 +                            break;
   1.105 +                        }
   1.106 +
   1.107 +                        Marshal.ReleaseComObject(currAccount);
   1.108 +                        currAccount = null;
   1.109 +                    }
   1.110 +                }
   1.111 +
   1.112 +                // Set the subject
   1.113 +                omi.Subject = this._ShortMsg;
   1.114 +
   1.115 +                // Set the body
   1.116 +                if (string.IsNullOrWhiteSpace(this._LongMsgFormatted))
   1.117 +                {
   1.118 +                    omi.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;
   1.119 +                    omi.Body = this._LongMsg;
   1.120 +                }
   1.121 +                else
   1.122 +                {
   1.123 +                    omi.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
   1.124 +                    omi.HTMLBody = this._LongMsgFormatted;
   1.125 +                }
   1.126 +
   1.127 +                // Remove any previous attachments
   1.128 +                attachments = omi.Attachments;
   1.129 +                while (attachments.Count > 0)
   1.130 +                {
   1.131 +                    attachments.Remove(1);
   1.132 +                }
   1.133 +
   1.134 +                // Create temp directory for attachments
   1.135 +                tempDir = Path.GetTempFileName() + ".dir";
   1.136 +                Directory.CreateDirectory(tempDir);
   1.137 +
   1.138 +                // Add new attachments
   1.139 +                for (int i = 0; i < this._Attachments.Count; i++)
   1.140 +                {
   1.141 +                    // Write current attachment to disk
   1.142 +                    tempFile = tempDir + "\\";
   1.143 +                    if (string.IsNullOrWhiteSpace(this._Attachments[i].FileName))
   1.144 +                    {
   1.145 +                        tempFile += "attachment" + i.ToString();
   1.146 +                    }
   1.147 +                    else
   1.148 +                    {
   1.149 +                        tempFile += this._Attachments[i].FileName;
   1.150 +                    }
   1.151 +                    File.WriteAllBytes(tempFile, this._Attachments[i].Data);
   1.152 +
   1.153 +                    // Create a new attachment from the file on disk
   1.154 +                    newAttachment = attachments.Add(tempFile);
   1.155 +
   1.156 +                    if (string.IsNullOrWhiteSpace(this._Attachments[i].MIMEType))
   1.157 +                    {
   1.158 +                        // Undefined
   1.159 +                        PEPAttachment.SetMIMEType(newAttachment, "application/octet-stream");
   1.160 +                    }
   1.161 +                    else
   1.162 +                    {
   1.163 +                        PEPAttachment.SetMIMEType(newAttachment, this._Attachments[i].MIMEType);
   1.164 +                    }
   1.165 +
   1.166 +                    Marshal.ReleaseComObject(newAttachment);
   1.167 +                    newAttachment = null;
   1.168 +                }
   1.169 +
   1.170 +                // Delete temp directory for attachments
   1.171 +                Directory.Delete(tempDir, true);
   1.172 +
   1.173 +                // Optional fields
   1.174 +                properties = omi.PropertyAccessor;
   1.175 +                for (int i = 0; i < this._OptionalFields.Count; i++)
   1.176 +                {
   1.177 +                    properties.SetProperty(ThisAddIn.PR_OPT_FIELD + this._OptionalFields[i].name,
   1.178 +                                           this._OptionalFields[i].value);
   1.179 +                }
   1.180              }
   1.181 +            finally
   1.182 +            {
   1.183 +                if (newAttachment != null)
   1.184 +                {
   1.185 +                    Marshal.ReleaseComObject(newAttachment);
   1.186 +                    newAttachment = null;
   1.187 +                }
   1.188  
   1.189 -            // Set recipients
   1.190 -            for (int i = 0; i < this._BCC.Count; i++)
   1.191 -            {
   1.192 -                if (this._BCC[i].Address != null)
   1.193 +                if (attachments != null)
   1.194                  {
   1.195 -                    newRecipient = omi.Recipients.Add(this._BCC[i].Address);
   1.196 -                    newRecipient.Type = (int)Outlook.OlMailRecipientType.olBCC;
   1.197 +                    Marshal.ReleaseComObject(attachments);
   1.198 +                    attachments = null;
   1.199 +                }
   1.200  
   1.201 +                if (newRecipient != null)
   1.202 +                {
   1.203                      Marshal.ReleaseComObject(newRecipient);
   1.204 +                    newRecipient = null;
   1.205 +                }
   1.206 +
   1.207 +                if (recipients != null)
   1.208 +                {
   1.209 +                    Marshal.ReleaseComObject(recipients);
   1.210 +                    recipients = null;
   1.211 +                }
   1.212 +
   1.213 +                if (currAccount != null)
   1.214 +                {
   1.215 +                    Marshal.ReleaseComObject(currAccount);
   1.216 +                    currAccount = null;
   1.217 +                }
   1.218 +
   1.219 +                if (sendUsingAccount != null)
   1.220 +                {
   1.221 +                    Marshal.ReleaseComObject(sendUsingAccount);
   1.222 +                    sendUsingAccount = null;
   1.223 +                }
   1.224 +
   1.225 +                if (accounts != null)
   1.226 +                {
   1.227 +                    Marshal.ReleaseComObject(accounts);
   1.228 +                    accounts = null;
   1.229 +                }
   1.230 +
   1.231 +                if (properties != null)
   1.232 +                {
   1.233 +                    Marshal.ReleaseComObject(properties);
   1.234 +                    properties = null;
   1.235                  }
   1.236              }
   1.237  
   1.238 -            for (int i = 0; i < this._CC.Count; i++)
   1.239 -            {
   1.240 -                if (this._CC[i].Address != null)
   1.241 -                {
   1.242 -                    newRecipient = omi.Recipients.Add(this._CC[i].Address);
   1.243 -                    newRecipient.Type = (int)Outlook.OlMailRecipientType.olCC;
   1.244 -
   1.245 -                    Marshal.ReleaseComObject(newRecipient);
   1.246 -                }
   1.247 -            }
   1.248 -
   1.249 -            for (int i = 0; i < this._To.Count; i++)
   1.250 -            {
   1.251 -                if (this._To[i].Address != null)
   1.252 -                {
   1.253 -                    newRecipient = omi.Recipients.Add(this._To[i].Address);
   1.254 -                    newRecipient.Type = (int)Outlook.OlMailRecipientType.olTo;
   1.255 -
   1.256 -                    Marshal.ReleaseComObject(newRecipient);
   1.257 -                }
   1.258 -            }
   1.259 -
   1.260 -            omi.Recipients.ResolveAll();
   1.261 -
   1.262 -            /* Set sender
   1.263 -             * Note that if fails, will be empty which eventually will use the default send account
   1.264 -             * If the send using account is already populated, this cannot be re-set.
   1.265 -             * So far this doesn't appear to be an issue as it occurs when applying unencrypted data to a mirror.
   1.266 -             * However, the mirror SendUsingAccount is already correct at this point and doesn't need to be set.
   1.267 -             */
   1.268 -            if ((this._From != null) &&
   1.269 -                (this._From.Address != null) &&
   1.270 -                (omi.SendUsingAccount == null))
   1.271 -            {
   1.272 -                for (int i = 1; i <= Globals.ThisAddIn.Application.Session.Accounts.Count; i++)
   1.273 -                {
   1.274 -                    Outlook.Account currAccount = Globals.ThisAddIn.Application.Session.Accounts[i];
   1.275 -
   1.276 -                    if ((currAccount.SmtpAddress != null) &&
   1.277 -                        (currAccount.SmtpAddress.ToUpper() == this._From.Address.ToUpper()))
   1.278 -                    {
   1.279 -                        omi.SendUsingAccount = currAccount;
   1.280 -
   1.281 -                        Marshal.ReleaseComObject(currAccount);
   1.282 -                        currAccount = null;
   1.283 -
   1.284 -                        break;
   1.285 -                    }
   1.286 -
   1.287 -                    Marshal.ReleaseComObject(currAccount);
   1.288 -                    currAccount = null;
   1.289 -                }
   1.290 -            }
   1.291 -
   1.292 -            // Set the subject
   1.293 -            omi.Subject = this._ShortMsg;
   1.294 -
   1.295 -            // Set the body
   1.296 -            if (string.IsNullOrWhiteSpace(this._LongMsgFormatted))
   1.297 -            {
   1.298 -                omi.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;
   1.299 -                omi.Body = this._LongMsg;
   1.300 -            }
   1.301 -            else
   1.302 -            {
   1.303 -                omi.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
   1.304 -                omi.HTMLBody = this._LongMsgFormatted;
   1.305 -            }
   1.306 -
   1.307 -            // Remove any previous attachments
   1.308 -            while (omi.Attachments.Count > 0)
   1.309 -            {
   1.310 -                omi.Attachments.Remove(1);
   1.311 -            }
   1.312 -
   1.313 -            // Create temp directory for attachments
   1.314 -            tempDir = Path.GetTempFileName() + ".dir";
   1.315 -            Directory.CreateDirectory(tempDir);
   1.316 -
   1.317 -            // Add new attachments
   1.318 -            for (int i = 0; i < this._Attachments.Count; i++)
   1.319 -            {
   1.320 -                // Write current attachment to disk
   1.321 -                tempFile = tempDir + "\\";
   1.322 -                if (string.IsNullOrWhiteSpace(this._Attachments[i].FileName))
   1.323 -                {
   1.324 -                    tempFile += "attachment" + i.ToString();
   1.325 -                }
   1.326 -                else
   1.327 -                {
   1.328 -                    tempFile += this._Attachments[i].FileName;
   1.329 -                }
   1.330 -                File.WriteAllBytes(tempFile, this._Attachments[i].Data);
   1.331 -
   1.332 -                // Create a new attachment from the file on disk
   1.333 -                attachment = omi.Attachments.Add(tempFile);
   1.334 -
   1.335 -                if (string.IsNullOrWhiteSpace(this._Attachments[i].MIMEType))
   1.336 -                {
   1.337 -                    // Undefined
   1.338 -                    PEPAttachment.SetMIMEType(attachment, "application/octet-stream");
   1.339 -                }
   1.340 -                else
   1.341 -                {
   1.342 -                    PEPAttachment.SetMIMEType(attachment, this._Attachments[i].MIMEType);
   1.343 -                }
   1.344 -
   1.345 -                // Release attachment
   1.346 -                if (attachment != null)
   1.347 -                {
   1.348 -                    Marshal.ReleaseComObject(attachment);
   1.349 -                    attachment = null;
   1.350 -                }
   1.351 -            }
   1.352 -
   1.353 -            // Delete temp directory for attachments
   1.354 -            Directory.Delete(tempDir, true);
   1.355 -            
   1.356 -            // Optional fields
   1.357 -            for (int i = 0; i < this._OptionalFields.Count; i++)
   1.358 -            {
   1.359 -                omi.PropertyAccessor.SetProperty(ThisAddIn.PR_OPT_FIELD + this._OptionalFields[i].name,
   1.360 -                                                 this._OptionalFields[i].value);
   1.361 -            }
   1.362 -            
   1.363              return;
   1.364          }
   1.365