Advertisement
Guest User

Untitled

a guest
Aug 6th, 2016
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. import java.util.*;
  2. import javax.mail.Folder;
  3. import javax.mail.Message;
  4. import javax.mail.Message.RecipientType;
  5. import javax.mail.MessagingException;
  6. import javax.mail.Session;
  7. import javax.mail.Store;
  8. import javax.mail.Transport;
  9. import javax.mail.internet.AddressException;
  10. import javax.mail.internet.InternetAddress;
  11. import javax.mail.internet.MimeMessage;
  12. import javax.swing.JOptionPane;
  13.  
  14. public class GmailClient {
  15.  
  16. private String userName;
  17. private String password;
  18. private String receivingHost;
  19.  
  20. public void setAccountDetails(String userName,String password){
  21.  
  22. this.userName=userName;//sender's email can also use as User Name
  23. this.password=password;
  24.  
  25. }
  26.  
  27. public void readGmail(){
  28.  
  29. /*this will print subject of all messages in the inbox of sender@gmail.com*/
  30.  
  31. this.receivingHost="imap.gmail.com";//for imap protocol
  32.  
  33. Properties props2=System.getProperties();
  34.  
  35. props2.setProperty("mail.store.protocol", "imaps");
  36. // I used imaps protocol here
  37.  
  38. Session session2=Session.getDefaultInstance(props2, null);
  39.  
  40. try {
  41.  
  42. Store store=session2.getStore("imaps");
  43.  
  44. store.connect(this.receivingHost,this.userName, this.password);
  45.  
  46. Folder folder=store.getFolder("INBOX");//get inbox
  47.  
  48. folder.open(Folder.READ_ONLY);//open folder only to read
  49.  
  50. Message message[]=folder.getMessages();
  51.  
  52. for(int i=0;i<message.length;i++){
  53.  
  54. //print subjects of all mails in the inbox
  55.  
  56. System.out.println(message[i].getSubject());
  57.  
  58. //anything else you want
  59.  
  60. }
  61.  
  62. //close connections
  63.  
  64. folder.close(true);
  65.  
  66. store.close();
  67.  
  68. } catch (Exception e) {
  69.  
  70. System.out.println(e.toString());
  71.  
  72. }
  73.  
  74. }
  75.  
  76. public static void main(String[] args) {
  77.  
  78. String mailFrom=new String("sender@gmail.com");
  79.  
  80. //Sender must be a Gmail Account holder
  81.  
  82. String mailTo=new String("receiver@gmail.com");
  83.  
  84. //but here you can send to any type of mail account
  85.  
  86. String senderPassword=new String("1234");
  87.  
  88. String senderUserName=new String("sender");
  89.  
  90. //Mention your email subject and content
  91.  
  92. String mailSubject=new String("Testing Mail");
  93.  
  94. String mailText=new String("Have an Nice Day ...........!!!");
  95.  
  96. //Create a GmailClient object
  97.  
  98. GmailClient newGmailClient=new GmailClient();
  99.  
  100. //Setting up account details
  101.  
  102. newGmailClient.setAccountDetails(senderUserName, senderPassword);
  103.  
  104. //Send mail
  105.  
  106. newGmailClient.sendGmail(mailFrom, mailTo, mailSubject, mailText);
  107.  
  108. //Receive mails
  109.  
  110. newGmailClient.readGmail();
  111.  
  112. }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement