internal void SetContentFromString(string contentString, Encoding encoding, string mediaType) { if (contentString == null) { throw new ArgumentNullException("content"); } if (this.part.Stream != null) { this.part.Stream.Close(); } if ((mediaType == null) || (mediaType == string.Empty)) { mediaType = "text/plain"; } int offset = 0; try { if (((MailBnfHelper.ReadToken(mediaType, ref offset, null).Length == 0) || (offset >= mediaType.Length)) || (mediaType[offset++] != '/')) { throw new ArgumentException(SR.GetString("MediaTypeInvalid"), "mediaType"); } if ((MailBnfHelper.ReadToken(mediaType, ref offset, null).Length == 0) || (offset < mediaType.Length)) { throw new ArgumentException(SR.GetString("MediaTypeInvalid"), "mediaType"); } } catch (FormatException) { throw new ArgumentException(SR.GetString("MediaTypeInvalid"), "mediaType"); } ContentType contentType = new ContentType(mediaType); if (encoding == null) { if (MimeBasePart.IsAscii(contentString, false)) { encoding = Encoding.ASCII; } else { encoding = Encoding.GetEncoding("utf-8"); } } contentType.CharSet = encoding.BodyName(); byte[] bytes = encoding.GetBytes(contentString); this.part.SetContent(new MemoryStream(bytes), contentType); if (MimeBasePart.ShouldUseBase64Encoding(encoding)) { this.part.TransferEncoding = TransferEncoding.Base64; } else { this.part.TransferEncoding = TransferEncoding.QuotedPrintable; } }
internal static string EncodeHeaderValue(string value, Encoding encoding, bool base64Encoding) { StringBuilder builder = new StringBuilder(); if ((encoding == null) && IsAscii(value, false)) { return(value); } if (encoding == null) { encoding = Encoding.GetEncoding("utf-8"); } string bodyName = encoding.BodyName(); if (encoding == Encoding.BigEndianUnicode) { bodyName = "utf-16be"; } builder.Append("=?"); builder.Append(bodyName); builder.Append("?"); builder.Append(base64Encoding ? "B" : "Q"); builder.Append("?"); byte[] bytes = encoding.GetBytes(value); if (base64Encoding) { Base64Stream stream = new Base64Stream(-1); stream.EncodeBytes(bytes, 0, bytes.Length, true); builder.Append(Encoding.ASCII.GetString(stream.WriteState.Buffer, 0, stream.WriteState.Length)); } else { QuotedPrintableStream stream2 = new QuotedPrintableStream(-1); stream2.EncodeBytes(bytes, 0, bytes.Length); builder.Append(Encoding.ASCII.GetString(stream2.WriteState.Buffer, 0, stream2.WriteState.Length)); } builder.Append("?="); return(builder.ToString()); }