Last active
December 30, 2015 08:29
-
-
Save royguo/7803161 to your computer and use it in GitHub Desktop.
Java Mail using Playframework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers.oa; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import models.oa.Leave; | |
import models.oa.Vacation; | |
import models.system.User; | |
import play.Logger; | |
import play.data.validation.Valid; | |
import utils.DateUtils; | |
import utils.JSONUtils; | |
import utils.MailUtils; | |
import utils.Message; | |
import controllers.Application; | |
/** | |
* TODO 需要统一认证 | |
*/ | |
public class Leaves extends Application { | |
public static void testEmail() { | |
List<String> tos = new ArrayList<String>(); | |
tos.add("aaaaaa@google.com"); | |
tos.add("bbbbbb@google.com"); | |
tos.add("cccccc@google.com"); | |
MailUtils.send("cccccc@google.com", "测试邮件标题", tos, "Hello, This is a testing email!"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.mail.internet.AddressException; | |
import javax.mail.internet.InternetAddress; | |
import org.apache.commons.mail.Email; | |
import org.apache.commons.mail.EmailException; | |
import org.apache.commons.mail.SimpleEmail; | |
import play.libs.Mail; | |
public class MailUtils { | |
/** | |
* @param from | |
* @param tos | |
* @param content | |
*/ | |
public static void send(String from, String subject, List<String> tos, String content) { | |
Email email = new SimpleEmail(); | |
List<InternetAddress> toAddress = new ArrayList<InternetAddress>(); | |
for (String to : tos) { | |
try { | |
InternetAddress address = new InternetAddress(to); | |
toAddress.add(address); | |
} catch (AddressException e) { | |
e.printStackTrace(); | |
} | |
} | |
try { | |
email.setTo(toAddress); | |
email.setFrom(from); | |
email.setSubject(subject); | |
} catch (EmailException e) { | |
e.printStackTrace(); | |
} | |
// Async send | |
Mail.send(email); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment