Files
sharedinbox/lib/data/imap/imap_client_factory.dart
T

45 lines
1.4 KiB
Dart
Raw Normal View History

2026-04-16 07:35:56 +02:00
import 'package:enough_mail/enough_mail.dart';
import '../../core/models/account.dart';
/// Opens an authenticated IMAP client for [account].
2026-04-16 07:35:56 +02:00
Future<ImapClient> connectImap(Account account, String password) async {
final client = ImapClient(isLogEnabled: false);
await client.connectToServer(
account.imapHost,
account.imapPort,
isSecure: account.imapSsl,
);
await client.login(account.email, password);
return client;
}
/// Opens an authenticated SMTP client for [account].
///
/// Caller is responsible for calling [SmtpClient.quit] when done.
2026-04-16 07:35:56 +02:00
Future<SmtpClient> connectSmtp(Account account, String password) async {
// clientDomain is the sending domain advertised in EHLO — use the host part
// of the sender email, falling back to the SMTP host.
final atIndex = account.email.lastIndexOf('@');
final clientDomain =
atIndex != -1 ? account.email.substring(atIndex + 1) : account.smtpHost;
final client = SmtpClient(clientDomain, isLogEnabled: false);
2026-04-16 07:35:56 +02:00
await client.connectToServer(
account.smtpHost,
account.smtpPort,
isSecure: account.smtpSsl,
);
await client.ehlo();
if (!account.smtpSsl) {
// Opportunistic TLS on submission port (587)
try {
await client.startTls();
} catch (_) {
// Server doesn't support STARTTLS — proceed without it.
}
2026-04-16 07:35:56 +02:00
}
await client.authenticate(account.email, password);
2026-04-16 07:35:56 +02:00
return client;
}