由于没有使用它的发送邮件模块,所以要自己实现。但却可以使用它的一些配置设定。
1.配置general.properties
[email protected]
mail.smtp.relay.host=smtp.163.com
[email protected]
mail.smtp.auth.password=发送邮箱的密码
mail.smtp.port=25
mail.smtp.socketFactory.port=25
2.假如javamail包
到
http://www.oracle.com/technetwork/java/javamail/index-138643.html下载javamail的包,并假如工程framework\base\lib,并引入到工程。
3.发送代码。
准备信息:
/**
* 通过type和id的组合,找到相应的学员和教练,然后构造密码并发送email,同时修改这个人相应的密码
*
* @param type
* @param id
*/
public static boolean SendPasswordInEmail(String email, String password) {
Map<String, String> emailContext = new HashMap<String, String>();
emailContext.put("Subject", UtilProperties.getMessage(resourceError,"modifyPwdEmailSubject", locale));
emailContext.put("ToEmail", email);
emailContext.put("cc", "");
emailContext.put("bcc", "");
emailContext.put("Text", UtilProperties.getMessage(resourceError,"modifyPwdEmailText", locale));
emailContext
.put("HtmlText",
" "+UtilProperties.getMessage(resourceError,"modifyPwdEmailContent1", locale)+
" : <span style='font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#666666; background-color:#EEEEEE;'>"+password+"</span>");
return SendEmailUtil.sendEmail(emailContext);
}
发送信息的类:
package org.surpass.tsp.utils;
import java.util.Map;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.ofbiz.base.util.UtilProperties;
public class SendEmailUtil {
private static boolean isNotBlank(String str) {
if (str == null)
return false;
if (str.trim().length() == 0)
return false;
return true;
}
public static boolean sendEmail(Map<String, String> emailContext) {
String defaultFromEmailAddress = UtilProperties.getPropertyValue( "general.properties", "defaultFromEmailAddress");
String host = UtilProperties.getPropertyValue("general.properties", "mail.smtp.relay.host");
String user = UtilProperties.getPropertyValue("general.properties", "mail.smtp.auth.user");
String password = UtilProperties.getPropertyValue("general.properties", "mail.smtp.auth.password");
Properties props = new Properties();
props.setProperty("mail.debug", "false");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.host", host);
props.setProperty("mail.transport.protocol", "smtp");
Session session = Session.getInstance(props);
javax.mail.Message msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(defaultFromEmailAddress));
if (isNotBlank(emailContext.get("Subject"))) {
msg.setSubject(emailContext.get("Subject"));
}
if (isNotBlank(emailContext.get("HtmlText")) && isNotBlank(emailContext.get("ToEmail"))) {
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
// 设定邮件内容的类型为 text/plain 或 text/html
mbp.setContent(emailContext.get("HtmlText"), "text/html;charset=UTF-8");
mp.addBodyPart(mbp);
msg.setContent(mp);
Transport transport = session.getTransport();
transport.connect(user, password);
//transport.sendMessage(msg, msg.getAllRecipients());
transport.sendMessage(msg, new Address[] { new InternetAddress( emailContext.get("ToEmail")) });
transport.close();
} else if (isNotBlank(emailContext.get("Text")) && isNotBlank(emailContext.get("ToEmail"))) {
msg.setText(emailContext.get("Text"));
Transport transport = session.getTransport();
transport.connect(user, password);
transport.sendMessage(msg, new Address[] { new InternetAddress( emailContext.get("ToEmail")) });
transport.close();
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}