validate method

  1. @override
void validate()
override

Validates the message before sending.

Throws an exception if the message is invalid.

Implementation

@override
void validate() {
  // Must have at least one recipient
  if (_to.isEmpty && _cc.isEmpty && _bcc.isEmpty) {
    throw MailException('Email must have at least one recipient');
  }

  // Must have a subject
  if (_subject == null || _subject!.trim().isEmpty) {
    throw MailException('Email must have a subject');
  }

  // Must have content (text or HTML)
  if ((_textBody == null || _textBody!.trim().isEmpty) &&
      (_htmlBody == null || _htmlBody!.trim().isEmpty)) {
    throw MailException('Email must have content (text or HTML body)');
  }

  // Validate from address if set
  if (_from != null && !_isValidEmail(_from!.email)) {
    throw MailException('Invalid from email address: ${_from!.email}');
  }

  // Validate all recipient addresses
  for (final address in [..._to, ..._cc, ..._bcc]) {
    if (!_isValidEmail(address.email)) {
      throw MailException('Invalid email address: ${address.email}');
    }
  }
}