private Multipart Encrypt(Multipart input, GpgPublicKeyInfo recipientKey) { var gpgMultipart = new Multipart("encrypted"); gpgMultipart.ContentType.Parameters.Add("protocol", "application/pgp-encrypted"); var versionPart = new TextPart("pgp-encrypted"); versionPart.ContentType.MediaType = "application"; versionPart.Headers.Add(new Header("Content-Description", "PGP/MIME version identification")); versionPart.Text = "Version: 1"; gpgMultipart.Add(input); var multipartStream = new MemoryStream(); input.WriteTo(multipartStream); multipartStream.Position = 0; var plainText = Encoding.UTF8.GetString(multipartStream.ToArray()).Replace("\n", "\r\n"); _gpg.Encrypt(plainText, out string cipherText, recipientKey.Id, true); var encryptedPart = new TextPart("octet-stream"); encryptedPart.ContentType.MediaType = "application"; encryptedPart.ContentType.Name = "encrypted.asc"; encryptedPart.ContentDisposition = new ContentDisposition("inline"); encryptedPart.ContentDisposition.FileName = "encrypted.asc"; encryptedPart.Text = cipherText; encryptedPart.ContentTransferEncoding = ContentEncoding.SevenBit; encryptedPart.Headers.Remove(HeaderId.ContentTransferEncoding); gpgMultipart.Add(encryptedPart); return(gpgMultipart); }
public void Send(InternetAddress from, InternetAddress to, GpgPrivateKeyInfo senderKey, GpgPublicKeyInfo recipientKey, string subject, Multipart content) { _log.Verbose("Sending message to {0}", to); try { var client = new SmtpClient(); client.SslProtocols = System.Security.Authentication.SslProtocols.None; client.Connect(_config.MailServerHost, _config.MailServerPort); client.Authenticate(_config.MailAccountName, _config.MailAccountPassword); _log.Verbose("Connected to mail server {0}:{1}", _config.MailServerHost, _config.MailServerPort); if (senderKey != null && recipientKey != null) { content = EncryptAndSign(content, senderKey, recipientKey); } else if (recipientKey != null) { content = Encrypt(content, senderKey); } else if (senderKey != null) { content = Sign(content, senderKey); } var message = new MimeMessage(); message.From.Add(from); message.To.Add(to); message.Subject = subject; message.Body = content; client.Send(message); _log.Info("Message sent to {0}", to); } catch (Exception exception) { _log.Error("Error sending mail to {0}", to); _log.Error(exception.ToString()); throw exception; } }
public void Send(InternetAddress to, GpgPrivateKeyInfo senderKey, GpgPublicKeyInfo recipientKey, string subject, Multipart content) { Send(new MailboxAddress(Global.Config.SystemMailAddress), to, senderKey, recipientKey, subject, content); }