-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mail.java
83 lines (67 loc) · 2.43 KB
/
Mail.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//package dataCollection;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Mail{
String smtpHost = "";
String smtpPort = "";
String gmailSender = "";
String gmailPwd = "";
String gmailRecvr = "";
Mail(){
// default constructor
}
Mail(String sender, String recvr, String pwd){
this.smtpHost = "smtp.gmail.com";
this.smtpPort = "587";
this.gmailSender = sender;
this.gmailPwd = pwd;
this.gmailRecvr = recvr;
}
public <E> void send(E msgTxt) {
Properties prop = System.getProperties();
prop.setProperty("mail.smtp.auth", "true");
prop.setProperty("mail.smtp.starttls.enable", "true");
prop.setProperty("mail.smtp.host", this.smtpHost);
prop.setProperty("mail.smtp.port", this.smtpPort);
Session session = Session.getInstance(prop, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(gmailSender, gmailPwd);
}
});
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(this.gmailSender));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(this.gmailRecvr));
message.setSubject("Test RSSServer still running");
message.setText((String)msgTxt);
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
System.out.println((new Date()).toString() + " Message exception: " + e.getMessage());
//e.printStackTrace();
}
}
public void test() {
try{
this.send("test message");
}catch(Exception e){
System.out.println("Error sending Mail " + e.getMessage());
System.exit(-1);
}
}
public static void main(String[] args) throws IOException{
Mail m = new Mail("senderTest@gmail.com", "recvrTest@gmail.com", "mypassword12345");
m.test();
}
}