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.69 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 = "-1";
  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. System.out.println("處理郵件編號:");
  151. for(int i=0;i<messages.length;i++)
  152. {
  153. System.out.print((i+1) + " ");
  154. if( messages[i].getSubject().equalsIgnoreCase(subject) ){
  155. System.out.println("\n找到主旨:" + subject + " 傳回編號:" + i);
  156. content="";
  157. Object body = messages[i].getContent();
  158. Part p = messages[i];
  159. if(body instanceof Multipart){
  160. System.out.println("此郵件為Multipart");
  161. p = ((Multipart)body).getBodyPart(0);
  162. }
  163. String contentType = p.getContentType();
  164. String thisLine="";
  165. System.out.println("ContentType:" + contentType);
  166. if(contentType.toUpperCase().startsWith("TEXT/PLAIN") || contentType.toUpperCase().startsWith("TEXT/HTML"))
  167. {
  168. System.out.println("為text/plain或text/html ===> 可處理");
  169. BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
  170. while( (thisLine = in.readLine())!=null){
  171. System.out.println("讀到:" + thisLine);
  172. content+=thisLine + "\r\n";
  173. }
  174. }
  175. else
  176. {
  177. System.out.println("不為text/plain或text/html");
  178. }
  179. break;
  180. }
  181. }
  182. inbox.close(false);
  183. store.close();
  184. return content;
  185. }catch (NoSuchProviderException e) {
  186. e.printStackTrace();
  187. System.exit(1);
  188. } catch (MessagingException e) {
  189. e.printStackTrace();
  190. System.exit(2);
  191. } catch (IOException e) {
  192. e.printStackTrace();
  193. }
  194. return content;
  195. }
  196.  
  197. public static void processMultipart(Multipart mp, String user_account,int numOfMsg ,String subject)
  198. {
  199. try {
  200. for(int i=0;i<mp.getCount();i++)
  201. {
  202. if(i>=1)
  203. subject = subject + "_" + (i+1) ;
  204. processPart(mp.getBodyPart(i),user_account,numOfMsg,subject);
  205. }
  206. } catch (MessagingException e) {
  207. e.printStackTrace();
  208. }
  209. }
  210. public static void processPart(Part p,String user_account,int numOfMsg,String subject)
  211. {
  212. try{
  213. String contentType = p.getContentType();
  214. StringBuffer contentString = new StringBuffer();
  215. String thisLine="";
  216. System.out.println("ContentType:" + contentType);
  217. if(contentType.toUpperCase().startsWith("TEXT/PLAIN") || contentType.toUpperCase().startsWith("TEXT/HTML"))
  218. {
  219. System.out.println("為text/plain或text/html ===> 可處理");
  220.  
  221. //讀取郵件的reader
  222. InputStream reader = p.getInputStream();
  223. reader = new BufferedInputStream(reader);
  224.  
  225.  
  226. //建立專門資料夾存放郵件內容
  227. File mailDir = new File(user_account+"_Email");
  228. mailDir.mkdir();
  229.  
  230. File aFile=new File( mailDir.getCanonicalPath()+"\\" +(numOfMsg)+"."+subject+".txt");
  231.  
  232. //將每封郵件寫到檔案的writer
  233. FileOutputStream out = new FileOutputStream(aFile);
  234. BufferedOutputStream bos = new BufferedOutputStream(out);
  235. int b;
  236. while( (b=reader.read())!=-1 )
  237. {
  238. bos.write(b);
  239. }
  240. bos.flush();
  241. bos.close();
  242. reader.close();
  243. }
  244. else{
  245. System.out.println("***************************************");
  246. System.out.println("************Warning! 警告**************");
  247. System.out.println("***************************************");
  248. System.out.printf("numOfMsg:%d Subject:%s\n",numOfMsg,subject);
  249. System.out.println("非為text/plain或text/html類型 ===> 真的遇到例外,該做事了!");
  250. System.out.println("\t方向:使用JavaBean Activation FrameWork的DataHandler");
  251. }
  252. }catch(Exception e){
  253. System.err.println(e);
  254. e.printStackTrace();
  255. }
  256. }
  257.  
  258. }
Add Comment
Please, Sign In to add comment