从InternetAddress打印出发件人的电子邮件地址 [英] Printing out the email address of sender from InternetAddress

查看:260
本文介绍了从InternetAddress打印出发件人的电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是提取电子邮件sendersubject的代码.使用此代码,我可以看到显示了正确的主题,但可以看到发送人的地址格式不同.

This is the code that fetches up the sender and the subject of email.With this code i see the correct subject getting displayed but i see the address of the sender in different format.

Properties props = new Properties();
    props.put("mail.imap.host" , "imap.gmail.com" );
    props.put("mail.imap.user" , "username");
    // User SSL
    props.put("mail.imap.socketFactory" , 993);
    props.put("mail.imap.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
    props.put("mail.imap.port" , 993 );
    Session session = Session.getDefaultInstance(props , new Authenticator() {
        @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("username" , "password");
        }
    });

    try {
      Store store = session.getStore("imap");
      store.connect("imap.gmail.com" , "username" , "password");
      Folder fldr = store.getFolder("Inbox");
      fldr.open(Folder.READ_ONLY);
      Message msgs[] = fldr.getMessages();
        for(int i = 0 ; i < msgs.length ; i++) {
            System.out.println(msgs[i].getFrom() + "<-- FROM" + " " + msgs[i].getSubject() + "<---Subject");
        }
    } catch(Exception exc) {

    }
}

输出为:

[Ljavax.mail.internet.InternetAddress;@1462851<-- FROMGet Gmail on your mobile phone<---Subject
[Ljavax.mail.internet.InternetAddress;@bdab91<-- FROMImport your contacts and old email<---Subject
[Ljavax.mail.internet.InternetAddress;@4ac00c<-- FROMCustomize Gmail with colors and themes<---Subject
[Ljavax.mail.internet.InternetAddress;@1865b28<-- FROMtester<---Subject

它是什么形式? (@ 1462851) 我希望显示发件人的电子邮件地址而不是@1462851.我该怎么做?

What form it is?(@1462851) I want the email address of sender to appear instead of @1462851.How can i do this ?

推荐答案

getForm()返回一个对象.要将其打印为纯字符串,请在System.out中尝试InternetAddress.toString(msgs[i].getFrom()).

The getForm() returns an object. To have it printed as a plain string, please try InternetAddress.toString(msgs[i].getFrom()) in your System.out.

这篇关于从InternetAddress打印出发件人的电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