public void Base64Converter_Encode_NotInsertNewline() { var bs = new Base64Converter(2000); bs.InsertNewline = false; var bb = bs.Encode(Encoding.GetEncoding(932).GetBytes("<div style='font-size:20px;'>This is a text/html body text.</div>")); var encodedText = Encoding.UTF8.GetString(bb); Assert.AreEqual("PGRpdiBzdHlsZT0nZm9udC1zaXplOjIwcHg7Jz5UaGlzIGlzIGEgdGV4dC9odG1sIGJvZHkgdGV4dC48L2Rpdj4=", encodedText); }
public MimeWriter(Stream stream) { _Stream = stream; _Base64HeaderConverter = new Base64Converter(200); _Base64HeaderConverter.InsertNewline = false; _Base64BodyConverter = new Base64Converter(12000); _Base64BodyConverter.InsertNewline = true; _QuotedPrintableHeaderConverter = new QuotedPrintableConverter(200, QuotedPrintableConvertMode.Header); _QuotedPrintableHeaderConverter.InsertNewline = false; _QuotedPrintableBodyConverter = new QuotedPrintableConverter(2000, QuotedPrintableConvertMode.Default); this.HeaderEncoding = Default.HeaderEncoding; this.HeaderRfc2047Encoding = Default.HeaderRfc2047Encoding; this.FileNameEncoding = Default.FileNameEncoding; this.MimeHeaderParameterEncoding = Default.MimeHeaderParameterEncoding; if (_Initialize != null) { _Initialize(this); } }
public ModifiedUtf7Converter(Int32 bufferSize) { _Base64Converter = new Base64Converter(bufferSize, ','); _Base64Converter.InsertNewline = false; _Base64Converter.Padding = false; }
public ParseContextData(Base64Converter base64Converter, QuotedPrintableConverter quotedPrintableConverter) { _Base64Converter = base64Converter; _QuotedPrintableHeaderConverter = quotedPrintableConverter; }
public Rfc2047Converter() { this.Encoding = Encoding.UTF8; _Base64Converter = new Base64Converter(200); _QuotedPrintableHeaderConverter = new QuotedPrintableConverter(200, QuotedPrintableConvertMode.Header); }
private void WriteSignedData(Stream stream, SmtpMessage message, Byte[] body, Byte[] boundary) { var mg = message; Stream mm = stream; ThrowExceptionIfValueIsNull(mg.From.SigningCertificate, "Can't sign message unless the From property contains a signing certificate."); mm.Write(ByteData.ContentTypeMultipartSignedProtocolApplicationXpkcs7Signature); mm.WriteByte(34);// " mm.Write(boundary); mm.WriteByte(34);// " mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); byte[] signature = Cryptography.GetSignature(body, mg.From.SigningCertificate, mg.From.EncryptionCertificate); Base64Converter converter = new Base64Converter(signature.Length); mm.Write(ByteData.NewLine); mm.Write(boundary); mm.Write(ByteData.NewLine); mm.Write(body); mm.Write(ByteData.NewLine); mm.Write(boundary); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentTypeApplicationXpkcs7Signature); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentTransferEncodingIsBase64); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentDispositionAttachmentFileNameIsSmimeP7s); mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); mm.Write(converter.Encode(signature)); mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); mm.Write(boundary); mm.Write(Encoding.UTF8.GetBytes("--")); mm.Write(ByteData.NewLine); }
private void WriteEncryptedData(Stream stream, SmtpMessage message, Byte[] signedContent) { var mg = message; Stream mm = stream; X509Certificate2Collection encryptionCertificates = new X509Certificate2Collection(); ThrowExceptionIfValueIsNull(mg.From.EncryptionCertificate, "To send an encryted message, the sender must have an encryption certificate specified."); encryptionCertificates.Add(mg.From.EncryptionCertificate); foreach (MailAddress address in mg.To) { ThrowExceptionIfValueIsNull(address.EncryptionCertificate, "To send an encryted message, all receivers( To, CC, and Bcc) must have an encryption certificate specified."); encryptionCertificates.Add(address.EncryptionCertificate); } mm.Write(ByteData.ContentTypeApplicationXpkcs7Mime); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentTransferEncodingIsBase64); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentDispositionAttachmentFileNameIsSmimeP7m); mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); Byte[] encrypted = Cryptography.EncryptMessage(signedContent, encryptionCertificates); var converter = new Base64Converter(encrypted.Length); mm.Write(converter.Encode(encrypted)); mm.Write(ByteData.NewLine); }
private void WriteSignedData(Stream stream, SmtpMessage message, Byte[] body, Byte[] boundary) { var mg = message; Stream mm = stream; ThrowExceptionIfValueIsNull(mg.From.CryptographicKeyInfo.Base64Content, "Can't sign message unless the From property contains a privateKey."); mm.Write(ByteData.ContentTypeMultipartSignedProtocolApplicationXpkcs7Signature); mm.WriteByte(34);// " mm.Write(boundary); mm.WriteByte(34);// " mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); mm.Write(boundary); mm.Write(ByteData.NewLine); mm.Write(body); mm.Write(ByteData.NewLine); mm.Write(boundary); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentTypeApplicationXpkcs7Signature); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentTransferEncodingIsBase64); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentDispositionAttachmentFileNameIsSmimeP7s); mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); var signatureBuffer = Cryptography.ToSignMessage(CryptographicBuffer.CreateFromByteArray(body), mg.From.CryptographicKeyInfo); Byte[] signature; CryptographicBuffer.CopyToByteArray(signatureBuffer, out signature); Base64Converter converter = new Base64Converter(body.Length); mm.Write(converter.Encode(signature)); mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); mm.Write(boundary); mm.Write(Encoding.UTF8.GetBytes("--")); mm.Write(ByteData.NewLine); }
private void Write(Stream stream, SmtpContent content) { var ct = content; ThrowExceptionIfValueIsNull(this.HeaderEncoding, "You must set HeaderEncoding property of SmtpContent object."); ThrowExceptionIfValueIsNull(ct.ContentDisposition, "You must set ContentDisposition property of SmtpContent object."); ThrowExceptionIfValueIsNull(ct.ContentType, "You must set ContentType property of SmtpContent object."); ThrowExceptionIfValueIsNull(ct.ContentType.CharsetEncoding, "You must set CharsetEncoding property of ContentType property of SmtpContent object."); foreach (var header in ct.Headers) { this.WriteHeader(stream, header.Key, header.Value); } if (ct.ContentType.IsMultipart == true && String.IsNullOrEmpty(ct.ContentType.Boundary) == true) { ct.ContentType.Boundary = MimeWriter.GenerateBoundary(); } this.WriteEncodedHeader(stream, ct.ContentType); this.WriteEncodedHeader(stream, ct.ContentDisposition); stream.Write(ByteData.NewLine); if (ct.ContentType.IsMultipart == true) { var boundary = ct.ContentType.CharsetEncoding.GetBytes("--" + ct.ContentType.Boundary + "\r\n"); for (int i = 0; i < ct.Contents.Count; i++) { stream.Write(boundary); Write(stream, ct.Contents[i]); } stream.Write(ct.ContentType.CharsetEncoding.GetBytes("--" + ct.ContentType.Boundary + "--\r\n")); } else { if (ct.ContentType.IsText == true && String.IsNullOrEmpty(ct.BodyText) == false) { this.WriteEncodedBodyText(stream, ct.BodyText, ct.ContentTransferEncoding, ct.ContentType.CharsetEncoding, 74); } else if (ct.BodyData != null) { Byte[] bb = null; switch (ct.ContentTransferEncoding) { case TransferEncoding.Base64: { var converter = new Base64Converter(ct.BodyData.Length); bb = converter.Encode(ct.BodyData); } break; case TransferEncoding.QuotedPrintable: { var converter = new QuotedPrintableConverter(ct.BodyData.Length, QuotedPrintableConvertMode.Default); bb = converter.Encode(ct.BodyData); } break; case TransferEncoding.None: case TransferEncoding.SevenBit: case TransferEncoding.EightBit: case TransferEncoding.Binary: default: throw new InvalidOperationException(); } stream.Write(bb, 0, bb.Length); stream.Write(ByteData.NewLine); } } }
/// <summary> /// Get decoded BodyData by ContentTransferEncoding /// </summary> /// <returns></returns> public Byte[] GetDecodedData() { Byte[] bb = this.BodyData; if (bb == null) throw new InvalidOperationException("BodyData is not loaded.Please ensure to set MimeParser.Filter.LoadContentBodyData = true when you download mail."); if (this.ContentTransferEncoding == TransferEncoding.Base64) { var cv = new Base64Converter(9000); bb = cv.Decode(bb); } else if (this.ContentTransferEncoding == TransferEncoding.QuotedPrintable) { var cv = new QuotedPrintableConverter(9000, QuotedPrintableConvertMode.Default); bb = cv.Decode(bb); } return bb; }