Guest User

Untitled

a guest
Oct 15th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.67 KB | None | 0 0
  1. package Mail;
  2. import java.util.Properties;
  3. import javax.mail.*;
  4. import javax.mail.internet.InternetAddress;
  5. import javax.mail.internet.MimeMessage;
  6.  
  7. import java.io.*;
  8.  
  9. public class MailHandler {
  10. public static String Gmail_Imap_Server = "imap.gmail.com";
  11. public static String Gmail_Smtp_Server = "smtp.gmail.com";
  12. public static String Gmail_User_Account = "nctunusa2011@gmail.com";
  13. public static String Gmail_Password = "nctunusa";
  14.  
  15. public static String Hotmail_Pop3_Server = "pop3.live.com"; //port:995
  16. public static String Hotmail_Smtp_Server = "smtp.live.com"; //port:25
  17. public static String Hotmail_User_Account = "nctunusa2011@hotmail.com";
  18. public static String Hotmail_Password = "nctunusa";
  19.  
  20. public static void main(String args[]) {
  21. //theContent=findContentOfSubjectOfAccount(Hotmail_Pop3_Server, Hotmail_User_Account, Hotmail_Password , "test");
  22. //int flag = sendMail("nctunusa2011@gmail.com","主旨","");
  23. //MailHandler.sendMail("nctunusa2011@gmail.com", "subject", "content");
  24. String haha=getContentOfSubject(Hotmail_Pop3_Server,Hotmail_User_Account,Hotmail_Password,"test");
  25. System.out.println(haha);
  26.  
  27. System.out.println("程式結束");
  28.  
  29. }
  30. public static int sendMail(String Recipient,String subject , String content){
  31. try{
  32. String SMTP_HOST_NAME = "smtp.gmail.com";
  33. int SMTP_HOST_PORT = 465;
  34. String SMTP_AUTH_USER = "nctunusa2011@gmail.com";
  35. String SMTP_AUTH_PWD = "nctunusa";
  36.  
  37. Properties props = new Properties();
  38.  
  39. props.put("mail.transport.protocol", "smtps");
  40. props.put("mail.smtps.host", SMTP_HOST_NAME);
  41. props.put("mail.smtps.auth", "true");
  42. // props.put("mail.smtps.quitwait", "false");
  43.  
  44. Session mailSession = Session.getDefaultInstance(props,null);
  45. //mailSession.setDebug(true); //debug information
  46.  
  47. Transport transport = mailSession.getTransport("smtps");//這裡從getTransport()改成getTransport("smtps")
  48.  
  49. MimeMessage message = new MimeMessage(mailSession);
  50. message.setSubject(subject);
  51. message.setContent(content, "text/plain");
  52.  
  53. message.addRecipient(Message.RecipientType.TO,
  54. new InternetAddress(Recipient));
  55.  
  56. transport.connect
  57. (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
  58.  
  59. transport.sendMessage(message,
  60. message.getRecipients(Message.RecipientType.TO));
  61. transport.close();
  62. return 0;
  63. }catch(NoSuchProviderException e){
  64. e.printStackTrace();
  65. }catch(Exception e){
  66. e.printStackTrace();
  67. }
  68. return -1;
  69. }
  70. public static int findSubject(String Server, String user_account, String password, String subject){
  71. /*傳回某個account有沒有某封主旨為subject的信件,有則傳回編號,沒有傳回-1*/
  72. Properties props = System.getProperties();
  73. int isFound = -1;
  74.  
  75. if(Server.equalsIgnoreCase("pop3.live.com")){
  76. props.setProperty("mail.store.protocol", "pop3");
  77. }else if(Server.equalsIgnoreCase("imap.gmail.com")) {
  78. props.setProperty("mail.store.protocol", "imaps");
  79. }else{
  80. System.out.println("Error! Neigher \"pop3.live.com\" or \"imap.gmail.com\"");
  81. }
  82. try
  83. {
  84. Session session = Session.getDefaultInstance(props, null);
  85. Store store;
  86. if(Server.equalsIgnoreCase("pop3.live.com")){
  87. store = session.getStore("pop3");
  88. }else{
  89. store = session.getStore("imaps");
  90. }
  91. store.connect(Server, user_account, password);
  92.  
  93. Folder inbox = store.getFolder("Inbox");
  94. inbox.open(Folder.READ_ONLY);
  95.  
  96. Message messages[] = inbox.getMessages();
  97. System.out.print("檢查郵件:");
  98. for(int i=0;i<messages.length;i++)
  99. {
  100. System.out.print((i+1) + " ");
  101. if( messages[i].getSubject().equalsIgnoreCase(subject) ){
  102. System.out.println("\n找到主旨:" + subject + " 傳回編號:" + i);
  103. isFound = i;
  104. break;
  105. }
  106. }
  107. System.out.println("");
  108. inbox.close(false);
  109. store.close();
  110. return isFound;
  111. }catch (NoSuchProviderException e) {
  112. e.printStackTrace();
  113. System.exit(1);
  114. } catch (MessagingException e) {
  115. e.printStackTrace();
  116. System.exit(2);
  117. }
  118. return isFound;
  119. }
  120.  
  121. public static String getContentOfSubject(String Server, String user_account, String password, String subject){
  122. /*
  123. * 傳回某個account的某subject的內容,若無內容,則傳回空內容。
  124. */
  125. Properties props = System.getProperties();
  126. String content = new String();
  127.  
  128. if(Server.equalsIgnoreCase("pop3.live.com")){
  129. props.setProperty("mail.store.protocol", "pop3");
  130. }else if(Server.equalsIgnoreCase("imap.gmail.com")) {
  131. props.setProperty("mail.store.protocol", "imaps");
  132. }else{
  133. System.out.println("Error! Neigher \"pop3.live.com\" or \"imap.gmail.com\"");
  134. }
  135. try
  136. {
  137. Session session = Session.getDefaultInstance(props, null);
  138. Store store;
  139. if(Server.equalsIgnoreCase("pop3.live.com")){
  140. store = session.getStore("pop3");
  141. }else{
  142. store = session.getStore("imaps");
  143. }
  144. store.connect(Server, user_account, password);
  145.  
  146. Folder inbox = store.getFolder("Inbox");
  147. inbox.open(Folder.READ_ONLY);
  148.  
  149. Message messages[] = inbox.getMessages();
  150. for(int i=0;i<messages.length;i++)
  151. {
  152. System.out.println("處理第"+(i+1)+"封郵件");
  153. if( messages[i].getSubject().equalsIgnoreCase(subject) ){
  154. System.out.println("找到主旨:" + subject + " 傳回編號:" + i);
  155. content="";
  156. Object body = messages[i].getContent();
  157. Part p = messages[i];
  158. if(body instanceof Multipart){
  159. System.out.println("此郵件為Multipart");
  160. p = ((Multipart)body).getBodyPart(0);
  161. }
  162. String contentType = p.getContentType();
  163. String thisLine="";
  164. System.out.println("ContentType:" + contentType);
  165. if(contentType.toUpperCase().startsWith("TEXT/PLAIN") || contentType.toUpperCase().startsWith("TEXT/HTML"))
  166. {
  167. System.out.println("為text/plain或text/html ===> 可處理");
  168. BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
  169. while( (thisLine = in.readLine())!=null){
  170. System.out.println("讀到:" + thisLine);
  171. content+=thisLine + "\r\n";
  172. }
  173. }
  174. else
  175. {
  176. System.out.println("不為text/plain或text/html");
  177. }
  178. break;
  179. }
  180. }
  181. inbox.close(false);
  182. store.close();
  183. return content;
  184. }catch (NoSuchProviderException e) {
  185. e.printStackTrace();
  186. System.exit(1);
  187. } catch (MessagingException e) {
  188. e.printStackTrace();
  189. System.exit(2);
  190. } catch (IOException e) {
  191. e.printStackTrace();
  192. }
  193. return content;
  194. }
  195.  
  196. public static void processMultipart(Multipart mp, String user_account,int numOfMsg ,String subject)
  197. {
  198. try {
  199. for(int i=0;i<mp.getCount();i++)
  200. {
  201. if(i>=1)
  202. subject = subject + "_" + (i+1) ;
  203. processPart(mp.getBodyPart(i),user_account,numOfMsg,subject);
  204. }
  205. } catch (MessagingException e) {
  206. e.printStackTrace();
  207. }
  208. }
  209. public static void processPart(Part p,String user_account,int numOfMsg,String subject)
  210. {
  211. try{
  212. String contentType = p.getContentType();
  213. StringBuffer contentString = new StringBuffer();
  214. String thisLine="";
  215. System.out.println("ContentType:" + contentType);
  216. if(contentType.toUpperCase().startsWith("TEXT/PLAIN") || contentType.toUpperCase().startsWith("TEXT/HTML"))
  217. {
  218. System.out.println("為text/plain或text/html ===> 可處理");
  219.  
  220. //讀取郵件的reader
  221. InputStream reader = p.getInputStream();
  222. reader = new BufferedInputStream(reader);
  223.  
  224.  
  225. //建立專門資料夾存放郵件內容
  226. File mailDir = new File(user_account+"_Email");
  227. mailDir.mkdir();
  228.  
  229. File aFile=new File( mailDir.getCanonicalPath()+"\\" +(numOfMsg)+"."+subject+".txt");
  230.  
  231. //將每封郵件寫到檔案的writer
  232. FileOutputStream out = new FileOutputStream(aFile);
  233. BufferedOutputStream bos = new BufferedOutputStream(out);
  234. int b;
  235. while( (b=reader.read())!=-1 )
  236. {
  237. bos.write(b);
  238. }
  239. bos.flush();
  240. bos.close();
  241. reader.close();
  242. }
  243. else{
  244. System.out.println("***************************************");
  245. System.out.println("************Warning! 警告**************");
  246. System.out.println("***************************************");
  247. System.out.printf("numOfMsg:%d Subject:%s\n",numOfMsg,subject);
  248. System.out.println("非為text/plain或text/html類型 ===> 真的遇到例外,該做事了!");
  249. System.out.println("\t方向:使用JavaBean Activation FrameWork的DataHandler");
  250. }
  251. }catch(Exception e){
  252. System.err.println(e);
  253. e.printStackTrace();
  254. }
  255. }
  256.  
  257. }
Add Comment
Please, Sign In to add comment