email sender: rebuild Email send feature with play-email plugins
- replace apache commons to Play-2-Mailplugin see: https://github.com/schleichardt/Play-2-Mailplugin - add email send test
@26d0ab17b29fcdd868975c07ad33b6b3451fdab7
--- app/controllers/SiteApp.java
+++ app/controllers/SiteApp.java
... | ... | @@ -4,15 +4,15 @@ |
4 | 4 |
import java.util.List; |
5 | 5 |
import java.util.Map; |
6 | 6 |
|
7 |
-import org.apache.commons.mail.EmailException; |
|
7 |
+import org.apache.commons.mail.*; |
|
8 | 8 |
|
9 | 9 |
import models.Project; |
10 | 10 |
import models.User; |
11 |
+import org.apache.commons.mail.SimpleEmail; |
|
11 | 12 |
import play.Configuration; |
12 | 13 |
import play.mvc.Controller; |
13 | 14 |
import play.mvc.Result; |
14 | 15 |
import utils.Constants; |
15 |
-import utils.Mailer; |
|
16 | 16 |
|
17 | 17 |
import views.html.site.setting; |
18 | 18 |
import views.html.site.mail; |
... | ... | @@ -21,35 +21,30 @@ |
21 | 21 |
|
22 | 22 |
import com.avaje.ebean.Page; |
23 | 23 |
import static play.data.Form.form; |
24 |
+import info.schleichardt.play2.mailplugin.Mailer; |
|
24 | 25 |
|
25 | 26 |
public class SiteApp extends Controller { |
26 |
- |
|
27 |
- public static Result sendMail() { |
|
28 |
- Mailer email = new Mailer(play.Play.application()); |
|
27 |
+ public static Result sendMail() throws EmailException{ |
|
28 |
+ SimpleEmail email = new SimpleEmail(); |
|
29 | 29 |
|
30 | 30 |
Map<String, String[]> formData = request().body().asFormUrlEncoded(); |
31 |
- email.addFrom(utils.HttpUtil.getFirstValueFromQuery(formData, "from")); |
|
31 |
+ email.setFrom(utils.HttpUtil.getFirstValueFromQuery(formData, "from")); |
|
32 | 32 |
email.setSubject(utils.HttpUtil.getFirstValueFromQuery(formData, "subject")); |
33 |
- email.addRecipient(utils.HttpUtil.getFirstValueFromQuery(formData, "to")); |
|
33 |
+ email.addTo(utils.HttpUtil.getFirstValueFromQuery(formData, "to")); |
|
34 |
+ email.setMsg(utils.HttpUtil.getFirstValueFromQuery(formData, "body")); |
|
35 |
+ email.setCharset("utf-8"); |
|
34 | 36 |
|
35 | 37 |
String errorMessage = null; |
36 | 38 |
boolean sended = false; |
37 |
- try { |
|
38 |
- email.send(utils.HttpUtil.getFirstValueFromQuery(formData, "body")); |
|
39 |
- sended = true; |
|
40 |
- } catch (EmailException e) { |
|
41 |
- errorMessage = e.toString(); |
|
42 |
- if (e.getCause() != null) { |
|
43 |
- errorMessage += "<br/>Caused by: " + e.getCause(); |
|
44 |
- } |
|
45 |
- } |
|
46 |
- |
|
39 |
+ String result = Mailer.send(email); |
|
40 |
+ System.out.println(">>>" + result); |
|
41 |
+ sended = true; |
|
47 | 42 |
return writeMail(errorMessage, sended); |
48 | 43 |
} |
49 | 44 |
|
50 | 45 |
public static Result writeMail(String errorMessage, boolean sended) { |
51 | 46 |
Configuration config = play.Play.application().configuration(); |
52 |
- List<String> notConfiguredItems = new ArrayList<String>(); |
|
47 |
+ List<String> notConfiguredItems = new ArrayList<>(); |
|
53 | 48 |
String[] requiredItems = {"smtp.host", "smtp.user", "smtp.password"}; |
54 | 49 |
for(String key : requiredItems) { |
55 | 50 |
if (config.getString(key) == null) { |
... | ... | @@ -57,13 +52,9 @@ |
57 | 52 |
} |
58 | 53 |
} |
59 | 54 |
|
60 |
- String sender = config.getString("smtp.user"); |
|
55 |
+ String sender = config.getString("smtp.user") + "@" + config.getString("smtp.domain"); |
|
61 | 56 |
|
62 | 57 |
return ok(mail.render("title.sendMail", notConfiguredItems, sender, errorMessage, sended)); |
63 |
- } |
|
64 |
- |
|
65 |
- public static Result writeMail() { |
|
66 |
- return writeMail(null, false); |
|
67 | 58 |
} |
68 | 59 |
|
69 | 60 |
public static Result setting() { |
--- app/utils/Mailer.java
... | ... | @@ -1,239 +0,0 @@ |
1 | -package utils; | |
2 | - | |
3 | -import java.util.ArrayList; | |
4 | -import java.util.Arrays; | |
5 | -import java.util.Collections; | |
6 | -import java.util.HashMap; | |
7 | -import java.util.Iterator; | |
8 | -import java.util.List; | |
9 | -import java.util.Map; | |
10 | - | |
11 | -import javax.mail.internet.AddressException; | |
12 | -import javax.mail.internet.InternetAddress; | |
13 | - | |
14 | -import org.apache.commons.mail.DefaultAuthenticator; | |
15 | -import org.apache.commons.mail.Email; | |
16 | -import org.apache.commons.mail.EmailException; | |
17 | -import org.apache.commons.mail.HtmlEmail; | |
18 | -import org.apache.commons.mail.MultiPartEmail; | |
19 | - | |
20 | -import play.Application; | |
21 | -//import scala.actors.threadpool.Arrays; | |
22 | - | |
23 | -public class Mailer { | |
24 | - private final String smtpHost; | |
25 | - private final Integer smtpPort; | |
26 | - private final Boolean smtpSsl; | |
27 | - private final Boolean smtpTls; | |
28 | - private final String smtpUser; | |
29 | - private final String smtpPassword; | |
30 | - | |
31 | - private final Map<String, List<String>> context = new HashMap<String, List<String>>(); | |
32 | - | |
33 | - public Mailer(Application app) { | |
34 | - smtpHost = app.configuration().getString("smtp.host"); | |
35 | - smtpPort = app.configuration().getInt("smtp.port"); | |
36 | - smtpSsl = app.configuration().getBoolean("smtp.ssl"); | |
37 | - smtpTls = app.configuration().getBoolean("smtp.tls"); | |
38 | - smtpUser = app.configuration().getString("smtp.user"); | |
39 | - smtpPassword = app.configuration().getString("smtp.password"); | |
40 | - } | |
41 | - | |
42 | - public String send(String bodyText) throws EmailException { | |
43 | - return send(bodyText, ""); | |
44 | - } | |
45 | - | |
46 | - public String sendHtml(String bodyHtml) throws EmailException { | |
47 | - return send("", bodyHtml); | |
48 | - } | |
49 | - | |
50 | - public String send(String bodyText, String bodyHtml) throws EmailException { | |
51 | - Email email = createEmailer(bodyText, bodyHtml); | |
52 | - email.setCharset(headOption("charset", "utf-8")); | |
53 | - email.setSubject(headOption("subject", "")); | |
54 | - | |
55 | - for (String fromEmail : e("from")) { | |
56 | - email.setFrom(addressFrom(fromEmail), nameFrom(fromEmail)); } | |
57 | - for(String replyToEmail : e("replyTo")) { | |
58 | - email.addReplyTo(addressFrom(replyToEmail), nameFrom(replyToEmail)); } | |
59 | - for(String recipient: e("recipients")) { | |
60 | - email.addTo(addressFrom(recipient), nameFrom(recipient)); } | |
61 | - for(String ccRecipient: e("ccRecipients")) { | |
62 | - email.addCc(addressFrom(ccRecipient), nameFrom(ccRecipient)); } | |
63 | - for(String bccRecipient: e("bccRecipients")) { | |
64 | - email.addBcc(addressFrom(bccRecipient), nameFrom(bccRecipient)); } | |
65 | - | |
66 | - Map<String, String> eheader = eheader(); | |
67 | - Iterator<String> it = eheader.keySet().iterator(); | |
68 | - while(it.hasNext()) { | |
69 | - String name = it.next(); | |
70 | - String value = eheader.get(name); | |
71 | - email.addHeader(name, value); | |
72 | - } | |
73 | - email.setHostName(smtpHost); | |
74 | - if (smtpPort != null) { | |
75 | - email.setSmtpPort(smtpPort); | |
76 | - } | |
77 | - if (smtpSsl != null) { | |
78 | - email.setSSL(smtpSsl); | |
79 | - } | |
80 | - if (smtpTls != null) { | |
81 | - email.setTLS(smtpTls); | |
82 | - } | |
83 | - email.setAuthenticator(new DefaultAuthenticator(smtpUser, smtpPassword)); | |
84 | - email.setDebug(false); | |
85 | - return email.send(); | |
86 | - } | |
87 | - | |
88 | - private String addressFrom(String emailAddress) { | |
89 | - return internetAddress(emailAddress).getAddress(); | |
90 | - } | |
91 | - | |
92 | - private String nameFrom(String emailAddress) { | |
93 | - return internetAddress(emailAddress).getPersonal(); | |
94 | - } | |
95 | - | |
96 | - private InternetAddress internetAddress(String emailAddress) { | |
97 | - InternetAddress iAddress = null; | |
98 | - if (emailAddress != null) { | |
99 | - try { | |
100 | - iAddress = new InternetAddress(emailAddress); | |
101 | - } catch (AddressException e) { | |
102 | - e.printStackTrace(); | |
103 | - } | |
104 | - } | |
105 | - return iAddress; | |
106 | - } | |
107 | - | |
108 | - private String headOption(String key, String defaultValue) { | |
109 | - List<String> values = e(key); | |
110 | - if (values.size() == 1) { | |
111 | - return values.get(0); | |
112 | - } else { | |
113 | - return defaultValue; | |
114 | - } | |
115 | - } | |
116 | - | |
117 | - /** | |
118 | - * Creates an appropriate email object based on the content type. | |
119 | - * | |
120 | - * @param bodyText | |
121 | - * @param bodyHtml | |
122 | - * @return | |
123 | - * @throws EmailException | |
124 | - */ | |
125 | - private Email createEmailer(String bodyText, String bodyHtml) | |
126 | - throws EmailException { | |
127 | - Email email = null; | |
128 | - if (bodyHtml == null || bodyHtml == "") { | |
129 | - email = new MultiPartEmail().setMsg(bodyText); | |
130 | - } else { | |
131 | - email = new HtmlEmail().setHtmlMsg(bodyHtml).setTextMsg(bodyText); | |
132 | - } | |
133 | - return email; | |
134 | - } | |
135 | - | |
136 | - private Map<String, String> eheader() { | |
137 | - Map<String, String> headers = new HashMap<String, String>(); | |
138 | - Iterator<String> it = context.keySet().iterator(); | |
139 | - while(it.hasNext()) { | |
140 | - String key = it.next(); | |
141 | - if(key.contains("header-")) { | |
142 | - headers.put(key.substring(7), context.get(key).get(0)); | |
143 | - } | |
144 | - } | |
145 | - return headers; | |
146 | - } | |
147 | - | |
148 | - /** | |
149 | - * extract parameter key from context | |
150 | - * | |
151 | - * @param key | |
152 | - */ | |
153 | - private List<String> e(String key) { | |
154 | - return context.containsKey(key) ? context.get(key) : Collections.<String>emptyList(); | |
155 | - } | |
156 | - | |
157 | - /** | |
158 | - * Sets a subject for this email. It enables formatting of the providing | |
159 | - * string using Java's string formatter. | |
160 | - * | |
161 | - * @param subject | |
162 | - * @param args | |
163 | - * TODO | |
164 | - */ | |
165 | - public void setSubject(String... subject) { | |
166 | - context.put("subject", Arrays.asList(subject)); | |
167 | - } | |
168 | - | |
169 | - /** | |
170 | - * Adds an email recipient ("to" addressee). | |
171 | - * | |
172 | - * @param recipients | |
173 | - */ | |
174 | - public void addRecipient(String... recipient) { | |
175 | - context.put("recipients", Arrays.asList(recipient)); | |
176 | - } | |
177 | - | |
178 | - /** | |
179 | - * Defines the sender of this email("from" address). | |
180 | - * | |
181 | - * @param from | |
182 | - */ | |
183 | - public void addFrom(String from) { | |
184 | - context.put("from", list(from)); | |
185 | - } | |
186 | - | |
187 | - private List<String> list(String string) { | |
188 | - List<String> list = new ArrayList<String>(); | |
189 | - list.add(string); | |
190 | - return list; | |
191 | - } | |
192 | - | |
193 | - /** | |
194 | - * Adds an email recipient in CC. | |
195 | - * | |
196 | - * @param ccRecipients | |
197 | - */ | |
198 | - public void addCc(String... ccRecipients) { | |
199 | - context.put("ccRecipients", Arrays.asList(ccRecipients)); | |
200 | - } | |
201 | - | |
202 | - /** | |
203 | - * Adds an email recipient in BCC. | |
204 | - * | |
205 | - * @param bccRecipients | |
206 | - */ | |
207 | - public void addBcc(String... bccRecipients) { | |
208 | - context.put("bccRecipients", Arrays.asList(bccRecipients)); | |
209 | - } | |
210 | - | |
211 | - /** | |
212 | - * Defines the "reply to" email address. | |
213 | - * | |
214 | - * @param replyTo | |
215 | - */ | |
216 | - public void setReplyTo(String replyTo) { | |
217 | - context.put("replyTo", list(replyTo)); | |
218 | - } | |
219 | - | |
220 | - /** | |
221 | - * Sets the charset for this email. | |
222 | - * | |
223 | - * @param charset | |
224 | - */ | |
225 | - public void setCharset(String charset) { | |
226 | - context.put("charset", list(charset)); | |
227 | - } | |
228 | - | |
229 | - /** | |
230 | - * Adds a request header to this email message. | |
231 | - * | |
232 | - * @param key | |
233 | - * @param value | |
234 | - */ | |
235 | - public void addHeader(String key, String value) { | |
236 | - context.put("header-" + key, list(value)); | |
237 | - } | |
238 | - | |
239 | -} |
--- conf/application.conf
+++ conf/application.conf
... | ... | @@ -82,12 +82,19 @@ |
82 | 82 |
# Mailer |
83 | 83 |
# ~~~~~~ |
84 | 84 |
# You have to configure SMTP to send mails. |
85 |
- |
|
86 |
-smtp.host = "smtp.gmail.com" |
|
85 |
+# Example settings, it assume that you use gamil smtp |
|
86 |
+smtp.host = smtp.gmail.com |
|
87 | 87 |
smtp.port = 465 |
88 |
-smtp.ssl = "yes" |
|
89 |
-smtp.user = "your mail address" |
|
90 |
-smtp.password = "your mail password" |
|
88 |
+smtp.ssl = true |
|
89 |
+smtp.user = yourGmailId |
|
90 |
+# Be careful!!! |
|
91 |
+smtp.password = yourGmailPassword |
|
92 |
+smtp.domain = gmail.com |
|
93 |
+ |
|
94 |
+#true to use mock mailer for testing, false for using real mail server |
|
95 |
+smtp.mock = true |
|
96 |
+# optional, size of mail archive for tests, default: 5 |
|
97 |
+smtp.archive.size = 5 |
|
91 | 98 |
|
92 | 99 |
# Akka |
93 | 100 |
akka.default-dispatcher.core-pool-size-max = 64 |
+++ conf/play.plugins
... | ... | @@ -0,0 +1,1 @@ |
1 | +15000:info.schleichardt.play2.mailplugin.MailPlugin(No newline at end of file) |
--- conf/routes
+++ conf/routes
... | ... | @@ -30,7 +30,7 @@ |
30 | 30 |
|
31 | 31 |
# Site |
32 | 32 |
GET /sites/setting controllers.SiteApp.setting() |
33 |
-GET /sites/mail controllers.SiteApp.writeMail() |
|
33 |
+GET /sites/mail controllers.SiteApp.writeMail(errorMessage:String ?= "", sended:Boolean ?= false) |
|
34 | 34 |
POST /sites/mail controllers.SiteApp.sendMail() |
35 | 35 |
GET /sites/userList controllers.SiteApp.userList(pageNum: Int ?= 0, loginId: String ?= null) |
36 | 36 |
POST /sites/userList controllers.SiteApp.searchUser() |
+++ test/utils/MailSendTest.java
... | ... | @@ -0,0 +1,68 @@ |
1 | +package utils; | |
2 | + | |
3 | +import controllers.routes; | |
4 | +import info.schleichardt.play2.mailplugin.Mailer; | |
5 | +import junit.framework.Assert; | |
6 | +import models.ModelTest; | |
7 | +import org.apache.commons.mail.EmailException; | |
8 | +import org.apache.commons.mail.SimpleEmail; | |
9 | +import org.junit.After; | |
10 | +import org.junit.Before; | |
11 | +import org.junit.BeforeClass; | |
12 | +import org.junit.Test; | |
13 | +import play.test.FakeApplication; | |
14 | +import play.test.Helpers; | |
15 | + | |
16 | +import java.util.Map; | |
17 | + | |
18 | +import static org.fest.assertions.Assertions.assertThat; | |
19 | +import static play.test.Helpers.callAction; | |
20 | +import static play.test.Helpers.fakeApplication; | |
21 | +import static play.test.Helpers.running; | |
22 | + | |
23 | +/** | |
24 | + * User: doortts | |
25 | + * Date: 4/2/13 | |
26 | + * Time: 4:49 PM | |
27 | + */ | |
28 | +public class MailSendTest { | |
29 | + protected static FakeApplication app; | |
30 | + | |
31 | + @Before | |
32 | + public void startApp() { | |
33 | + app = Helpers.fakeApplication(Helpers.inMemoryDatabase()); | |
34 | + Helpers.start(app); | |
35 | + } | |
36 | + | |
37 | + @After | |
38 | + public void stopApp() { | |
39 | + Helpers.stop(app); | |
40 | + } | |
41 | + | |
42 | + public static final String DEFAULT_TEXT_MESSAGE = "Dear recipient,\n\n\n" + | |
43 | + "this is the mailtext.\n\nsincerely sender\n\n" + | |
44 | + "테스트 메일입니다. 답장을 보내지 마세요. :)"; | |
45 | + public static final String DEFAULT_HTML_MESSAGE = "<html><body style='background: red'><p>Hello</p></body></html>"; | |
46 | + public static final String SENDER_LOCALHOST = "hiveproject.mail@gmail.com"; | |
47 | + public static final String RECIPIENT_LOCALHOST = "nforge@nhn.com"; | |
48 | + public static final String SUBJECT = "메일 발송 테스트입니다 - the subject of the email"; | |
49 | + public static final String ATTACHEMENT_TXT_FILE_NAME = "the-text-file.txt"; | |
50 | + | |
51 | + | |
52 | + @Test | |
53 | + public void testSendSimpleMail() throws Exception { | |
54 | + //Given | |
55 | + SimpleEmail email = new SimpleEmail(); | |
56 | + email.setFrom(SENDER_LOCALHOST); | |
57 | + email.setSubject(SUBJECT); | |
58 | + email.addTo(RECIPIENT_LOCALHOST); | |
59 | + email.setMsg(DEFAULT_TEXT_MESSAGE); | |
60 | + email.setCharset("utf-8"); | |
61 | + | |
62 | + //When | |
63 | + Mailer.send(email); | |
64 | + | |
65 | + //Then | |
66 | + assertThat(Mailer.history().get(0)).isEqualTo(email); | |
67 | + } | |
68 | +} |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?