Enough-Software / enough_mail

IMAP, POP3 and SMTP clients for Dart developers. Contains both low level as well as a high level API.
Mozilla Public License 2.0
104 stars 56 forks source link

High-Level API problem with our company email #162

Closed xamantra closed 3 years ago

xamantra commented 3 years ago

Full code

(taken from readme sample, modified a bit)

import 'package:enough_mail/enough_mail.dart';
import 'package:enough_mail_test/print.dart';

String email = 'myname@company.com';
String password = '({hKu8qM?BSx'; // real password looks like this

void main() async {
  await mailExample();
}

/// High level mail API example
Future<void> mailExample() async {
  print('discovering settings for  $email...');
  ClientConfig? config;
  try {
    config = await Discover.discover(email);
    if (config == null) {
      print('Unable to autodiscover settings for $email');
      return;
    }
  } catch (e) {
    print(e);
    print('Unable to autodiscover settings for $email');
    return;
  }
  print('connecting to ${config.displayName}.');
  final account = MailAccount.fromDiscoveredSettings('my account', email, password, config);
  final mailClient = MailClient(account, isLogEnabled: true);
  try {
    await mailClient.connect();
    print('connected');
    final mailboxes = await mailClient.listMailboxesAsTree(createIntermediate: false);
    print(mailboxes);
    await mailClient.selectInbox();
    final messages = await mailClient.fetchMessages(count: 3);
    for (final msg in messages) {
      printMessage(msg);
    }
    mailClient.eventBus.on<MailLoadEvent>().listen((event) {
      print('New message at ${DateTime.now()}:');
      printMessage(event.message);
    });
    await mailClient.startPolling();
  } on MailException catch (e) {
    print('High level API failed with $e');
  }
}

Logs

discovering settings for  myname@company.com...
XmlParserException: Expected name at 1:2
unable to parse: 
<!doctype html><html lang="en"><head><meta http-equiv="content-type" content="text/html;charset=utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon"><title></title><script src="https://www.google.com/adsense/domains/caf.js" type="text/javascript"></script><noscript><style>#content-main{display:none}</style><div>For full functionality of this site it is necessary to enable JavaScript. Here are the <a target="_blank" href="https://www.enable-javascript.com/">instructions how to enable JavaScript in your web browser</a>.</div></noscript><script type="application/javascript">window.LANDER_SYSTEM="CP"</script></head><body><div id="contentMain"></div><script>!function(e){function r(r){for(var n,a,i=r[0],l=r[1],p=r[2],c=0,s=[];c<i.length;c++)a=i[c],Object.prototype.hasOwnProperty.call(o,a)&&o[a]&&s.push(o[a][0]),o[a]=0;for(n in l)Object.prototype.hasOwnProperty.call(l,n)&&(e[n]=l[n]);for(f&&f(r);s.length;)s.shift()();return u.push.apply(u,p||[]),t()}function t(){for(var e,r=0;r<u.length;r++){for(var t=u[r],n=!0,i=1;i<t.length;i++){var l=t[i];0!==o[l]&&(n=!1)}n&&(u.splice(r--,1),e=a(a.s=t[0]))}return e}var n={},o={1:0},u=[];function a(r){if(n[r])return n[r].exports;var t=n[r]={i:r,l:!1,exports:{}};return 
e[r].call(t.exports,t,t.exports,a),t.l=!0,t.exports}a.m=e,a.c=n,a.d=function(e,r,t){a.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},a.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},a.t=function(e,r){if(1&r&&(e=a(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var t=Object.create(null);if(a.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var n in e)a.d(t,n,function(r){return e[r]}.bind(null,n));return t},a.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return a.d(r,"a",r),r},a.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},a.p="https://img1.wsimg.com/parking-lander/";var i=this["webpackJsonpparking-lander"]=this["webpackJsonpparking-lander"]||[],l=i.push.bind(i);i.push=r,i=i.slice();for(var p=0;p<i.length;p++)r(i[p]);var f=l;t()}([])</script><script src="https://img1.wsimg.com/parking-lander/static/js/2.818ef8bd.chunk.js"></script><script src="https://img1.wsimg.com/parking-lander/static/js/main.30b7d524.chunk.js"></script></body></html>

