addMessagePart method Null safety

PartBuilder addMessagePart(
  1. MimeMessage mimeMessage,
  2. {ContentDisposition disposition = ContentDisposition.attachment}

Adds the message mimeData as a message/rfc822 content with the given subject as its filename.

Optionally specify the disposition which defaults to ContentDisposition.attachment.


PartBuilder addMessagePart(MimeMessage mimeMessage,
    {ContentDisposition disposition = ContentDisposition.attachment}) {
  // message data can be binary or textual
  // even binary message data should not be base64 encoded, since it has itself encodings etc
  final mediaType = MediaSubtype.messageRfc822.mediaType;
  final subject = mimeMessage.decodeSubject()?.replaceAll('"', r'\"');
  final filename = '${subject ?? ''}.eml';
  final messageText = mimeMessage.renderMessage();
  final partBuilder = addPart(
    mimePart: MimePart()..mimeData = TextMimeData(messageText, false),
    mediaSubtype: MediaSubtype.messageRfc822,
        ContentDispositionHeader.from(disposition, filename: filename),
  if (disposition == ContentDisposition.attachment) {
      AttachmentInfo(null, mediaType, filename, null, disposition,
          utf8.encode(messageText) as Uint8List, partBuilder),
  return partBuilder;