getReceive.java
上传用户:liangcc
上传日期:2019-05-24
资源大小:4412k
文件大小:5k
- package com.softeem.webmail.action.receivemail;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Properties;
- import java.util.Vector;
- import javax.mail.Address;
- import javax.mail.Folder;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.NoSuchProviderException;
- import javax.mail.Session;
- import javax.mail.Store;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import javax.servlet.http.HttpServletRequest;
- import com.softeem.webmail.dao.mail.Mail;
- public class getReceive {
-
- public static Message[] getMessage(String host,String name,String password) {
- host="pop."+host+".com";
- // String host = "pop.163.com";
- // String username = "zserdx001";
- // String password = "jimjim";
- Folder folder=null;
- Store store=null;
- Properties props = new Properties();
- Session session = Session.getDefaultInstance(props, null);
- try {
- store = session.getStore("pop3");
- store.connect(host, name, password);
- folder = store.getFolder("INBOX");
- folder.open(Folder.READ_ONLY);
- Message[] message = folder.getMessages();
- return message;
- } catch (NoSuchProviderException e2) {
- e2.printStackTrace();
- } catch (MessagingException e) {
- e.printStackTrace();
- }
- // finally{
- // try {
- // folder.close(false);
- // store.close();
- // } catch (MessagingException e) {
- // e.printStackTrace();
- // }
- // }
- return null;
- }
- public static void main(String[] args) {
- getReceive.getMessage("163", "zserdx001", "jimjim");
- }
-
- public static List<Mail> getMail(Message[] message ,String username,HttpServletRequest request){
- List<Mail> mails=new ArrayList<Mail>();
- for (int i = 0, n = message.length; i < n; i++){
- try {
- //new一个mail对象
- Mail mail=new Mail();
- //填充mail_from
- InternetAddress address[] = (InternetAddress[]) message[i].getFrom();
- String from = address[0].getAddress();
- System.out.println("from="+from+"<><><>");
- if (from == null)
- from = "";
- String personal = address[0].getPersonal();
- if (personal == null)
- personal = "";
- String fromaddr = personal + "<" + from + ">";
- mail.setMail_from(from);
- //填充日期
- mail.setDate(message[i].getSentDate().toString());
- //填充大小
- mail.setSize(message[i].getSize());
- //填充所要放入的邮箱
- mail.setBox_name("recbox");
-
- //填充收件人
-
- mail.setMail_to(username);
- //填充主题
- mail.setSubject(message[i].getSubject());
- //填充附件地址
- csdntest csdn=new csdntest();
- Vector vec=csdn.getAtta(message[i],request);
- if(vec!=null){
- Vector va=(Vector)vec.get(2);
- if(va.size()>0){
- String path=(String)va.get(0);
- mail.setAdds(path);
-
- }
- }
- //填充邮件内容
- mail.setContent(message[i].getSubject());
- //填充状态
- mail.setStat("1");
- mails.add(mail);
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
- return mails;
-
- }
-
-
- // public static Mail[] getMail(Message[] message){
- // Mail[] mail;
- // for (int i = 0, n = message.length; i < n; i++) {
- // Mail m=new Mail();
- // try {
- // if (!"class java.lang.String".equals(message[i].getContent()
- // .getClass().toString())) {
- // Multipart mp = (Multipart) message[i].getContent();
- // for (int j = 0, s = mp.getCount(); j < s; j++) {
- // BodyPart part = mp.getBodyPart(j);
- // String disposition = part.getDisposition();
- // if (disposition == null) {
- // // Check if plain
- // MimeBodyPart mbp = (MimeBodyPart) part;
- // if (mbp.isMimeType("text/plain;")) {
- // FromBase64.getFromBase64(mbp.toString());
- //// m.setContent();
- //
- //
- // } else {
- // if(mbp.isMimeType("non-attachment")){
- // System.out.println(mbp.getFileName()+"============");
- // /*
- // * 存储附件
- // *
- // * */
- // SaveAdd.SaveFile(mbp.getFileName(), mbp.getInputStream());
- // }
- //
- // }
- // }
- // else if ((disposition != null)
- // && ((disposition.equals(Part.ATTACHMENT)))
- // || (disposition.equals(Part.INLINE))) {
- // try {
- // System.out.println(part.getFileName()
- // + "============附件名称");
- // SaveAdd.SaveFile(part.getFileName(), part.getInputStream());
- // } catch (MessagingException e) {
- // e.printStackTrace();
- // }
- // try {
- // part.writeTo(System.out);
- // } catch (IOException e) {
- // e.printStackTrace();
- // } catch (MessagingException e) {
- // e.printStackTrace();
- // }
- // }
- // }
- // }
- // else{
- // System.out.println("邮件内容是"+message[i].getContent());
- // }
- // } catch (IOException e) {
- // e.printStackTrace();
- // } catch (MessagingException e) {
- // e.printStackTrace();
- // }
- // }
- // return null;
- //}
- }