Guest User

Untitled

a guest
Aug 5th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.22 KB | None | 0 0
  1. Where must i copy the default.properties file in Netbeans 7.1 for java code?
  2. key=value
  3. key2=value2
  4. #comment1
  5.  
  6. package inboxreader;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.FileInputStream;
  10. import java.io.FileReader;
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.PreparedStatement;
  14. import java.util.HashSet;
  15. import java.util.Properties;
  16. import java.util.regex.Matcher;
  17. import java.util.regex.Pattern;
  18.  
  19. import javax.mail.Address;
  20. import javax.mail.Flags;
  21. import javax.mail.Folder;
  22. import javax.mail.Message;
  23. import javax.mail.MessagingException;
  24. import javax.mail.NoSuchProviderException;
  25. import javax.mail.Part;
  26. import javax.mail.Session;
  27. import javax.mail.Store;
  28. import javax.mail.internet.MimeBodyPart;
  29. import javax.mail.internet.MimeMultipart;
  30.  
  31. public class InboxReader {
  32. static HashSet<String> mails;
  33. public static void main(String args[])
  34. {
  35. while(true)
  36. {
  37. try {
  38. System.out.println("Started.......");
  39. Start();
  40. System.out.println("...Read completed.......");
  41.  
  42.  
  43. try {
  44. Thread.sleep(1000*60*5);
  45. } catch (InterruptedException e1) {
  46.  
  47.  
  48. }
  49.  
  50. } catch (Exception e) {
  51.  
  52. try {connecttoMySql();
  53.  
  54. e.printStackTrace();
  55. System.out.println("..Error in connection Sleeping...");
  56.  
  57. } catch (Exception e1) {
  58.  
  59.  
  60. }
  61. }
  62. }
  63.  
  64. }
  65. public static void Start() throws Exception {
  66. Properties props = System.getProperties();
  67.  
  68. props.setProperty("mail.store.protocol", "imaps");
  69. try {
  70. Session session = Session.getDefaultInstance(props, null);
  71. Store store = session.getStore("imaps");
  72. store.connect("imap.gmail.com", "email@gmail.com", "password");
  73. System.out.println(store);
  74. int cout=0;
  75.  
  76. Folder inbox = store.getFolder("Inbox");
  77. inbox.open(Folder.READ_WRITE);
  78. Message messages[] = inbox.getMessages();
  79. for(Message message:messages) {
  80. mails=new HashSet<String>();
  81. System.out.println("Reading:"+ (messages.length-cout));
  82. cout++;
  83. InboxReader.storeAddresses(message);
  84. dumpPart(message);
  85. for(String temp:mails)
  86. System.out.println(temp);
  87. connecttoMySql();
  88. message.setFlag(Flags.Flag.DELETED, true);
  89. }
  90.  
  91.  
  92.  
  93. } catch (NoSuchProviderException e) {
  94. connecttoMySql();
  95. e.printStackTrace();
  96.  
  97. } catch (MessagingException e) {
  98. connecttoMySql();
  99. e.printStackTrace();
  100.  
  101. }
  102.  
  103. }
  104. public static void storeAddresses(Message msg)
  105. {
  106. try {
  107. for(Address adr:msg.getAllRecipients())
  108. {
  109. addAddresses(adr.toString());
  110.  
  111. }
  112.  
  113. } catch (Exception e) {
  114.  
  115. e.printStackTrace();
  116. }
  117.  
  118. }
  119. public static void addAddresses(String input_text)
  120. {
  121. Pattern p= Pattern.compile("[A-Z0-9\._%\+\-]+@[A-Z0-9\.\-]+\.[A-Z]{2,4}",Pattern.CASE_INSENSITIVE);
  122.  
  123. Matcher m=p.matcher(input_text);
  124. while(m.find())
  125. {
  126. mails.add(m.group());
  127.  
  128. }
  129.  
  130. }
  131. public static void dumpPart(Part p) throws Exception {
  132.  
  133.  
  134.  
  135. if (p.isMimeType("text/plain")) {
  136.  
  137. try{
  138. addAddresses((String)p.getContent());
  139. }catch(Exception e){}
  140. } else {
  141.  
  142. MimeMultipart mb = null;
  143. try{
  144. mb=(MimeMultipart ) (p.getContent());
  145. }
  146. catch(Exception e)
  147. { try{
  148. if(p.getContent() instanceof String)
  149.  
  150.  
  151. addAddresses((String)p.getContent());
  152. }catch(Exception e1){}
  153. return;
  154. }
  155.  
  156. MimeBodyPart mb1=(MimeBodyPart) mb.getBodyPart(0);
  157. mb1.saveFile("emailtext.html");
  158. BufferedReader br = new BufferedReader(new FileReader("emailtext.html"));
  159. StringBuffer content = new StringBuffer();
  160. String line ="";
  161. while((line = br.readLine())!= null )
  162. {
  163. if(line.length()>=2)if(line.substring(line.length()-1).equals("="))
  164. {
  165. content.append(line.substring(line.length()-1) );
  166. }else
  167. content.append(line+"n");
  168. }
  169. addAddresses(content.toString());
  170.  
  171.  
  172. }
  173. }
  174. public static void connecttoMySql()
  175. {
  176. Connection conn = null;
  177.  
  178. try
  179. {
  180. Properties details= new Properties();
  181. details.load(new FileInputStream("details.properties"));
  182. String userName = details.getProperty("root");
  183. String password = details.getProperty("password_of-mysql");
  184. String url = details.getProperty("jdbc:mysql://localhost/Test");
  185. Class.forName ("com.mysql.jdbc.Driver").newInstance ();
  186. conn = DriverManager.getConnection (url, userName, password);
  187. System.out.println ("Database connection established");
  188. PreparedStatement st= conn.prepareStatement("insert into `Email_list` values(?)");
  189. for(String mail:mails)
  190. {
  191. try{
  192. st.setString(1, mail);
  193. st.execute();
  194. }catch(Exception e){}
  195. }
  196.  
  197.  
  198.  
  199. }
  200. catch (Exception e)
  201. {
  202. System.err.println ("Cannot connect to database server");
  203. e.printStackTrace();
  204. }
  205. finally
  206. {
  207. if (conn != null)
  208. {
  209. try
  210. {
  211. conn.close ();
  212. System.out.println ("Database connection terminated");
  213. }
  214. catch (Exception e) { }
  215. }
  216. }
  217. }
  218.  
  219.  
  220.  
  221. }
  222.  
  223. Cannot connect to database server
  224. java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
  225. at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
  226. at java.security.AccessController.doPrivileged(Native Method)
  227. at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
  228. at java.lang.ClassLoader.loadClass(ClassLoader.java:303)
  229. at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
  230. at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
  231. at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
  232. at java.lang.Class.forName0(Native Method)
  233. at java.lang.Class.forName(Class.java:169)
  234. at inboxreader.InboxReader.connecttoMySql(InboxReader.java:180)
  235. at inboxreader.InboxReader.main(InboxReader.java:47)
  236. com.sun.mail.util.DecodingException: BASE64Decoder: Error in encoded stream: needed 4 valid base64 characters but only got 2 before EOF, the 10 most recent characters were: "AKxCo9RUjD"
  237. ..Error in connection Sleeping...
  238.  
  239. import java.io.FileOutputStream;
  240. import java.io.IOException;
  241. import java.util.Properties;
  242.  
  243. public class App
  244. {
  245. public static void main( String[] args )
  246. {
  247. Properties prop = new Properties();
  248.  
  249. try {
  250. //set the properties value
  251. prop.setProperty("database", "localhost");
  252. prop.setProperty("dbuser", "root");
  253. prop.setProperty("dbpassword", "password");
  254.  
  255. //save properties to project root folder
  256. prop.store(new FileOutputStream("default.properties.properties"), null);
  257.  
  258. } catch (IOException ex) {
  259. ex.printStackTrace();
  260. }
  261. }
  262. }
Add Comment
Please, Sign In to add comment