failed to find settings for company.com: imap: failure pop: failure smtp: ok
Unable to autodiscover settings for myname@company.com

enough_mail: 1.3.2

robert-virkus commented 3 years ago

Please note that you do not need to use the autodiscovery feature when using the high level API.

Instead you can also specify the relevant servers directly, but this involves quite some code, so I have now added the MailAccount.fromManualSettings() methods in commit bf54846767e2c26eaef313339f49b308469bea35:

  /// Creates a mail account from manual settings with a simple user-name/password authentication.
  ///
  /// You need to specify the account [name], the associated [email], the [incomingHost], [outgoingHost] and [password].
  /// When the [userName] is different from the email, it must also be specified.
  /// You should specify the [outgoingClientDomain] for sending messages, this defaults to `enough.de`.
  /// The [incomingType] defaults to [ServerType.imap], the [incomingPort] to `993` and the [incomingSocketType] to [SocketType.ssl].
  /// The [outgoingType] defaults to [ServerType.smtp], the [outgoingPort] to `465` and the [outgoingSocketType] to [SocketType.ssl].
  static MailAccount fromManualSettings(
    String name,
    String email,
    String incomingHost,
    String outgoingHost,
    String password, {
    ServerType incomingType = ServerType.imap,
    ServerType outgoingType = ServerType.smtp,
    String? userName,
    String outgoingClientDomain = 'enough.de',
    incomingPort = 993,
    outgoingPort = 465,
    SocketType incomingSocketType = SocketType.ssl,
    SocketType outgoingSocketType = SocketType.ssl,
  }) {
    final auth = PlainAuthentication(userName ?? email, password);
    return fromManualSettingsWithAuth(
        name, email, incomingHost, outgoingHost, auth,
        incomingType: incomingType,
        outgoingType: outgoingType,
        outgoingClientDomain: outgoingClientDomain);
  }

  /// Creates a mail account from manual settings with the specified [auth]entication.
  ///
  /// You need to specify the account [name], the associated [email], the [incomingHost], [outgoingHost] and [auth].
  /// You can specify a different authentication for the outgoing server using the [outgoingAuth] parameter.
  /// You should specify the [outgoingClientDomain] for sending messages, this defaults to `enough.de`.
  /// The [incomingType] defaults to [ServerType.imap], the [incomingPort] to `993` and the [incomingSocketType] to [SocketType.ssl].
  /// The [outgoingType] defaults to [ServerType.smtp], the [outgoingPort] to `465` and the [outgoingSocketType] to [SocketType.ssl].
  static MailAccount fromManualSettingsWithAuth(
    String name,
    String email,
    String incomingHost,
    String outgoingHost,
    MailAuthentication auth, {
    ServerType incomingType = ServerType.imap,
    ServerType outgoingType = ServerType.smtp,
    MailAuthentication? outgoingAuth,
    String outgoingClientDomain = 'enough.de',
    incomingPort = 993,
    outgoingPort = 465,
    SocketType incomingSocketType = SocketType.ssl,
    SocketType outgoingSocketType = SocketType.ssl,
  }) {
    final incoming = MailServerConfig()
      ..authentication = auth
      ..serverConfig = ServerConfig(
        type: incomingType,
        hostname: incomingHost,
        port: incomingPort,
        socketType: incomingSocketType,
      );
    final outgoing = MailServerConfig()
      ..authentication = outgoingAuth ?? auth
      ..serverConfig = ServerConfig(
        type: outgoingType,
        hostname: outgoingHost,
        port: outgoingPort,
        socketType: outgoingSocketType,
      );
    final account = MailAccount()
      ..name = name
      ..email = email
      ..incoming = incoming
      ..outgoing = outgoing
      ..outgoingClientDomain = outgoingClientDomain;
    return account;
  }

This is not yet released but available in Git. You can use the above code now, too.

robert-virkus commented 3 years ago

The release v1.3.3 contains the API improvement

xamantra commented 3 years ago

Okay. I will try this.