This repository has been archived by the owner on Jan 19, 2019. It is now read-only.
/
Mail.java
99 lines (84 loc) · 2.97 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package models.utils;
import com.typesafe.plugin.MailerAPI;
import com.typesafe.plugin.MailerPlugin;
import play.Configuration;
import play.Logger;
import play.libs.Akka;
import scala.concurrent.duration.Duration;
import scala.concurrent.duration.FiniteDuration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* Send a mail with PlayStartApp.
* <p/>
* User: yesnault
* Date: 24/01/12
*/
public class Mail {
/**
* 1 second delay on sending emails
*/
private static final int DELAY = 1;
/**
* Envelop to prepare.
*/
public static class Envelop {
private String subject;
private String message;
private List<String> toEmails;
/**
* Constructor of Envelop.
*
* @param subject the subject
* @param message a message
* @param toEmails list of emails adress
*/
public Envelop(String subject, String message, List<String> toEmails) {
this.subject = subject;
this.message = message;
this.toEmails = toEmails;
}
public Envelop(String subject, String message, String email) {
this.message = message;
this.subject = subject;
this.toEmails = new ArrayList<String>();
this.toEmails.add(email);
}
}
/**
* Send a email, using Akka to offload it to an actor.
*
* @param envelop envelop to send
*/
public static void sendMail(Mail.Envelop envelop) {
EnvelopJob envelopJob = new EnvelopJob(envelop);
final FiniteDuration delay = Duration.create(DELAY, TimeUnit.SECONDS);
Akka.system().scheduler().scheduleOnce(delay, envelopJob, Akka.system().dispatcher());
}
static class EnvelopJob implements Runnable {
Mail.Envelop envelop;
public EnvelopJob(Mail.Envelop envelop) {
this.envelop = envelop;
}
public void run() {
MailerAPI email = play.Play.application().plugin(MailerPlugin.class).email();
final Configuration root = Configuration.root();
final String mailFrom = root.getString("mail.from");
email.setFrom(mailFrom);
email.setSubject(envelop.subject);
for (String toEmail : envelop.toEmails) {
email.setRecipient(toEmail);
Logger.debug("Mail.sendMail: Mail will be sent to " + toEmail);
}
final String mailSign = root.getString("mail.sign");
email.send(envelop.message + "\n\n " + mailSign,
envelop.message + "<br><br>--<br>" + mailSign);
Logger.debug("Mail sent - SMTP:" + root.getString("smtp.host")
+ ":" + root.getString("smtp.port")
+ " SSL:" + root.getString("smtp.ssl")
+ " user:" + root.getString("smtp.user")
+ " password:" + root.getString("smtp.password"));
}
}
}