jmail 수신 메 일
package com.chinahrt.zyn;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
public class ReciveMailUtil {
private MimeMessage msg = null;
private String saveAttchPath="";
private StringBuffer bodytext = new StringBuffer();
private String dateformate = "yy-MM-dd HH:mm:ss";
public ReciveMailUtil (MimeMessage msg){
this.msg = msg;
}
public void setMsg(MimeMessage msg){
this.msg = msg;
}
//
public String getFrom() throws MessagingException{
InternetAddress[] address = (InternetAddress[]) msg.getFrom();
String from = address[0].getAddress();
if(from == null){
from = "";
}
String personal = address[0].getPersonal();
if(personal == null){
personal = "";
}
String fromaddr = personal +"<"+from+">";
return fromaddr;
}
// , , ,"to"--> ,"cc"--> ,"bcc"-->
public String getMailAddress(String type) throws MessagingException,UnsupportedEncodingException{
String mailaddr = "";
String addrType = type.toUpperCase();
InternetAddress[] address = null;
if(addrType.equals("TO")||addrType.equals("CC")||addrType.equals("BCC")){
if(addrType.equals("TO")){
address = (InternetAddress[])msg.getRecipients(Message.RecipientType.TO);
}
if(addrType.equals("CC")){
address = (InternetAddress[])msg.getRecipients(Message.RecipientType.CC);
}
if(addrType.equals("BCC")){
address = (InternetAddress[])msg.getRecipients(Message.RecipientType.BCC);
}
if(address!=null){
for(int i=0;i<address.length;i++){
String mail = address[i].getAddress();
if(mail == null){
mail = "";
}else{
mail = MimeUtility.decodeText(mail);
}
String personal = address[i].getPersonal();
if(personal == null){
personal = "";
}else{
personal = MimeUtility.decodeText(personal);
}
String compositeto = personal +"<"+mail+">";
mailaddr += "," + compositeto;
}
mailaddr = mailaddr.substring(1);
}
}else{
throw new RuntimeException("Error email Type!");
}
return mailaddr;
}
//
public String getSubject() throws UnsupportedEncodingException,MessagingException{
String subject = "";
subject = MimeUtility.decodeText(msg.getSubject());
if(subject == null){
subject = "";
}
return subject;
}
//
public String getSendDate() throws MessagingException{
Date sendDate = msg.getSentDate();
SimpleDateFormat sdf = new SimpleDateFormat(dateformate);
return sdf.format(sendDate);
}
//
public String getBodyText(){
return bodytext.toString();
}
//
public void getMailContent(Part part) throws MessagingException,IOException{
String contentType = part.getContentType();
int nameindex = contentType.indexOf("name");
boolean conname = false;
if(nameindex !=-1){
conname = true;
}
System.out.println("CONTENTTYPE:"+contentType);
if(part.isMimeType("text/plain")&&!conname){
bodytext.append((String)part.getContent());
}else if(part.isMimeType("text/html")&&!conname){
bodytext.append((String)part.getContent());
}else if(part.isMimeType("multipart/*")){
Multipart multipart = (Multipart)part.getContent();
int count = multipart.getCount();
for(int i=0;i<count;i++){
getMailContent(multipart.getBodyPart(i));
}
}else if(part.isMimeType("message/rfc822")){
getMailContent((Part)part.getContent());
}
}
// , true, false
public boolean getReplySign()throws MessagingException{
boolean replySign = false;
String needreplay[] = msg.getHeader("Disposition-Notification-TO");
if(needreplay != null){
replySign = true;
}
return replySign;
}
// message-id
public String getMessageId()throws MessagingException{
return msg.getMessageID();
}
// , false, true
public boolean isNew() throws MessagingException{
boolean isnew = false;
Flags flags = ((Message)msg).getFlags();
Flags.Flag[] flag = flags.getSystemFlags();
System.out.println("flags's legth:"+flag.length);
for(int i=0;i<flag.length;i++){
if(flag[i] == Flags.Flag.SEEN){
isnew = true;
System.out.println("seen message ......");
break;
}
}
return isnew;
}
//
public boolean isContainAttch(Part part) throws MessagingException,IOException{
boolean flag = false;
String contentType = part.getContentType();
if(part.isMimeType("multipart/*")){
Multipart multipart = (Multipart)part.getContent();
int count = multipart.getCount();
for(int i=0;i<count;i++){
BodyPart bodypart = multipart.getBodyPart(i);
String dispostion = bodypart.getDisposition();
if((dispostion != null) && (dispostion.equals(Part.ATTACHMENT)||dispostion.equals(Part.INLINE))){
flag = true;
}else if(bodypart.isMimeType("multipart/*")){
flag = isContainAttch(bodypart);
}else{
String conType = bodypart.getContentType();
if(conType.toLowerCase().indexOf("appliaction")!=-1){
flag = true;
}
if(conType.toLowerCase().indexOf("name")!=-1){
flag = true;
}
}
}
}else if(part.isMimeType("message/rfc822")){
flag = isContainAttch((Part)part.getContent());
}
return flag;
}
//
public void saveAttchMent(Part part) throws MessagingException,IOException{
String filename = "";
if(part.isMimeType("multipart/*")){
Multipart mp = (Multipart)part.getContent();
for(int i=0;i<mp.getCount();i++){
BodyPart mpart = mp.getBodyPart(i);
String dispostion = mpart.getDisposition();
if((dispostion!=null) && (dispostion.equals(Part.ATTACHMENT)|| dispostion.equals(Part.INLINE))){
filename = mpart.getFileName();
if(filename.toLowerCase().indexOf("gb2312")!=-1){
filename = MimeUtility.decodeText(filename);
}
saveFile(filename,mpart.getInputStream());
}else if(mpart.isMimeType("multipart/*")){
saveAttchMent(mpart);
}else{
filename = mpart.getFileName();
if(filename != null && (filename.toLowerCase().indexOf("gb2312")!=-1)){
filename = MimeUtility.decodeText(filename);
}
saveFile(filename,mpart.getInputStream());
}
}
}else if(part.isMimeType("message/rfc822")){
saveAttchMent((Part)part.getContent());
}
}
//
public String getSaveAttchPath(){
return saveAttchPath;
}
//
public void setSaveAttchPath(String savaAttchPath){
this.saveAttchPath = saveAttchPath;
}
//
public void setDateformate(String dateformate){
this.dateformate = dateformate;
}
private void saveFile(String filename,InputStream inputStream) throws IOException{
String osname = System.getProperty("os.name");
String storedir = getSaveAttchPath();
String sepatror = "";
if(osname == null){
osname = "";
}
if(osname.toLowerCase().indexOf("win")!=-1){
sepatror = "//";
if(storedir == null ||"".equals(storedir)){
storedir = "d://temp";
}
}else{
sepatror = "/";
storedir = "/temp";
}
File storefile = new File(storedir+sepatror+filename);
System.out.println("storefile's path:"+storefile.toString());
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try{
bos = new BufferedOutputStream(new FileOutputStream(storefile));
bis = new BufferedInputStream(inputStream);
int c;
while((c=bis.read())!=-1){
bos.write(c);
bos.flush();
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
bos.close();
bis.close();
}
}
public void recive(Part part,int i) throws MessagingException,IOException{
System.out.println("@@@start@@@");
System.out.println("Message"+i+" subject:"+getSubject());
System.out.println("Message"+i+" from"+getFrom());
System.out.println("Message"+i+" isNew:"+isNew());
boolean flag = isContainAttch(part);
System.out.println("Message"+i+" iscontainAttch:"+flag);
System.out.println("Message"+i+" replySign:"+getReplySign());
getMailContent(part);
System.out.println("Message"+i+" content:"+getBodyText());
setSaveAttchPath("c://temp//"+i);
if(flag){
saveAttchMent(part);
}
System.out.println("@@@end@@@");
}
/**
* @param args
*/
public static void main(String[] args) throws MessagingException,IOException{
Properties props = new Properties();
props.setProperty("mail.smtp.host", "smtp.163.com");
props.setProperty("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, null);
URLName urlname = new URLName("pop3","pop3.163.com",110,null," "," ");
Store store = session.getStore(urlname);
store.connect();
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message msgs[] = folder.getMessages();
int count = msgs.length;
System.out.println(count);
ReciveMailUtil rm = null;
for(int i=0;i<count;i++){
rm = new ReciveMailUtil((MimeMessage)msgs[i]);
rm.recive(msgs[i], i);
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.