Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Mail;
- import java.util.Properties;
- import javax.mail.*;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import java.io.*;
- public class MailHandler {
- public static String Gmail_Imap_Server = "imap.gmail.com";
- public static String Gmail_Smtp_Server = "smtp.gmail.com";
- public static String Gmail_User_Account = "nctunusa2011@gmail.com";
- public static String Gmail_Password = "nctunusa";
- public static String Hotmail_Pop3_Server = "pop3.live.com"; //port:995
- public static String Hotmail_Smtp_Server = "smtp.live.com"; //port:25
- public static String Hotmail_User_Account = "nctunusa2011@hotmail.com";
- public static String Hotmail_Password = "nctunusa";
- public static void main(String args[]) {
- //theContent=findContentOfSubjectOfAccount(Hotmail_Pop3_Server, Hotmail_User_Account, Hotmail_Password , "test");
- //int flag = sendMail("nctunusa2011@gmail.com","主旨","");
- //MailHandler.sendMail("nctunusa2011@gmail.com", "subject", "content");
- String haha=getContentOfSubject(Hotmail_Pop3_Server,Hotmail_User_Account,Hotmail_Password,"test");
- System.out.println(haha);
- System.out.println("程式結束");
- }
- public static int sendMail(String Recipient,String subject , String content){
- try{
- String SMTP_HOST_NAME = "smtp.gmail.com";
- int SMTP_HOST_PORT = 465;
- String SMTP_AUTH_USER = "nctunusa2011@gmail.com";
- String SMTP_AUTH_PWD = "nctunusa";
- Properties props = new Properties();
- props.put("mail.transport.protocol", "smtps");
- props.put("mail.smtps.host", SMTP_HOST_NAME);
- props.put("mail.smtps.auth", "true");
- // props.put("mail.smtps.quitwait", "false");
- Session mailSession = Session.getDefaultInstance(props,null);
- //mailSession.setDebug(true); //debug information
- Transport transport = mailSession.getTransport("smtps");//這裡從getTransport()改成getTransport("smtps")
- MimeMessage message = new MimeMessage(mailSession);
- message.setSubject(subject);
- message.setContent(content, "text/plain");
- message.addRecipient(Message.RecipientType.TO,
- new InternetAddress(Recipient));
- transport.connect
- (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
- transport.sendMessage(message,
- message.getRecipients(Message.RecipientType.TO));
- transport.close();
- return 0;
- }catch(NoSuchProviderException e){
- e.printStackTrace();
- }catch(Exception e){
- e.printStackTrace();
- }
- return -1;
- }
- public static int findSubject(String Server, String user_account, String password, String subject){
- /*傳回某個account有沒有某封主旨為subject的信件,有則傳回編號,沒有傳回-1*/
- Properties props = System.getProperties();
- int isFound = -1;
- if(Server.equalsIgnoreCase("pop3.live.com")){
- props.setProperty("mail.store.protocol", "pop3");
- }else if(Server.equalsIgnoreCase("imap.gmail.com")) {
- props.setProperty("mail.store.protocol", "imaps");
- }else{
- System.out.println("Error! Neigher \"pop3.live.com\" or \"imap.gmail.com\"");
- }
- try
- {
- Session session = Session.getDefaultInstance(props, null);
- Store store;
- if(Server.equalsIgnoreCase("pop3.live.com")){
- store = session.getStore("pop3");
- }else{
- store = session.getStore("imaps");
- }
- store.connect(Server, user_account, password);
- Folder inbox = store.getFolder("Inbox");
- inbox.open(Folder.READ_ONLY);
- Message messages[] = inbox.getMessages();
- System.out.print("檢查郵件:");
- for(int i=0;i<messages.length;i++)
- {
- System.out.print((i+1) + " ");
- if( messages[i].getSubject().equalsIgnoreCase(subject) ){
- System.out.println("\n找到主旨:" + subject + " 傳回編號:" + i);
- isFound = i;
- break;
- }
- }
- System.out.println("");
- inbox.close(false);
- store.close();
- return isFound;
- }catch (NoSuchProviderException e) {
- e.printStackTrace();
- System.exit(1);
- } catch (MessagingException e) {
- e.printStackTrace();
- System.exit(2);
- }
- return isFound;
- }
- public static String getContentOfSubject(String Server, String user_account, String password, String subject){
- /*
- * 傳回某個account的某subject的內容,若無內容,則傳回空內容。
- */
- Properties props = System.getProperties();
- String content = new String();
- if(Server.equalsIgnoreCase("pop3.live.com")){
- props.setProperty("mail.store.protocol", "pop3");
- }else if(Server.equalsIgnoreCase("imap.gmail.com")) {
- props.setProperty("mail.store.protocol", "imaps");
- }else{
- System.out.println("Error! Neigher \"pop3.live.com\" or \"imap.gmail.com\"");
- }
- try
- {
- Session session = Session.getDefaultInstance(props, null);
- Store store;
- if(Server.equalsIgnoreCase("pop3.live.com")){
- store = session.getStore("pop3");
- }else{
- store = session.getStore("imaps");
- }
- store.connect(Server, user_account, password);
- Folder inbox = store.getFolder("Inbox");
- inbox.open(Folder.READ_ONLY);
- Message messages[] = inbox.getMessages();
- for(int i=0;i<messages.length;i++)
- {
- System.out.println("處理第"+(i+1)+"封郵件");
- if( messages[i].getSubject().equalsIgnoreCase(subject) ){
- System.out.println("找到主旨:" + subject + " 傳回編號:" + i);
- content="";
- Object body = messages[i].getContent();
- Part p = messages[i];
- if(body instanceof Multipart){
- System.out.println("此郵件為Multipart");
- p = ((Multipart)body).getBodyPart(0);
- }
- String contentType = p.getContentType();
- String thisLine="";
- System.out.println("ContentType:" + contentType);
- if(contentType.toUpperCase().startsWith("TEXT/PLAIN") || contentType.toUpperCase().startsWith("TEXT/HTML"))
- {
- System.out.println("為text/plain或text/html ===> 可處理");
- BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
- while( (thisLine = in.readLine())!=null){
- System.out.println("讀到:" + thisLine);
- content+=thisLine + "\r\n";
- }
- }
- else
- {
- System.out.println("不為text/plain或text/html");
- }
- break;
- }
- }
- inbox.close(false);
- store.close();
- return content;
- }catch (NoSuchProviderException e) {
- e.printStackTrace();
- System.exit(1);
- } catch (MessagingException e) {
- e.printStackTrace();
- System.exit(2);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return content;
- }
- public static void processMultipart(Multipart mp, String user_account,int numOfMsg ,String subject)
- {
- try {
- for(int i=0;i<mp.getCount();i++)
- {
- if(i>=1)
- subject = subject + "_" + (i+1) ;
- processPart(mp.getBodyPart(i),user_account,numOfMsg,subject);
- }
- } catch (MessagingException e) {
- e.printStackTrace();
- }
- }
- public static void processPart(Part p,String user_account,int numOfMsg,String subject)
- {
- try{
- String contentType = p.getContentType();
- StringBuffer contentString = new StringBuffer();
- String thisLine="";
- System.out.println("ContentType:" + contentType);
- if(contentType.toUpperCase().startsWith("TEXT/PLAIN") || contentType.toUpperCase().startsWith("TEXT/HTML"))
- {
- System.out.println("為text/plain或text/html ===> 可處理");
- //讀取郵件的reader
- InputStream reader = p.getInputStream();
- reader = new BufferedInputStream(reader);
- //建立專門資料夾存放郵件內容
- File mailDir = new File(user_account+"_Email");
- mailDir.mkdir();
- File aFile=new File( mailDir.getCanonicalPath()+"\\" +(numOfMsg)+"."+subject+".txt");
- //將每封郵件寫到檔案的writer
- FileOutputStream out = new FileOutputStream(aFile);
- BufferedOutputStream bos = new BufferedOutputStream(out);
- int b;
- while( (b=reader.read())!=-1 )
- {
- bos.write(b);
- }
- bos.flush();
- bos.close();
- reader.close();
- }
- else{
- System.out.println("***************************************");
- System.out.println("************Warning! 警告**************");
- System.out.println("***************************************");
- System.out.printf("numOfMsg:%d Subject:%s\n",numOfMsg,subject);
- System.out.println("非為text/plain或text/html類型 ===> 真的遇到例外,該做事了!");
- System.out.println("\t方向:使用JavaBean Activation FrameWork的DataHandler");
- }
- }catch(Exception e){
- System.err.println(e);
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment