/// <summary>
		/// WrapperEvent fired when a mailItem is opened.
		/// This handler is designed to initialize the state of the compose button
		/// states (Sign/Encrypt) with recorded values, if present, or with default
		/// settings values.
		/// </summary>
		/// <param name="mailItem">the opened mailItem</param>
		/// <param name="cancel">False when the event occurs. If the event procedure sets this argument to True,
		/// the open operation is not completed and the inspector is not displayed.</param>
		void mailItem_Open(Outlook.MailItem mailItem, ref bool cancel)
		{
			if (mailItem == null)
				return;

			SetProperty(mailItem, "GnuPGSetting.Sign", false);
			SetProperty(mailItem, "GnuPGSetting.Encrypt", false);

			// New mail (Compose)
			if (!mailItem.Sent)
			{
				_ribbon.SignButton.Checked = _settings.AutoSign;
				_ribbon.EncryptButton.Checked = _settings.AutoEncrypt;

				if (mailItem.Body.Contains("\n** Message decrypted. Valid signature"))
				{
					_ribbon.SignButton.Checked = true;
					_ribbon.EncryptButton.Checked = true;
				}
				else if (mailItem.Body.Contains("\n** Message decrypted."))
				{
					_ribbon.EncryptButton.Checked = true;
				}

				SetProperty(mailItem, "GnuPGSetting.Sign", _ribbon.SignButton.Checked);
				SetProperty(mailItem, "GnuPGSetting.Encrypt", _ribbon.EncryptButton.Checked);

				_ribbon.InvalidateButtons();

				if (_ribbon.EncryptButton.Checked || _ribbon.SignButton.Checked)
					mailItem.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;
			}
			else
			// Read mail
			{
				// Look for PGP headers
				Match match = null;
				if (mailItem.Body != null)
					match = Regex.Match(mailItem.Body, PgpHeaderPattern);

				if (match != null && (_autoDecrypt || _settings.AutoDecrypt) && match.Value == PgpEncryptedHeader)
				{
					if (mailItem.BodyFormat != Outlook.OlBodyFormat.olFormatPlain)
					{
						var body = new StringBuilder(mailItem.Body);

						RemovePgpHeader(body);

						mailItem.Body = body.ToString();
					}

					_autoDecrypt = false;
					DecryptEmail(mailItem);
					// Update match again, in case decryption failed/cancelled.
					match = Regex.Match(mailItem.Body, PgpHeaderPattern);

					SetProperty(mailItem, "GnuPGSetting.Decrypted", true);
				}
				else if (match != null && _settings.AutoVerify && match.Value == PgpSignedHeader)
				{
					if (mailItem.BodyFormat != Outlook.OlBodyFormat.olFormatPlain)
					{
						var body = new StringBuilder(mailItem.Body);
						mailItem.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;

						RemovePgpHeader(body);

						mailItem.Body = body.ToString();
					}

					VerifyEmail(mailItem);
				}
				else
				{
					HandleMailWithoutPgpBody(mailItem);
				}
			}

			_ribbon.InvalidateButtons();
		}