Android下Imap发信

Feb 01, 2011

结合SMS Backup的代码,Imap发信的流程如下:

1.邮箱登陆

imapStore = new ImapStore(String.format(Consts.IMAP_URI,URLEncoder.encode(username),
            URLEncoder.encode(password).replace("+", "%20")));   //%20即URI编码中的空格

其中Consts.IMAP_URI的值为:"imap+ssl+://%s:%s@imap.gmail.com:993"。 其中ssl加密的默认端口是993。ImapStore的源代码见:http://hi-android.info/src/com/android/email/mail/store/ImapStore.java.html。 若密码错误或者用户不存在则可抛出如下异常:

try {
       imapStore = new ImapStore(String.format(Consts.IMAP_URI, URLEncoder.encode(username),
       URLEncoder.encode(password).replace("+", "%20")));                                                              
       folder = imapStore.getFolder(label);
       folderExists = folder.exists();
       if (!folderExists) {
            Log.i(Consts.TAG, "Label '" + label + "' does not exist yet. Creating.");
            folder.create(FolderType.HOLDS_MESSAGES);
       }
       folder.open(OpenMode.READ_WRITE);    
} catch (MessagingException e) {        //登陆出错
       throw new AuthenticationErrorException(e);
}

2.确认要发送至哪个folder

跟普通的打开文件没有太大区别:

folder = imapStore.getFolder(label);
folderExists = folder.exists();
if (!folderExists) {
       Log.i(Consts.TAG, "Label '" + label + "' does not exist yet. Creating.");
       folder.create(FolderType.HOLDS_MESSAGES);
}
folder.open(OpenMode.READ_WRITE);    //注意权限的设定

3.主题、头部、日期等其他信息设定

由于下属代码涉及SMS Backup中短信和邮件内容、主题、日期的同步,所以只注意主要Imap部分的设定即可。

private Message messageFromHashMap(HashMap<string , String> msgMap) throws MessagingException {
    Message msg = new MimeMessage();   //MimeMessage()作为Message()的接口,实现了Message()的一些抽象方法:
    //MimeMessage源代码:http://hi-android.info/src/com/android/email/mail/internet/MimeMessage.java.html
    //Message源代码:http://hi-android.info/src/com/android/email/mail/Message.java.html

    PersonRecord record = null;
    String address = msgMap.get("address");
    if (address != null) {
        address = address.trim();    //去空格
        if (address.length() > 0) {
            record = lookupPerson(address);
        }
    }

    if (record == null) {
        record = new PersonRecord();
        record._id = address;
        record.name = address;
        record.address = new Address(address + "@" + UNKNOWN_PERSON);
    }

    msg.setSubject("SMS with " + record.name);     //确定邮件主题

    TextBody body = new TextBody(msgMap.get(SmsConsts.BODY));    //获取短信内容

    int messageType = Integer.valueOf(msgMap.get(SmsConsts.TYPE));
    if (SmsConsts.MESSAGE_TYPE_INBOX == messageType) {    //收到的短信
        // Received message
        msg.setFrom(record.address);        //收到的短信在邮件From中显示收件人
        msg.setRecipient(RecipientType.TO, mUserAddress);         
    } else {          //发出去的短信
        // Sent message
        msg.setRecipient(RecipientType.TO, record.address);
        msg.setFrom(mUserAddress);
    }

    msg.setBody(body);    //设定邮件内容
    Date then = new Date(Long.valueOf(msgMap.get(SmsConsts.DATE)));
    msg.setSentDate(then);    //设定发件日期
    msg.setInternalDate(then);
    // Threading by person ID, not by thread ID. I think this value is more
    // stable.
    //设定头信息
    msg.setHeader("References", String.format(REFERENCE_UID_TEMPLATE, mReferenceValue,
            record._id));

    msg.setHeader("X-smssync-id", msgMap.get(SmsConsts.ID));           
    msg.setHeader("X-smssync-address", address);
    msg.setHeader("X-smssync-type", msgMap.get(SmsConsts.TYPE));
    msg.setHeader("X-smssync-date", msgMap.get(SmsConsts.DATE));
    msg.setHeader("X-smssync-thread", msgMap.get(SmsConsts.THREAD_ID));
    msg.setHeader("X-smssync-read", msgMap.get(SmsConsts.READ));
    msg.setHeader("X-smssync-status", msgMap.get(SmsConsts.STATUS));
    msg.setHeader("X-smssync-protocol", msgMap.get(SmsConsts.PROTOCOL));
    msg.setHeader("X-smssync-service_center", msgMap.get(SmsConsts.SERVICE_CENTER));
    msg.setHeader("X-smssync-backup_time", new Date().toGMTString());
    msg.setFlag(Flag.SEEN, mMarkAsRead);

    return msg;
}

4.发送邮件

folder.appendMessages(messages.toArray(new Message[messages.size()]));
Posted by admin android , imap , android develop , send mail

Comments

  1. 在sms backup源代码中imapstore.jar这个参考类库(我也不知道该怎么叫),我有这个东西的源代码,但是怎么编译?应该用到那些工具,求教

    Avatar
    • imapstore是SDK的东西啊,我可能不太清楚你的意思。你编译imapstore是指对imapstore的源码做了修改,想重新编译吗??
      实在不好意思,这个我不太懂,我的java是半桶水而已,而且实在做项目的时候现学的,这个问题可能帮不到你....

      Avatar

Get In Touch With Us ...