当前位置: 技术问答>java相关
哪位能提供一段收pop3邮件的代码?利用javamail工具包就行了。
来源: 互联网 发布时间:2015-05-28
本文导语: 能够接收带附件的邮件。谢谢~~ | properties.put("mail.smtp.host", "pop3.sohu.com"); 不对!应该是 properties.put("mail.smtp.host", "smtp.sohu.com"); | public String getMail(String username,String password,String mailh...
能够接收带附件的邮件。谢谢~~
|
properties.put("mail.smtp.host", "pop3.sohu.com");
不对!应该是
properties.put("mail.smtp.host", "smtp.sohu.com");
不对!应该是
properties.put("mail.smtp.host", "smtp.sohu.com");
|
public String getMail(String username,String password,String mailhost) {
String blnResult = "";
String mbox = "INBOX";
try {
Properties props = System.getProperties();
if (mailhost != null)
props.put("mail.smtp.host", mailhost);
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("pop3");
try {
store.connect(mailhost,username,password);
}
catch(Exception e) {
blnResult = "ERROR:connect failed";
}
Folder inbox = store.getFolder(mbox);
if (inbox == null) {
System.out.println("ERROR:can't open the mail box");
blnResult = "ERROR:can't open the mail box";
}
try {
inbox.open(Folder.READ_WRITE);
}
catch (MessagingException ex) {
inbox.open(Folder.READ_ONLY);
}
int totalMessages = inbox.getMessageCount();
int newMessages = inbox.getNewMessageCount();
blnResult = blnResult + "Total messages = " + totalMessages + "
";
blnResult = blnResult + "New messages = " + newMessages + "
";
blnResult = blnResult + "-------------------------------
";
//System.out.println("Total messages = " + totalMessages);
//System.out.println("New messages = " + newMessages);
//System.out.println("-------------------------------");
Message message[] = inbox.getMessages();
for(int i =0;i
String blnResult = "";
String mbox = "INBOX";
try {
Properties props = System.getProperties();
if (mailhost != null)
props.put("mail.smtp.host", mailhost);
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("pop3");
try {
store.connect(mailhost,username,password);
}
catch(Exception e) {
blnResult = "ERROR:connect failed";
}
Folder inbox = store.getFolder(mbox);
if (inbox == null) {
System.out.println("ERROR:can't open the mail box");
blnResult = "ERROR:can't open the mail box";
}
try {
inbox.open(Folder.READ_WRITE);
}
catch (MessagingException ex) {
inbox.open(Folder.READ_ONLY);
}
int totalMessages = inbox.getMessageCount();
int newMessages = inbox.getNewMessageCount();
blnResult = blnResult + "Total messages = " + totalMessages + "
";
blnResult = blnResult + "New messages = " + newMessages + "
";
blnResult = blnResult + "-------------------------------
";
//System.out.println("Total messages = " + totalMessages);
//System.out.println("New messages = " + newMessages);
//System.out.println("-------------------------------");
Message message[] = inbox.getMessages();
for(int i =0;i