在日常开发中发送邮件是很正常的,现贴出发送邮件代码
1.实例类,只贴出所拥有的属性,持久化数据库的配置不再列出
public class Mail{
/**
* @description 属性说明
*/
private static final long serialVersionUID = 1797738641950018355L;
/**
* 主键
*/
private String mailId;
/**
* SMTP服务器
*/
private String smtpSer;
/**
* 邮件接收端口
*/
private int mailPort;
/**
* 验证用户名
*/
private String username;
/**
* 验证密码
*/
private String password;
/**
* 是否验证
*/
private int isVal;
/**
* 发件人信箱
*/
private String sendBox;
/**
* 默认主题
*/
private String deTheme;
/**
* 默认接收者
*/
private String deReceiver;
}
2.发送邮件
public boolean sendEmail(String[] to,String[] cc,Mail mail , String subject, String content,
String[] attachements) {
MimeMessage mimeMsg;// MIME邮件对象
MimeMultipart mp;// Multipart对象,邮件内容,标题,附件等内容均添加到其中之后再生成//MimeMessage对象
Session session;// 邮件会话对象
Properties props = new Properties();// 系统属性
try {
props.put("mail.smtp.host", mail.getSmtpSer());// 设置SMTP主机
props.put("mail.smtp.port", mail.getMailPort());
MyAuthenticator authenticator = null;
if (mail.getIsVal() == 1) {
props.put("mail.smtp.auth", "true");
authenticator = new MyAuthenticator(mail.getUsername(),mail.getPassword());
} else {
props.put("mail.smtp.auth", "false");
System.out.println("发送邮件出错,必须验证stmp!");
return false;
}
session = Session.getDefaultInstance(props, authenticator);// 获得邮件会话对象
mimeMsg = new MimeMessage(session);
mp = new MimeMultipart();
mimeMsg.setFrom(new InternetAddress(mail.getSendBox()));// 设置发信人
// 设置收信人
Address toaddress[] = new Address[to.length];
for(int i=0;i<to.length;i++){
toaddress[i]=InternetAddress.parse(to[i].toString())[0];
}
mimeMsg.setRecipients(Message.RecipientType.TO, toaddress);
// 设置抄送人
Address ccaddress[] = new Address[cc.length];
for(int i=0;i<cc.length;i++){
ccaddress[i]=InternetAddress.parse(cc[i].toString())[0];
}
mimeMsg.setRecipients(Message.RecipientType.CC, ccaddress);
// 设置邮件体格式
mimeMsg.setSubject(mail.getDeTheme(), "GB2312");
BodyPart bp = new MimeBodyPart();
bp.setContent(
"<meta http-equiv=Content-Type content=text/html; charset=GB2312>"
+ content, "text/html;charset=GB2312");
mp.addBodyPart(bp);
// 添加附件,超过10M不可以发送。
if (attachements != null && attachements.length > 0) {
for (int i = 0; i < attachements.length; i++) {
if((new File(attachements[i])).exists()){
FileInputStream fis = new FileInputStream(new File(attachements[i]));
if((double)fis.available()/1048576 <10.0){
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(attachements[i]);
DataHandler dh = new DataHandler(fileds);
mbp.setDisposition(Part.ATTACHMENT);
mbp.setFileName(fileds.getName());
mbp.setDataHandler(dh);
mp.addBodyPart(mbp);
}
}
}
}
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
// 正式发送邮件
Session mailSession = Session.getInstance(props, null);
Transport transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"),
mail.getUsername(), mail.getPassword());
transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
transport.close();
System.out.println("Mail Successfully Sended!");
return true;
} catch (Exception e) {
logger.debug("发送邮件出错,详见类:UniresponseApiImpl。错误信息为:"
+ e.getMessage());
return false;
}
}