package com.zlf.javamail.mail;
im
port java.io.FileNotFoundException;
im
port java.io.FileOutputStream;
im
port java.io.IOException;
im
port java.io.OutputStream;
im
port java.io.UnsupportedEncodingException;
im
port java.util.Properties;
im
port javax.activation.DataHandler;
im
port javax.activation.DataSource;
im
port javax.activation.FileDataSource;
im
port javax.mail.MessagingException;
im
port javax.mail.Session;
im
port javax.mail.Transport;
im
port javax.mail.Message.RecipientType;
im
port javax.mail.internet.MimeBodyPart;
im
port javax.mail.internet.MimeMessage;
im
port javax.mail.internet.MimeMultipart;
im
port javax.mail.internet.MimeUtility;
im
port com.zlf.javamail.Util.MailCfg;
im
port com.zlf.javamail.Util.MailUtilBean;
public class Mails {
public MimeMessage setmail(){
Session session =Session.getInstance(new Properties());//创建环境
MimeMessage msg=new MimeMessage(session);//创建邮件
try {
msg.setSubject(MailUtilBean.getSubject());
msg.setFrom(MailCfg.getFromaddress());
msg.setRecipients(RecipientType.TO,MailCfg.getToaddress());
} catch (MessagingException e1) {
e1.printStackTrace();
}
if(MailCfg.getEnclurl().size()>0||MailUtilBean.getTextstyle().equals("")||MailUtilBean.getTextstyle()!=null){
MimeMultipart msgmultipart=new MimeMultipart("mixed");
MimeBodyPart co
ntent=new MimeBodyPart();
try {
msgmultipart.addBodyPart(content);
for(int i=0;i<MailCfg.getEnclurl().size();i++){
MimeBodyPart attch=new MimeBodyPart();
msgmultipart.addBodyPart(attch);
String urlstr=MailCfg.getEnclurl().get(i);
DataSource ds=new FileDataSource(urlstr);
DataHandler dh=new DataHandler(ds);
attch.setDataHandler(dh);
attch.setFileName(MimeUtility.encodeText(urlstr.substrin
g(urlstr.lastIndexOf("\")+1)));
}
} catch (MessagingException e1) {
System.out.println("附件加入异常");
e1.printStackTrace();
} catch (UnsupportedEncodingException e) {
System.out.println("附件名字转换异常");
e.printStackTrace();
}
try {
msg.setCo
ntent(msgmultipart);
} catch (MessagingException e) {
System.out.println("载入附件异常");
e.printStackTrace();
}
MimeMultipart bodymultipart=new MimeMultipart("related");
try {
content.setCo
ntent(bodymultipart);
} catch (MessagingException e) {
System.out.println("设置内容模式异常");
e.printStackTrace();
}
MimeBodyPart htmlpart=new MimeBodyPart();
try {
bodymultipart.addBodyPart(htmlpart);
} catch (MessagingException e) {
System.out.println("载入内容异常");
e.printStackTrace();
}
try {
htmlpart.setCo
ntent(MailUtilBean.getCo
ntext(), MailUtilBean.getTextstyle());
msg.saveChanges();
} catch (MessagingException e) {
System.out.println("保存邮件异常");
e.printStackTrace();
}
}else{
try {
msg.setText(MailUtilBean.getCo
ntext());
System.out.println("简单邮件");
} catch (MessagingException e) {
System.out.println("设置简单邮件内容异常");
e.printStackTrace();
}
}
OutputStream ops=null;
try {
ops=new FileOutputStream(MailUtilBean.getUrl());
} catch (FileNotFoundException e) {
System.out.println("打开文件流异常");
e.printStackTrace();
}
try {
msg.writeTo(ops);
ops.close();
} catch (IOException e) {
System.out.println("关闭文件流异常");
e.printStackTrace();
} catch (MessagingException e) {
System.out.println("邮件写入到磁盘异常");
e.printStackTrace();
}
return msg;
}
public void sendmail(MimeMessage message){
Properties props=new Properties();
props.setProperty("mail.stmp.auth","true");
props.setProperty("mail.transport.protocol","smtp");
Session session =Session.getInstance(props);
session.setDebug(true);
try {
Transport ts=session.getTransport();
ts.co
nnect(MailUtilBean.getHost(),MailUtilBean.getPort(),MailUtilBean.getUsername(), MailUtilBean.getPasswor
d());
ts.sendMessage(message,message.getAllRecipients());
ts.close();
} catch (MessagingException e) {
System.out.println("发送失败");
e.printStackTrace();
}
}