使用Play Framework 2.0(Java版)进行开发时,我有一个问题,因为我想用html发送电子邮件, 例如
/app
--a.html
--controller
如何加载a.html作为邮件正文?
答案 0 :(得分:0)
看一下这个例子:
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.AddressException;
import javax.mail.internet.MimeBodyPart;
...
// set up smtp
Properties props = System.getProperties();
props.put("mail.transport.protocol", "smtp");
// if needed
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", SMTP_SSL_ENABLE);
props.put("mail.smtp.host", SMTP_SERVER);
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.user", SMTP_USER);
props.put("mail.password", SMTP_PASSWD);
Authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(SMTP_USER, SMTP_PASSWD);
}
};
// create message
Session session = Session.getDefaultInstance(props, authenticator);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(SMTP_SENDER));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
message.setSubject("emails title");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(yourTemplate.render().body(), "text/html; charset=iso-8859-1");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
message.saveChanges();
// send message
Transport.send(message);
您应该使用html文档替换播放视图模板正文部分:
messageBodyPart.setContent(yourTemplate.render().body(), "text/html; charset=iso-8859-1");
希望它有所帮助!
答案 1 :(得分:0)
/Controller/Mails.java method: sendmail()
public static void sendmail(){
send(body)
}
/Views/Mails/sendmail.html [template]