private void Application_ItemSend(object Item, ref bool Cancel)
		{
			var mailItem = Item as Outlook.MailItem;
			if (mailItem == null)
				return;

			OppRibbon currentRibbon = _ribbon;
			if (currentRibbon == null)
				return;

			var mail = mailItem.Body;
			var mailType = mailItem.BodyFormat;
			var needToEncrypt = currentRibbon.EncryptButton.Checked;
			var needToSign = currentRibbon.SignButton.Checked;

			// Early out when we don't need to sign/encrypt
			if (!needToEncrypt && !needToSign)
				return;

			// DEFAULT TO CANCEL
			Cancel = true;

			try
			{
				if (mailType != Outlook.OlBodyFormat.olFormatPlain)
				{
					MessageBox.Show(
						Localized.ErrorInvalidFormat,
						"Invalid Mail Format",
						MessageBoxButtons.OK,
						MessageBoxIcon.Error);

					Cancel = true; // Prevent sending the mail
					return;
				}

				IList<string> recipients = new List<string>();

				if (needToEncrypt)
				{
					// Popup UI to select the encryption targets 
					var mailRecipients = (from Outlook.Recipient mailRecipient in mailItem.Recipients 
										  select GetSmtpAddressForRecipient(mailRecipient)).ToList();

					var recipientDialog = new FormKeySelection(
						mailRecipients,
						GetKeysForEncryption,
						needToEncrypt,
						needToSign);

					recipientDialog.TopMost = true;

					var recipientResult = recipientDialog.DialogResult;

					if(recipientDialog.DialogResult == DialogResult.Ignore)
						recipientResult = recipientDialog.ShowDialog();
					
					if (recipientResult != DialogResult.OK)
					{
						// The user closed the recipient dialog, prevent sending the mail
						Cancel = true;
						return;
					}

					needToEncrypt = recipientDialog.Encrypt;
					needToSign = recipientDialog.Sign;

					foreach (var r in recipientDialog.SelectedKeys)
						recipients.Add(r.Key);

					if (needToEncrypt && recipients.Count == 0)
					{
						MessageBox.Show(
							Localized.ErrorInvalidRecipientKey,
							"Invalid Recipient KeyItem",
							MessageBoxButtons.OK,
							MessageBoxIcon.Error);

						Cancel = true; // Prevent sending the mail
						return;
					}

                    if(_settings.Encrypt2Self)
					    recipients.Add(GetSMTPAddress(mailItem));
				}

				var attachments = new List<Attachment>();

				// Sign and encrypt the plaintext mail
				if ((needToSign) && (needToEncrypt))
				{
					mail = SignAndEncryptEmail(mail, GetSMTPAddress(mailItem), recipients);
					if (mail == null)
						return;

					var mailAttachments = mailItem.Attachments.Cast<Outlook.Attachment>().ToList();

					foreach (var attachment in mailAttachments)
					{
						var a = new Attachment()
						{
							TempFile = Path.Combine(Path.GetTempPath(), attachment.FileName) + ".pgp",
							FileName = attachment.FileName,
							DisplayName = attachment.DisplayName,
							AttachmentType = attachment.Type,
						};

						attachment.SaveAsFile(a.TempFile);
						attachment.Delete();

						// Encrypt file
						var cleartext = File.ReadAllBytes(a.TempFile);
						var cyphertext = SignAndEncryptAttachment(cleartext, GetSMTPAddress(mailItem), recipients);
						File.WriteAllText(a.TempFile, cyphertext);

						a.Encrypted = true;
						attachments.Add(a);
					}
				}
				else if (needToSign)
				{
					// Sign the plaintext mail if needed

					// 1. Verify text lines are not too long
					var mailLines = mail.Split('\n');
					var longLines = mailLines.Any(line => line.Length > 70);
					var wrapLines = false;

					if(longLines)
					{
						var dialog = new FormSelectLineWrap();
						var result = dialog.ShowDialog();
						if (result == DialogResult.Cancel)
							return;

						wrapLines = dialog.radioButtonWrap.Checked;

						if (dialog.radioButtonMime.Checked)
						{
							// todo
						}
						
						if (dialog.radioButtonEdit.Checked)
							return;
					}

					mail = SignEmail(mail, GetSMTPAddress(mailItem), wrapLines);
					if (mail == null)
						return;
				}
				else if (needToEncrypt)
				{
					// Encrypt the plaintext mail if needed
					mail = EncryptEmail(mail, recipients);
					if (mail == null)
						return;

					var mailAttachments = mailItem.Attachments.Cast<Outlook.Attachment>().ToList();

					foreach (var attachment in mailAttachments)
					{
						var a = new Attachment()
						{
							TempFile = Path.Combine(Path.GetTempPath(), attachment.FileName)+ ".pgp",
							FileName = attachment.FileName,
							DisplayName = attachment.DisplayName,
							AttachmentType = attachment.Type
						};

						attachment.SaveAsFile(a.TempFile);
						attachment.Delete();

						// Encrypt file
						var cleartext = File.ReadAllBytes(a.TempFile);
						var cyphertext = EncryptEmail(cleartext, recipients);
						File.WriteAllText(a.TempFile, cyphertext);

						a.Encrypted = true;
						attachments.Add(a);
					}
				}

				foreach (var a in attachments)
				{
					mailItem.Attachments.Add(a.TempFile, a.AttachmentType, 1, a.DisplayName);
				}
			}
			catch (Exception ex)
			{
				if (ex.Message.ToLower().StartsWith("checksum"))
				{
					ClearLastPassword();

					MessageBox.Show(
						Localized.ErrorBadPassphrase,
						Localized.ErrorDialogTitle,
						MessageBoxButtons.OK,
						MessageBoxIcon.Error);
					return;
				}

				WriteErrorData("Application_ItemSend", ex);
				MessageBox.Show(
					ex.Message + "\n"+ex.StackTrace,
					Localized.ErrorDialogTitle,
					MessageBoxButtons.OK,
					MessageBoxIcon.Error);

				// Cancel sending
				return;
			}

			if (mail == null)
				return;

			Cancel = false;
			mailItem.Body = mail;

			currentRibbon.EncryptButton.Checked = false;
			currentRibbon.SignButton.Checked = false;

			SetProperty(mailItem, "GnuPGSetting.Sign", false);
			SetProperty(mailItem, "GnuPGSetting.Encrypt", false);
		}
		private void Application_ItemSend(object Item, ref bool Cancel)
		{
			var mailItem = Item as Outlook.MailItem;
			if (mailItem == null)
				return;

			GnuPGRibbon currentRibbon = ribbon;
			if (currentRibbon == null)
				return;

			string mail = mailItem.Body;
			Outlook.OlBodyFormat mailType = mailItem.BodyFormat;
			bool needToEncrypt = currentRibbon.EncryptButton.Checked;
			bool needToSign = currentRibbon.SignButton.Checked;

			// Early out when we don't need to sign/encrypt
			if (!needToEncrypt && !needToSign)
				return;

			// DEFAULT TO CANCEL
			Cancel = true;

			try
			{
				if (mailType != Outlook.OlBodyFormat.olFormatPlain)
				{
					MessageBox.Show(
						Localized.ErrorInvalidFormat,
						"Invalid Mail Format",
						MessageBoxButtons.OK,
						MessageBoxIcon.Error);

					Cancel = true; // Prevent sending the mail
					return;
				}

				IList<string> recipients = new List<string>();

				if (needToEncrypt)
				{
					// Popup UI to select the encryption targets 
					List<string> mailRecipients = new List<string>();
					foreach (Outlook.Recipient mailRecipient in mailItem.Recipients)
						mailRecipients.Add(GetSmtpAddressForRecipient((Outlook.Recipient)mailRecipient));

					var recipientDialog = new FormKeySelection(
						mailRecipients,
						GetKeysForEncryption,
						needToEncrypt,
						needToSign);

					recipientDialog.TopMost = true;

					DialogResult recipientResult = recipientDialog.DialogResult;

					if(recipientDialog.DialogResult == DialogResult.Ignore)
						recipientResult = recipientDialog.ShowDialog();
					
					if (recipientResult != DialogResult.OK)
					{
						// The user closed the recipient dialog, prevent sending the mail
						Cancel = true;
						return;
					}

					needToEncrypt = recipientDialog.Encrypt;
					needToSign = recipientDialog.Sign;

					foreach (var r in recipientDialog.SelectedKeys)
						recipients.Add(r.Key);

					if (needToEncrypt && recipients.Count == 0)
					{
						MessageBox.Show(
							Localized.ErrorInvalidRecipientKey,
							"Invalid Recipient Key",
							MessageBoxButtons.OK,
							MessageBoxIcon.Error);

						Cancel = true; // Prevent sending the mail
						return;
					}

					recipients.Add(GetSMTPAddress(mailItem));
				}

				var attachments = new List<Attachment>();

				// Sign and encrypt the plaintext mail
				if ((needToSign) && (needToEncrypt))
				{
					mail = SignAndEncryptEmail(mail, GetSMTPAddress(mailItem), recipients);
					if (mail == null)
						return;

					var mailAttachments = new List<Outlook.Attachment>();
					foreach (Outlook.Attachment attachment in mailItem.Attachments)
						mailAttachments.Add(attachment);

					foreach (var attachment in mailAttachments)
					{
						var a = new Attachment();

						a.TempFile = Path.GetTempPath();
						a.FileName = attachment.FileName;
						a.DisplayName = attachment.DisplayName;
						a.AttachmentType = attachment.Type;

						a.TempFile = Path.Combine(a.TempFile, a.FileName);
						a.TempFile = a.TempFile + ".pgp";
						attachment.SaveAsFile(a.TempFile);
						attachment.Delete();

						// Encrypt file
						byte[] cleartext = File.ReadAllBytes(a.TempFile);
						string cyphertext = SignAndEncryptAttachment(cleartext, GetSMTPAddress(mailItem), recipients);
						File.WriteAllText(a.TempFile, cyphertext);

						a.Encrypted = true;
						attachments.Add(a);
					}
				}
				else if (needToSign)
				{
					// Sign the plaintext mail if needed

					// 1. Verify text lines are not too long
					var mailLines = mail.Split('\n');
					var longLines = mailLines.Any(line => line.Length > 70);

					if(longLines)
					{
						var dialog = new FormSelectLineWrap();
						var result = dialog.ShowDialog();
						if (result == DialogResult.Cancel)
							return;

						if (dialog.radioButtonWrap.Checked)
						{
							var lines = new List<string>(mailLines);
							for(int i = 0; i<lines.Count; i++)
							{
								var line = lines[i];
								if(line.Length>70)
								{
									var newLine = line.Substring(70);
									line = line.Substring(0, 70);

									lines[i] = line;
									lines.Insert(i + 1, newLine);
								}
							}

							var sb = new StringBuilder(mail.Length+20);
							foreach (var line in lines)
								sb.AppendLine(line.TrimEnd('\r','\n'));

							mail = sb.ToString();
						}
						if (dialog.radioButtonMime.Checked)
						{
							// todo
						}
						if (dialog.radioButtonEdit.Checked)
							return;
					}

					mail = SignEmail(mail, GetSMTPAddress(mailItem));
					if (mail == null)
						return;
				}
				else if (needToEncrypt)
				{
					// Encrypt the plaintext mail if needed
					mail = EncryptEmail(mail, recipients);
					if (mail == null)
						return;

					var mailAttachments = new List<Outlook.Attachment>();
					foreach (Outlook.Attachment attachment in mailItem.Attachments)
						mailAttachments.Add(attachment);

					foreach (var attachment in mailAttachments)
					{
						var a = new Attachment();

						a.TempFile = Path.GetTempPath();
						a.FileName = attachment.FileName;
						a.DisplayName = attachment.DisplayName;
						a.AttachmentType = attachment.Type;

						a.TempFile = Path.Combine(a.TempFile, a.FileName);
						a.TempFile = a.TempFile + ".pgp";
						attachment.SaveAsFile(a.TempFile);
						attachment.Delete();

						// Encrypt file
						byte[] cleartext = File.ReadAllBytes(a.TempFile);
						string cyphertext = EncryptEmail(cleartext, recipients);
						File.WriteAllText(a.TempFile, cyphertext);

						a.Encrypted = true;
						attachments.Add(a);
					}
				}

				foreach (var a in attachments)
				{
					mailItem.Attachments.Add(a.TempFile, a.AttachmentType, 1, a.DisplayName);
				}
			}
			catch (Exception ex)
			{
				if (ex.Message.ToLower().StartsWith("checksum"))
				{
					MessageBox.Show(
						Localized.ErrorBadPassphrase,
						"Outlook Privacy Error",
						MessageBoxButtons.OK,
						MessageBoxIcon.Error);
					return;
				}

				WriteErrorData("Application_ItemSend", ex);
				MessageBox.Show(
					ex.Message,
					"Outlook Privacy Error",
					MessageBoxButtons.OK,
					MessageBoxIcon.Error);

				// Cancel sending
				return;
			}

			if (mail == null)
				return;

			Cancel = false;
			mailItem.Body = mail;

			currentRibbon.EncryptButton.Checked = false;
			currentRibbon.SignButton.Checked = false;

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