diff --git a/lib/src/mail/mail_client.dart b/lib/src/mail/mail_client.dart index 2793a030..030a7168 100644 --- a/lib/src/mail/mail_client.dart +++ b/lib/src/mail/mail_client.dart @@ -925,14 +925,17 @@ class MailClient { final message = messageBuilder.buildMimeMessage(); final use8Bit = builderEncoding == TransferEncoding.eightBit; + return sendMessage( message, from: from, appendToSent: appendToSent, + supportUnicode: supportUnicode sentMailbox: sentMailbox, use8BitEncoding: use8Bit, recipients: recipients, ); + } /// Sends the specified [message]. @@ -956,20 +959,24 @@ class MailClient { /// /// Optionally specify the [sentMailbox] when the mail system does not /// support mailbox flags. + /// first Future sendMessage( MimeMessage message, { MailAddress? from, bool appendToSent = true, + bool supportUnicode = false, Mailbox? sentMailbox, bool use8BitEncoding = false, List? recipients, }) async { await _prepareConnect(); final futures = [ + _outgoingLock.synchronized( () => - _sendMessageViaOutgoing(message, from, use8BitEncoding, recipients), + _sendMessageViaOutgoing(message, from, use8BitEncoding, recipients, supportUnicode: supportUnicode), ), + ]; if (appendToSent && _incomingMailClient.supportsAppendingMessages) { sentMailbox ??= getMailbox(MailboxFlag.sent); @@ -988,21 +995,26 @@ class MailClient { } } - await Future.wait(futures); + + return Future.wait(futures); } + Future _sendMessageViaOutgoing( MimeMessage message, MailAddress? from, bool use8BitEncoding, List? recipients, + {bool supportUnicode = false} ) async { await _outgoingMailClient.sendMessage( message, from: from, use8BitEncoding: use8BitEncoding, + supportUnicode: supportUnicode, recipients: recipients, ); + await _outgoingMailClient.disconnect(); } @@ -1676,9 +1688,13 @@ enum ThreadPreference { abstract class _IncomingMailClient { _IncomingMailClient(this.downloadSizeLimit, this._config, this.mailClient); + final MailClient mailClient; + ClientBase get client; + ServerType get clientType; + int? downloadSizeLimit; MailServerConfig _config; Mailbox? _selectedMailbox; @@ -1851,6 +1867,7 @@ class _IncomingImapClient extends _IncomingMailClient { @override ClientBase get client => _imapClient; + @override ServerType get clientType => ServerType.imap; final ImapClient _imapClient; @@ -1863,9 +1880,11 @@ class _IncomingImapClient extends _IncomingMailClient { int _reconnectCounter = 0; bool _isIdlePaused = false; ThreadDataResult? _threadData; + @override bool get supportsMailboxes => true; Id? _serverId; + @override Id? get serverId => _serverId; @@ -3257,6 +3276,7 @@ class _IncomingPopClient extends _IncomingMailClient { @override ClientBase get client => _popClient; + @override ServerType get clientType => ServerType.pop; @@ -3551,6 +3571,7 @@ abstract class _OutgoingMailClient { : _mailConfig = mailConfig; ClientBase get client; + ServerType get clientType; MailServerConfig _mailConfig; @@ -3559,12 +3580,13 @@ abstract class _OutgoingMailClient { /// Is only correct after authorizing. Future supports8BitEncoding(); - Future sendMessage( - MimeMessage message, { - MailAddress? from, - bool use8BitEncoding = false, - List? recipients, - }); + + Future sendMessage(MimeMessage message, + {required bool supportUnicode, + MailAddress? from, + bool use8BitEncoding = false, + List? recipients}); + Future disconnect(); } @@ -3590,6 +3612,7 @@ class _OutgoingSmtpClient extends _OutgoingMailClient { @override ClientBase get client => _smtpClient; + @override ServerType get clientType => ServerType.smtp; final MailClient mailClient; @@ -3635,6 +3658,7 @@ class _OutgoingSmtpClient extends _OutgoingMailClient { @override Future sendMessage( MimeMessage message, { + required bool supportUnicode, MailAddress? from, bool use8BitEncoding = false, List? recipients, @@ -3645,6 +3669,7 @@ class _OutgoingSmtpClient extends _OutgoingMailClient { await _smtpClient.sendChunkedMessage( message, from: from, + supportUnicode: supportUnicode, use8BitEncoding: use8BitEncoding, recipients: recipients, ); diff --git a/lib/src/private/smtp/commands/smtp_send_bdat_command.dart b/lib/src/private/smtp/commands/smtp_send_bdat_command.dart index 13c7125c..f2333b4c 100644 --- a/lib/src/private/smtp/commands/smtp_send_bdat_command.dart +++ b/lib/src/private/smtp/commands/smtp_send_bdat_command.dart @@ -15,6 +15,7 @@ class _SmtpSendBdatCommand extends SmtpCommand { this.fromEmail, this.recipientEmails, { required this.use8BitEncoding, + required this.supportUnicode, }) : super('MAIL FROM') { final binaryData = _codec.encode(getData()); _chunks = chunkData(binaryData); @@ -24,6 +25,7 @@ class _SmtpSendBdatCommand extends SmtpCommand { final String? fromEmail; final List recipientEmails; final bool use8BitEncoding; + final bool supportUnicode; _BdatSequence _currentStep = _BdatSequence.mailFrom; int _recipientIndex = 0; late List _chunks; @@ -54,6 +56,10 @@ class _SmtpSendBdatCommand extends SmtpCommand { @override String get command { + if (supportUnicode) { + print('supportUnicode $supportUnicode'); + return 'MAIL FROM:<$fromEmail> SMTPUTF8'; + } if (use8BitEncoding) { return 'MAIL FROM:<$fromEmail> BODY=8BITMIME'; } @@ -116,6 +122,7 @@ class SmtpSendBdatMailCommand extends _SmtpSendBdatCommand { MailAddress? from, List recipientEmails, { required bool use8BitEncoding, + required bool supportUnicode, }) : super( () => message .renderMessage() @@ -123,7 +130,8 @@ class SmtpSendBdatMailCommand extends _SmtpSendBdatCommand { from?.email ?? message.fromEmail, recipientEmails, use8BitEncoding: use8BitEncoding, - ); + supportUnicode: supportUnicode, + ); /// The message to be sent final MimeMessage message; @@ -137,14 +145,15 @@ class SmtpSendBdatMailDataCommand extends _SmtpSendBdatCommand { MailAddress from, List recipientEmails, { required bool use8BitEncoding, + required bool supportUnicode, }) : super( - () => data - .toString() - .replaceAll(RegExp('^Bcc:.*\r\n', multiLine: true), ''), - from.email, - recipientEmails, - use8BitEncoding: use8BitEncoding, - ); + () => data + .toString() + .replaceAll(RegExp('^Bcc:.*\r\n', multiLine: true), ''), + from.email, + recipientEmails, + use8BitEncoding: use8BitEncoding, + supportUnicode: supportUnicode); /// The message data to be sent final MimeData data; @@ -158,12 +167,9 @@ class SmtpSendBdatMailTextCommand extends _SmtpSendBdatCommand { MailAddress from, List recipientEmails, { required bool use8BitEncoding, - }) : super( - () => data, - from.email, - recipientEmails, - use8BitEncoding: use8BitEncoding, - ); + required bool supportUnicode, + }) : super(() => data, from.email, recipientEmails, + use8BitEncoding: use8BitEncoding, supportUnicode: supportUnicode); /// The message text data final String data; diff --git a/lib/src/private/smtp/commands/smtp_sendmail_command.dart b/lib/src/private/smtp/commands/smtp_sendmail_command.dart index e523342d..ae669932 100644 --- a/lib/src/private/smtp/commands/smtp_sendmail_command.dart +++ b/lib/src/private/smtp/commands/smtp_sendmail_command.dart @@ -1,3 +1,5 @@ +import 'dart:developer'; + import '../../../../enough_mail.dart'; import '../smtp_command.dart'; diff --git a/lib/src/private/util/client_base.dart b/lib/src/private/util/client_base.dart index aad335ef..8086184c 100644 --- a/lib/src/private/util/client_base.dart +++ b/lib/src/private/util/client_base.dart @@ -210,7 +210,6 @@ abstract class ClientBase { /// Disconnects from the service Future disconnect() async { if (_isConnected) { - logApp('disconnecting'); isLoggedIn = false; _isConnected = false; isSocketClosingExpected = true; diff --git a/lib/src/smtp/smtp_client.dart b/lib/src/smtp/smtp_client.dart index c2e10c60..6920fc52 100644 --- a/lib/src/smtp/smtp_client.dart +++ b/lib/src/smtp/smtp_client.dart @@ -301,6 +301,7 @@ class SmtpClient extends ClientBase { /// defined in the message are ignored. Future sendChunkedMessage( MimeMessage message, { + required bool supportUnicode, bool use8BitEncoding = false, MailAddress? from, List? recipients, @@ -312,7 +313,7 @@ class SmtpClient extends ClientBase { throw SmtpException(this, SmtpResponse(['500 no recipients'])); } return sendCommand(SmtpSendBdatMailCommand(message, from, recipientEmails, - use8BitEncoding: use8BitEncoding)); + use8BitEncoding: use8BitEncoding, supportUnicode: supportUnicode)); } /// Sends the specified message [data] [from] to the [recipients] @@ -325,7 +326,7 @@ class SmtpClient extends ClientBase { /// Set [use8BitEncoding] to `true` for sending a UTF-8 encoded message body. Future sendChunkedMessageData( MimeData data, MailAddress from, List recipients, - {bool use8BitEncoding = false}) { + { required bool supportUnicode,bool use8BitEncoding = false}) { if (recipients.isEmpty) { throw SmtpException(this, SmtpResponse(['500 no recipients'])); } @@ -334,6 +335,7 @@ class SmtpClient extends ClientBase { data, from, recipients.map((r) => r.email).toList(), + supportUnicode: supportUnicode, use8BitEncoding: use8BitEncoding, ), ); @@ -352,7 +354,7 @@ class SmtpClient extends ClientBase { /// Set [use8BitEncoding] to `true` for sending a UTF-8 encoded message body. Future sendChunkedMessageText( String text, MailAddress from, List recipients, - {bool use8BitEncoding = false}) { + {required bool supportUnicode,bool use8BitEncoding = false}) { if (recipients.isEmpty) { throw SmtpException(this, SmtpResponse(['500 no recipients'])); } @@ -361,6 +363,7 @@ class SmtpClient extends ClientBase { text, from, recipients.map((r) => r.email).toList(), + supportUnicode: supportUnicode, use8BitEncoding: use8BitEncoding, ), ); diff --git a/test/smtp/smtp_client_test.dart b/test/smtp/smtp_client_test.dart index 791ecde4..2c37ce37 100644 --- a/test/smtp/smtp_client_test.dart +++ b/test/smtp/smtp_client_test.dart @@ -93,7 +93,7 @@ void main() { final message = MessageBuilder.buildSimpleTextMessage( from, to, 'Today as well.\r\nOne more time:\r\nHello from enough_mail!', subject: 'enough_mail hello'); - final response = await client.sendChunkedMessage(message); + final response = await client.sendChunkedMessage(message,supportUnicode: false); expect(response.type, SmtpResponseType.success); expect(response.code, 250); });