· 8 years ago · May 05, 2018, 05:04 PM
1import java.awt.Toolkit;
2import java.awt.datatransfer.Clipboard;
3import java.awt.datatransfer.StringSelection;
4import java.io.FileNotFoundException;
5import java.io.FileReader;
6import java.util.*;
7import javax.mail.*;
8import javax.mail.internet.*;
9
10public class TestEmail {
11
12 private static String USER_NAME = "USERNAME"; // GMail user name (just the part before "@gmail.com")
13 private static String PASSWORD = "PASSWORD"; // GMail password
14 private static String RECIPIENT = "trigger@recipe.ifttt.com"; //i used ifttt to send email to tumblr to add the post a queue item.
15
16 public static void main(String[] args) throws FileNotFoundException {
17 String from = USER_NAME;
18 String pass = PASSWORD;
19 String[] to = { RECIPIENT }; // list of recipient email addresses
20
21 Scanner kb = new Scanner(System.in);
22 Scanner sc = new Scanner(new FileReader("text.txt")); //text file contains title and body delimited by an ";"
23 String[] title = null;
24 for (int i=0;i<100;i++){
25
26 String line = sc.nextLine();
27 title = line.split(";");
28 StringSelection stringSelection = new StringSelection (title[0]);
29 Clipboard clpbrd = Toolkit.getDefaultToolkit ().getSystemClipboard ();
30 clpbrd.setContents (stringSelection, null);
31 System.out.println("title: "+title[0]);
32 //kb.next();
33 //Thread.sleep(2000);
34 stringSelection = new StringSelection (title[1]);
35 clpbrd = Toolkit.getDefaultToolkit ().getSystemClipboard ();
36 clpbrd.setContents (stringSelection, null);
37 System.out.println("\ndef: "+title[1]+"\n\n"); /debugging purposes
38 //Thread.sleep(5000);
39 sendFromGMail(from, pass, to, title[0], title[1]);
40 //kb.next();
41
42 }
43 }
44
45 private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
46 Properties props = System.getProperties();
47 String host = "smtp.gmail.com";
48 props.put("mail.smtp.starttls.enable", "true");
49 props.put("mail.smtp.host", host);
50 props.put("mail.smtp.user", from);
51 props.put("mail.smtp.password", pass);
52 props.put("mail.smtp.port", "587");
53 props.put("mail.smtp.auth", "true");
54
55 Session session = Session.getDefaultInstance(props);
56 MimeMessage message = new MimeMessage(session);
57
58 try {
59 message.setFrom(new InternetAddress(from));
60 InternetAddress[] toAddress = new InternetAddress[to.length];
61
62 // To get the array of addresses
63 for( int i = 0; i < to.length; i++ ) {
64 toAddress[i] = new InternetAddress(to[i]);
65 }
66
67 for( int i = 0; i < toAddress.length; i++) {
68 message.addRecipient(Message.RecipientType.TO, toAddress[i]);
69 }
70
71 message.setSubject(subject);
72 message.setText(body);
73 Transport transport = session.getTransport("smtp");
74 transport.connect(host, from, pass);
75 transport.sendMessage(message, message.getAllRecipients());
76 transport.close();
77 }
78 catch (AddressException ae) {
79 ae.printStackTrace();
80 }
81 catch (MessagingException me) {
82 me.printStackTrace();
83 }
84 }
85}