-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mailthread.java
99 lines (90 loc) · 3.66 KB
/
Mailthread.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Developer : Hitesh Sethi
* This class is implemented for making threads.
* This class is mainly responsible for sending message
*/
package massmailer;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Mailthread extends Thread{
private int id;
private String to,from,body,subject;
private boolean flag_problem = true;
public Mailthread(int id,String to,String from,String subject,String body)
{
//assigning the variables to the local variables of the class
this.to = to;
this.id = id;
this.from = from;
this.subject = subject;
this.body = body;
}
@Override
public void run()
{
//implementing the overriden function
final String username = "hiteshsethi";//change accordingly
final String password = "*******";//change accordingly
String host = "smtp.sendgrid.net";
//String host = "smtp.gmail.com";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
// Get the Session object.
final Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try{
// Create a default MimeMessage object.
Message message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
// Set Subject: header field
message.setSubject(subject);
// Now set the actual message
message.setText(body);
Transport t = session.getTransport(); //initialising the Transport object
t.connect(); //making the connection to the DB
try{
t.sendMessage(message, message.getAllRecipients()); //sending the mail to SMTP server
System.out.println("Sent message successfully...."+message.getSubject()+" "+id);
flag_problem = false; //no problem
}
catch(Exception e)
{
flag_problem = true;
System.out.println("Not able to send this message...."+message.getSubject()+" "+id+" due to error "+e);
}
t.close(); //closing the connection
}
catch(Exception e)
{
System.out.println("Problem in connection. Error :"+e);
}
}
String getStatus()
{
if(flag_problem)
{
//if there was a problem to T for Tried
return "T,"+id;
}
else
{
//success
return "S,"+id;
}
}
}