buildMimeMessage method Null safety

MimeMessage buildMimeMessage()

Creates the mime message based on the previous input.

Implementation

MimeMessage buildMimeMessage() {
  // there are not mandatory fields required in case only a Draft message should be stored, for example

  // set default values for standard headers:
  date ??= DateTime.now();
  messageId ??= createMessageId(
      (from?.isEmpty ?? true) ? 'enough.de' : from!.first.hostName,
      isChat: isChat,
      chatGroupId: chatGroupId);
  if (subject == null && originalMessage != null) {
    final originalSubject = originalMessage!.decodeSubject();
    if (originalSubject != null) {
      subject = createReplySubject(originalSubject);
    }
  }
  if (from != null) {
    setMailAddressHeader('From', from!);
  }
  if (sender != null) {
    setMailAddressHeader('Sender', [sender!]);
  }
  var addresses = to;
  if (addresses != null && addresses.isNotEmpty) {
    setMailAddressHeader('To', addresses);
  }
  addresses = cc;
  if (addresses != null && addresses.isNotEmpty) {
    setMailAddressHeader('Cc', addresses);
  }
  addresses = bcc;
  if (addresses != null && addresses.isNotEmpty) {
    setMailAddressHeader('Bcc', addresses);
  }
  setHeader('Date', DateCodec.encodeDate(date!));
  setHeader('Message-Id', messageId);
  if (isChat) {
    setHeader('Chat-Version', '1.0');
  }
  if (subject != null) {
    setHeader('Subject', subject, encoding: subjectEncoding);
  }
  setHeader(MailConventions.headerMimeVersion, '1.0');
  final original = originalMessage;
  if (original != null) {
    final originalMessageId = original.getHeaderValue('message-id');
    setHeader(MailConventions.headerInReplyTo, originalMessageId);
    final originalReferences = original.getHeaderValue('references');
    final references = originalReferences == null
        ? originalMessageId
        : replyToSimplifyReferences
            ? originalReferences
            : originalReferences + ' ' + originalMessageId!;
    setHeader(MailConventions.headerReferences, references);
  }
  if (text != null && attachments.isNotEmpty) {
    addTextPlain(text!, transferEncoding: transferEncoding, insert: true);
  }
  _buildPart();
  _message.parse();
  return _message;
}