从 InternetAddress 打印发件人的电子邮件地址

发布于 2024-11-25 14:04:43 字数 1714 浏览 1 评论 0原文

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

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。我该如何执行此操作?

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) {

    }
}

The output is :

[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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

画离情绘悲伤 2024-12-02 14:04:43

getFrom() 函数返回 Address[] 类型的列表。要将其打印为纯字符串,请在 System.out 中尝试 InternetAddress.toString(msgs[i].getFrom())

The getFrom() function returns a list of type Address[]. To have it printed as a plain string, please try InternetAddress.toString(msgs[i].getFrom()) in your System.out.

琉璃繁缕 2024-12-02 14:04:43

我花了几分钟才发现这个简单的代码:


System.out.println("从 "+((InternetAddress)((Address)(message.getFrom()[0]))).getAddress()) 收到;

I've spent some hard minutes before I found out this simple code:


System.out.println("received from "+((InternetAddress)((Address)(message.getFrom()[0]))).getAddress());

无名指的心愿 2024-12-02 14:04:43

您应该使用 msgs [i].getFrom().getAddress()。你看到的是InternetAddress对象的toString结果(类名+哈希码)

You should use msgs[i].getFrom().getAddress(). What you see is the toString result of InternetAddress object (class name + hashcode)

奶茶白久 2024-12-02 14:04:43

原因是您只是打印出 InternetAddress 实例,它没有 toString() 方法。然后它默认为Object.toString(),它主要用于查看对象是否不同。

考虑明确挑选出您想要在打印语句中看到的字段。

The reason for this is because you just print out the InternetAddress instance, which does not have a toString() method. Then it defaults to the Object.toString() which is primarily useful to see if objects are different.

Consider explicitly picking out the field you want to see in your print statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文