从 InternetAddress 打印发件人的电子邮件地址
这是获取电子邮件的发件人
和主题
的代码。使用此代码,我看到显示了正确的主题,但我看到了不同格式的发件人地址。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
getFrom()
函数返回Address[]
类型的列表。要将其打印为纯字符串,请在System.out
中尝试InternetAddress.toString(msgs[i].getFrom())
。The
getFrom()
function returns a list of typeAddress[]
. To have it printed as a plain string, please tryInternetAddress.toString(msgs[i].getFrom())
in yourSystem.out
.我花了几分钟才发现这个简单的代码:
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());
您应该使用
msgs [i].getFrom().getAddress()
。你看到的是InternetAddress
对象的toString
结果(类名+哈希码)You should use
msgs[i].getFrom().getAddress()
. What you see is thetoString
result ofInternetAddress
object (class name + hashcode)原因是您只是打印出
InternetAddress
实例,它没有toString()
方法。然后它默认为Object.toString()
,它主要用于查看对象是否不同。考虑明确挑选出您想要在打印语句中看到的字段。
The reason for this is because you just print out the
InternetAddress
instance, which does not have atoString()
method. Then it defaults to theObject.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.