使用IMAP

时间:2019-05-07 22:25:20

标签: dart flutter imap

我正在尝试在Flutter中创建一个邮件应用程序,现在我正在测试后端,然后将其集成到我的应用程序中。

我正在使用IMAP从大学获取我的所有电子邮件,并使用名为Emails的类将它们插入到列表中,该类的参数包括主题,日期,从,到和正文(所有字符串类型)。

    List emails;
  Future<EMails> getEmails() async {
    ImapFolder inbox = await client.getFolder("inbox");
    EMails list;
    for (var i = inbox.mailCount; i > 0; i--) {
      list.subject = await inbox.fetch(["BODY.PEEK[HEADER.FIELDS (SUBJECT)]"],
          messageIds: [i]) as String; //gets the subject
      list.date = await inbox.fetch(["BODY.PEEK[HEADER.FIELDS (Date)]"],
          messageIds: [i]) as String; //gets the Date
      list.from = await inbox.fetch(["BODY.PEEK[HEADER.FIELDS (From)]"],
          messageIds: [i]) as String; //gets the From
      list.to =
          await inbox.fetch(["BODY.PEEK[HEADER.FIELDS (To)]"], messageIds: [i])
              as String; //gets the To
      list.body = await inbox.fetch(["RFC822.TEXT"], messageIds: [i]) as String;
      emails.add(list);
    }
    return list;
  }

问题是我无法将获取的数据转换为字符串。    https://i.imgur.com/vP91ZG1.png

2 个答案:

答案 0 :(得分:1)

.fetch返回一个Future,但是看起来您还没有在等待它。在每个await前面添加inbox.fetch

此文档记录在https://pub.dev/documentation/imap_client/latest/imap_client/ImapFolder-class.html

答案 1 :(得分:0)

.fetch返回一个Map>

Map<int, Map<String, dynamic>> subject =  await inbox.fetch(["BODY.PEEK[HEADER.FIELDS (SUBJECT)]"],messageIds: [i]);

var mapSubjectEmail = subject[i];
var mapEmail = mapSubjectEmail.values;
var subjectEmail = mapEmail.first as String;