import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;/**
* This is a utility class that allows the system to send email notifications
*
*/
public class MailUtil {private MimeMessage mimeMsg; //MIME邮件对象
//private Session session; //邮件会话对象
private Properties props; //系统属性
private boolean needAuth = true; //smtp是否需要认证
private String username = "my"; //smtp认证用户名和密码
private String password = "passwd";
private Multipart mp; //Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
private String mailBody = "";//邮件主体
private String title = "";//邮件标题
private String FileAffix = "";//附件
private String smtp = "smtp.163.com";
private String sendto = "";
private String sendfrom = ""; //~ Static fields/initializers ============================================= private static Session session = null; static {
Properties props = new Properties();
props.put("mail.smtp.host", Constants.MAIL_SMTP_HOST);
props.put("mail.from", Constants.MAIL_SENDER_ADDRESS);
props.put("mail.transport.protocol", "smtp");
session = Session.getDefaultInstance(props, null); } //~ Methods ================================================================ public static boolean sendEmail(String message, String subject,
String recipient, String sender) {
if (session == null) {
return false;
}
boolean retVal = true;
try {
Multipart mp = new MimeMultipart();
BodyPart bp = new MimeBodyPart();
MimeMessage msg = new MimeMessage(session);
msg.setSentDate(new Date());//设置发送时间
bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+message,"text/html;charset=GB2312");
mp.addBodyPart(bp);
//msg.setContent(message, "text/html");
msg.setContent(mp);
Address recp = new InternetAddress(recipient);
if (sender != null) {
Address snd = new InternetAddress(sender);
msg.setFrom(snd);
}
msg.addRecipient(Message.RecipientType.TO, recp);
msg.setSubject(subject);
Transport.send(msg);
//////////////////////////////////////
//Session mailSession = Session.getInstance(props,null);
// Transport transport = mailSession.getTransport("smtp");
//transport.connect(smtp,username,password);
//transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO));
//transport.close(); } catch (MessagingException e) {
e.printStackTrace();
retVal = false;
} return retVal;
}
/**
* 设置邮件body
* @param mailBody String
* @return boolean
*/
public boolean setBody(String mailBody)
{
try
{
BodyPart bp = new MimeBodyPart();
bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+mailBody,"text/html;charset=GB2312");
mp.addBodyPart(bp);
return true;
}
catch(Exception e){
//logger.debug("设置邮件正文时发生错误!"+e);
return false;
}
}
/**
* 设置smtp身份认证
* @param need boolean
*/
private void setNeedAuth(boolean need){
//logger.debug("设置smtp身份认证:mail.smtp.auth = "+need);
if(props == null)props = System.getProperties();
if(need){
props.put("mail.smtp.auth","true");
}else{
props.put("mail.smtp.auth","false");
}
}
}
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;/**
* This is a utility class that allows the system to send email notifications
*
*/
public class MailUtil {private MimeMessage mimeMsg; //MIME邮件对象
//private Session session; //邮件会话对象
private Properties props; //系统属性
private boolean needAuth = true; //smtp是否需要认证
private String username = "my"; //smtp认证用户名和密码
private String password = "passwd";
private Multipart mp; //Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
private String mailBody = "";//邮件主体
private String title = "";//邮件标题
private String FileAffix = "";//附件
private String smtp = "smtp.163.com";
private String sendto = "";
private String sendfrom = ""; //~ Static fields/initializers ============================================= private static Session session = null; static {
Properties props = new Properties();
props.put("mail.smtp.host", Constants.MAIL_SMTP_HOST);
props.put("mail.from", Constants.MAIL_SENDER_ADDRESS);
props.put("mail.transport.protocol", "smtp");
session = Session.getDefaultInstance(props, null); } //~ Methods ================================================================ public static boolean sendEmail(String message, String subject,
String recipient, String sender) {
if (session == null) {
return false;
}
boolean retVal = true;
try {
Multipart mp = new MimeMultipart();
BodyPart bp = new MimeBodyPart();
MimeMessage msg = new MimeMessage(session);
msg.setSentDate(new Date());//设置发送时间
bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+message,"text/html;charset=GB2312");
mp.addBodyPart(bp);
//msg.setContent(message, "text/html");
msg.setContent(mp);
Address recp = new InternetAddress(recipient);
if (sender != null) {
Address snd = new InternetAddress(sender);
msg.setFrom(snd);
}
msg.addRecipient(Message.RecipientType.TO, recp);
msg.setSubject(subject);
Transport.send(msg);
//////////////////////////////////////
//Session mailSession = Session.getInstance(props,null);
// Transport transport = mailSession.getTransport("smtp");
//transport.connect(smtp,username,password);
//transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO));
//transport.close(); } catch (MessagingException e) {
e.printStackTrace();
retVal = false;
} return retVal;
}
/**
* 设置邮件body
* @param mailBody String
* @return boolean
*/
public boolean setBody(String mailBody)
{
try
{
BodyPart bp = new MimeBodyPart();
bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+mailBody,"text/html;charset=GB2312");
mp.addBodyPart(bp);
return true;
}
catch(Exception e){
//logger.debug("设置邮件正文时发生错误!"+e);
return false;
}
}
/**
* 设置smtp身份认证
* @param need boolean
*/
private void setNeedAuth(boolean need){
//logger.debug("设置smtp身份认证:mail.smtp.auth = "+need);
if(props == null)props = System.getProperties();
if(need){
props.put("mail.smtp.auth","true");
}else{
props.put("mail.smtp.auth","false");
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货