private void CleanupCurrentBodyPart() { if (_currentBodyPart != null) { _currentBodyPart.Dispose(); _currentBodyPart = null; } }
private void CleanupCurrentBodyPart() { if (this._currentBodyPart != null) { this._currentBodyPart.Dispose(); this._currentBodyPart = null; } }
/// <summary> /// Initializes a new instance of the <see cref="MimeMultipartBodyPartParser"/> class. /// </summary> /// <param name="content">An existing <see cref="HttpContent"/> instance to use for the object's content.</param> /// <param name="streamProvider">A stream provider providing output streams for where to write body parts as they are parsed.</param> /// <param name="maxMessageSize">The max length of the entire MIME multipart message.</param> /// <param name="maxBodyPartHeaderSize">The max length of the MIME header within each MIME body part.</param> public MimeMultipartBodyPartParser( HttpContent content, IMultipartStreamProvider streamProvider, long maxMessageSize, int maxBodyPartHeaderSize) { Contract.Assert(content != null, "content cannot be null."); Contract.Assert(streamProvider != null, "streamProvider cannot be null."); string boundary = ValidateArguments(content, maxMessageSize, true); _mimeParser = new MimeMultipartParser(boundary, maxMessageSize); _currentBodyPart = new MimeBodyPart(streamProvider, maxBodyPartHeaderSize); _maxBodyPartHeaderSize = maxBodyPartHeaderSize; _streamProvider = streamProvider; }
/// <summary> /// Initializes a new instance of the <see cref="MimeMultipartBodyPartParser"/> class. /// </summary> /// <param name="content">An existing <see cref="HttpContent"/> instance to use for the object's content.</param> /// <param name="streamProvider">A stream provider providing output streams for where to write body parts as they are parsed.</param> /// <param name="maxMessageSize">The max length of the entire MIME multipart message.</param> /// <param name="maxBodyPartHeaderSize">The max length of the MIME header within each MIME body part.</param> public MimeMultipartBodyPartParser( HttpContent content, MultipartStreamProvider streamProvider, long maxMessageSize, int maxBodyPartHeaderSize) { Contract.Assert(content != null, "content cannot be null."); Contract.Assert(streamProvider != null, "streamProvider cannot be null."); string boundary = ValidateArguments(content, maxMessageSize, true); _mimeParser = new MimeMultipartParser(boundary, maxMessageSize); _currentBodyPart = new MimeBodyPart(streamProvider, maxBodyPartHeaderSize, content); _content = content; _maxBodyPartHeaderSize = maxBodyPartHeaderSize; _streamProvider = streamProvider; }
/// <summary> /// 以HTML格式发送邮件 /// </summary> /// <param name="mailInfo"> 待发送的邮件信息 </param> public static bool sendHtmlMail(MailSenderInfo mailInfo) { try { LOG.info("send to " + mailInfo.FromAddress); // 设置一些通用的数据 Message mailMessage = setCommon(mailInfo); // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象 Multipart mainPart = new MimeMultipart(); // 创建一个包含HTML内容的MimeBodyPart BodyPart html = new MimeBodyPart(); // 设置HTML内容 html.setContent(mailInfo.Content, "text/html; charset=utf-8"); mainPart.addBodyPart(html); // 将MiniMultipart对象设置为邮件内容 mailMessage.Content = mainPart; // 发送邮件 Transport.send(mailMessage); LOG.info("send to " + mailInfo.FromAddress + " dnoe."); return true; } catch (MessagingException ex) { LOG.error(ex.ToString(), ex); } return false; }
/// <summary> /// Parses the data provided and generates parsed MIME body part bodies in the form of <see cref="ArraySegment{T}"/> which are ready to /// write to the output stream. /// </summary> /// <param name="data">The data to parse</param> /// <param name="bytesRead">The number of bytes available in the input data</param> /// <returns>Parsed <see cref="MimeBodyPart"/> instances.</returns> public IEnumerable<MimeBodyPart> ParseBuffer(byte[] data, int bytesRead) { int bytesConsumed = 0; bool isFinal = false; if (bytesRead == 0) { CleanupCurrentBodyPart(); throw new IOException(Properties.Resources.ReadAsMimeMultipartUnexpectedTermination); } // Make sure we remove an old array segments. _currentBodyPart.Segments.Clear(); while (bytesConsumed < bytesRead) { _mimeStatus = _mimeParser.ParseBuffer(data, bytesRead, ref bytesConsumed, out _parsedBodyPart[0], out _parsedBodyPart[1], out isFinal); if (_mimeStatus != MimeMultipartParser.State.BodyPartCompleted && _mimeStatus != MimeMultipartParser.State.NeedMoreData) { CleanupCurrentBodyPart(); throw new IOException(RS.Format(Properties.Resources.ReadAsMimeMultipartParseError, bytesConsumed, data)); } // First body is empty preamble which we just ignore if (_isFirst) { if (_mimeStatus == MimeMultipartParser.State.BodyPartCompleted) { _isFirst = false; } continue; } // Parse the two array segments containing parsed body parts that the MIME parser gave us foreach (ArraySegment<byte> part in _parsedBodyPart) { if (part.Count == 0) { continue; } if (_bodyPartHeaderStatus != ParserState.Done) { int headerConsumed = part.Offset; _bodyPartHeaderStatus = _currentBodyPart.HeaderParser.ParseBuffer(part.Array, part.Count + part.Offset, ref headerConsumed); if (_bodyPartHeaderStatus == ParserState.Done) { // Add the remainder as body part content _currentBodyPart.Segments.Add(new ArraySegment<byte>(part.Array, headerConsumed, part.Count + part.Offset - headerConsumed)); } else if (_bodyPartHeaderStatus != ParserState.NeedMoreData) { CleanupCurrentBodyPart(); throw new IOException(RS.Format(Properties.Resources.ReadAsMimeMultipartHeaderParseError, headerConsumed, part.Array)); } } else { // Add the data as body part content _currentBodyPart.Segments.Add(part); } } if (_mimeStatus == MimeMultipartParser.State.BodyPartCompleted) { // If body is completed then swap current body part MimeBodyPart completed = _currentBodyPart; completed.IsComplete = true; completed.IsFinal = isFinal; _currentBodyPart = new MimeBodyPart(_streamProvider, _maxBodyPartHeaderSize); _mimeStatus = MimeMultipartParser.State.NeedMoreData; _bodyPartHeaderStatus = ParserState.NeedMoreData; yield return completed; } else { // Otherwise return what we have yield return _currentBodyPart; } } }
/// <summary> /// Parses the data provided and generates parsed MIME body part bodies in the form of <see cref="ArraySegment{T}"/> which are ready to /// write to the output stream. /// </summary> /// <param name="data">The data to parse</param> /// <param name="bytesRead">The number of bytes available in the input data</param> /// <returns>Parsed <see cref="MimeBodyPart"/> instances.</returns> public IEnumerable <MimeBodyPart> ParseBuffer(byte[] data, int bytesRead) { int bytesConsumed = 0; bool isFinal = false; // There's a special case here - if we've reached the end of the message and there's no optional // CRLF, then we're out of bytes to read, but we have finished the message. // // If IsWaitingForEndOfMessage is true and we're at the end of the stream, then we're going to // call into the parser again with an empty array as the buffer to signal the end of the parse. // Then the final boundary segment will be marked as complete. if (bytesRead == 0 && !_mimeParser.IsWaitingForEndOfMessage) { CleanupCurrentBodyPart(); throw new IOException(Properties.Resources.ReadAsMimeMultipartUnexpectedTermination); } // Make sure we remove an old array segments. _currentBodyPart.Segments.Clear(); while (_mimeParser.CanParseMore(bytesRead, bytesConsumed)) { _mimeStatus = _mimeParser.ParseBuffer(data, bytesRead, ref bytesConsumed, out _parsedBodyPart[0], out _parsedBodyPart[1], out isFinal); if (_mimeStatus != MimeMultipartParser.State.BodyPartCompleted && _mimeStatus != MimeMultipartParser.State.NeedMoreData) { CleanupCurrentBodyPart(); throw Error.InvalidOperation(Properties.Resources.ReadAsMimeMultipartParseError, bytesConsumed, data); } // First body is empty preamble which we just ignore if (_isFirst) { if (_mimeStatus == MimeMultipartParser.State.BodyPartCompleted) { _isFirst = false; } continue; } // Parse the two array segments containing parsed body parts that the MIME parser gave us foreach (ArraySegment <byte> part in _parsedBodyPart) { if (part.Count == 0) { continue; } if (_bodyPartHeaderStatus != ParserState.Done) { int headerConsumed = part.Offset; _bodyPartHeaderStatus = _currentBodyPart.HeaderParser.ParseBuffer(part.Array, part.Count + part.Offset, ref headerConsumed); if (_bodyPartHeaderStatus == ParserState.Done) { // Add the remainder as body part content _currentBodyPart.Segments.Add(new ArraySegment <byte>(part.Array, headerConsumed, part.Count + part.Offset - headerConsumed)); } else if (_bodyPartHeaderStatus != ParserState.NeedMoreData) { CleanupCurrentBodyPart(); throw Error.InvalidOperation(Properties.Resources.ReadAsMimeMultipartHeaderParseError, headerConsumed, part.Array); } } else { // Add the data as body part content _currentBodyPart.Segments.Add(part); } } if (_mimeStatus == MimeMultipartParser.State.BodyPartCompleted) { // If body is completed then swap current body part MimeBodyPart completed = _currentBodyPart; completed.IsComplete = true; completed.IsFinal = isFinal; _currentBodyPart = new MimeBodyPart(_streamProvider, _maxBodyPartHeaderSize, _content); _mimeStatus = MimeMultipartParser.State.NeedMoreData; _bodyPartHeaderStatus = ParserState.NeedMoreData; yield return(completed); } else { // Otherwise return what we have yield return(_currentBodyPart); } } }
/// <summary> /// Parses the data provided and generates parsed MIME body part bodies in the form of <see cref="ArraySegment{T}"/> which are ready to /// write to the output stream. /// </summary> /// <param name="data">The data to parse</param> /// <param name="bytesRead">The number of bytes available in the input data</param> /// <returns>Parsed <see cref="MimeBodyPart"/> instances.</returns> public IEnumerable <MimeBodyPart> ParseBuffer(byte[] data, int bytesRead) { int bytesConsumed = 0; bool isFinal = false; if (bytesRead == 0) { CleanupCurrentBodyPart(); throw new IOException(Properties.Resources.ReadAsMimeMultipartUnexpectedTermination); } // Make sure we remove an old array segments. _currentBodyPart.Segments.Clear(); while (bytesConsumed < bytesRead) { _mimeStatus = _mimeParser.ParseBuffer(data, bytesRead, ref bytesConsumed, out _parsedBodyPart[0], out _parsedBodyPart[1], out isFinal); if (_mimeStatus != MimeMultipartParser.State.BodyPartCompleted && _mimeStatus != MimeMultipartParser.State.NeedMoreData) { CleanupCurrentBodyPart(); throw new IOException(RS.Format(Properties.Resources.ReadAsMimeMultipartParseError, bytesConsumed, data)); } // First body is empty preamble which we just ignore if (_isFirst) { if (_mimeStatus == MimeMultipartParser.State.BodyPartCompleted) { _isFirst = false; } continue; } // Parse the two array segments containing parsed body parts that the MIME parser gave us foreach (ArraySegment <byte> part in _parsedBodyPart) { if (part.Count == 0) { continue; } if (_bodyPartHeaderStatus != ParserState.Done) { int headerConsumed = part.Offset; _bodyPartHeaderStatus = _currentBodyPart.HeaderParser.ParseBuffer(part.Array, part.Count + part.Offset, ref headerConsumed); if (_bodyPartHeaderStatus == ParserState.Done) { // Add the remainder as body part content _currentBodyPart.Segments.Add(new ArraySegment <byte>(part.Array, headerConsumed, part.Count + part.Offset - headerConsumed)); } else if (_bodyPartHeaderStatus != ParserState.NeedMoreData) { CleanupCurrentBodyPart(); throw new IOException(RS.Format(Properties.Resources.ReadAsMimeMultipartHeaderParseError, headerConsumed, part.Array)); } } else { // Add the data as body part content _currentBodyPart.Segments.Add(part); } } if (_mimeStatus == MimeMultipartParser.State.BodyPartCompleted) { // If body is completed then swap current body part MimeBodyPart completed = _currentBodyPart; completed.IsComplete = true; completed.IsFinal = isFinal; _currentBodyPart = new MimeBodyPart(_streamProvider, _maxBodyPartHeaderSize); _mimeStatus = MimeMultipartParser.State.NeedMoreData; _bodyPartHeaderStatus = ParserState.NeedMoreData; yield return(completed); } else { // Otherwise return what we have yield return(_currentBodyPart); } } }
/// <summary> /// Parses the data provided and generates parsed MIME body part bodies in the form of <see cref="ArraySegment{T}"/> which are ready to /// write to the output stream. /// </summary> /// <param name="data">The data to parse</param> /// <param name="bytesRead">The number of bytes available in the input data</param> /// <returns>Parsed <see cref="MimeBodyPart"/> instances.</returns> public IEnumerable<MimeBodyPart> ParseBuffer(byte[] data, int bytesRead) { int bytesConsumed = 0; bool isFinal = false; // There's a special case here - if we've reached the end of the message and there's no optional // CRLF, then we're out of bytes to read, but we have finished the message. // // If IsWaitingForEndOfMessage is true and we're at the end of the stream, then we're going to // call into the parser again with an empty array as the buffer to signal the end of the parse. // Then the final boundary segment will be marked as complete. if (bytesRead == 0 && !_mimeParser.IsWaitingForEndOfMessage) { CleanupCurrentBodyPart(); throw new IOException(Properties.Resources.ReadAsMimeMultipartUnexpectedTermination); } // Make sure we remove an old array segments. _currentBodyPart.Segments.Clear(); while (_mimeParser.CanParseMore(bytesRead, bytesConsumed)) { _mimeStatus = _mimeParser.ParseBuffer(data, bytesRead, ref bytesConsumed, out _parsedBodyPart[0], out _parsedBodyPart[1], out isFinal); if (_mimeStatus != MimeMultipartParser.State.BodyPartCompleted && _mimeStatus != MimeMultipartParser.State.NeedMoreData) { CleanupCurrentBodyPart(); throw Error.InvalidOperation(Properties.Resources.ReadAsMimeMultipartParseError, bytesConsumed, data); } // First body is empty preamble which we just ignore if (_isFirst) { if (_mimeStatus == MimeMultipartParser.State.BodyPartCompleted) { _isFirst = false; } continue; } // Parse the two array segments containing parsed body parts that the MIME parser gave us foreach (ArraySegment<byte> part in _parsedBodyPart) { if (part.Count == 0) { continue; } if (_bodyPartHeaderStatus != ParserState.Done) { int headerConsumed = part.Offset; _bodyPartHeaderStatus = _currentBodyPart.HeaderParser.ParseBuffer(part.Array, part.Count + part.Offset, ref headerConsumed); if (_bodyPartHeaderStatus == ParserState.Done) { // Add the remainder as body part content _currentBodyPart.Segments.Add(new ArraySegment<byte>(part.Array, headerConsumed, part.Count + part.Offset - headerConsumed)); } else if (_bodyPartHeaderStatus != ParserState.NeedMoreData) { CleanupCurrentBodyPart(); throw Error.InvalidOperation(Properties.Resources.ReadAsMimeMultipartHeaderParseError, headerConsumed, part.Array); } } else { // Add the data as body part content _currentBodyPart.Segments.Add(part); } } if (_mimeStatus == MimeMultipartParser.State.BodyPartCompleted) { // If body is completed then swap current body part MimeBodyPart completed = _currentBodyPart; completed.IsComplete = true; completed.IsFinal = isFinal; _currentBodyPart = new MimeBodyPart(_streamProvider, _maxBodyPartHeaderSize, _content); _mimeStatus = MimeMultipartParser.State.NeedMoreData; _bodyPartHeaderStatus = ParserState.NeedMoreData; yield return completed; } else { // Otherwise return what we have yield return _currentBodyPart; } } }