java email 需要的包
activation.jar
mail.jar
定时对指定时间生成的报表发送邮件通知。
class
---------------------------------------------
身份认证类
package com.yjm.Authenticator;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class EmailAuthenticator extends Authenticator{
String username;
String password;
public EmailAuthenticator(String username,String password){
this.username=username;
this.password=password;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username, password);
}
}
----------------------------------------------
发送邮件信息类
package com.yjm.emailinfo;
public class EmailInfo {
private String serverName;
private String username;
private String password;
private String port;
private String fromEmailAddress;
private String toEmailAddress;
private String subject;
private String content;
private String attachFileNames;
public String getServerName() {
return serverName;
}
public void setServerName(String serverName) {
this.serverName = serverName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getFromEmailAddress() {
return fromEmailAddress;
}
public void setFromEmailAddress(String fromEmailAddress) {
this.fromEmailAddress = fromEmailAddress;
}
public String getToEmailAddress() {
return toEmailAddress;
}
public void setToEmailAddress(String toEmailAddress) {
this.toEmailAddress = toEmailAddress;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAttachFileNames() {
return attachFileNames;
}
public void setAttachFileNames(String attachFileNames) {
this.attachFileNames = attachFileNames;
}
}
---------------------------------------------------
邮件发送控制类
package com.yjm.emailsent;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import com.yjm.Authenticator.EmailAuthenticator;
import com.yjm.emailinfo.EmailInfo;
public class EmailSent {
Properties pro;
public void sentEmail(EmailInfo mEmailInfo) {
EmailAuthenticator mEmailAuthenticator = new EmailAuthenticator(
mEmailInfo.getUsername(), mEmailInfo.getPassword());
pro = java.lang.System.getProperties();
pro.put("mail.smtp.host", mEmailInfo.getServerName());
pro.put("mail.smtp.port", mEmailInfo.getPort());
pro.put("mail.smtp.auth", "true");
Session mSession = Session.getDefaultInstance(pro, mEmailAuthenticator);
MimeMessage mMessage = new MimeMessage(mSession);
Address from;
Address to;
try {
from = new InternetAddress(mEmailInfo.getFromEmailAddress());
to = new InternetAddress(mEmailInfo.getToEmailAddress());
mMessage.setFrom(from);
mMessage.setRecipient(Message.RecipientType.TO, to);// to address[]
// mMessage.setRecipient(Message.RecipientType.CC, cc);//抄送地址数组
mMessage.setSubject(mEmailInfo.getSubject(), "gb2312");
mMessage.setSentDate(new Date());
// 不带附件使用 mMessage.setContent();String 类型
// 带附件 要在mMessage.setContent(); MimeMultipart类型 带附件
MimeMultipart mp = new MimeMultipart();
MimeBodyPart mb = new MimeBodyPart();
mb.setContent(mEmailInfo.getContent(), "text/plain;charset=GB2312"); // 设置编码
mb.setHeader("Content-Transfer-Encoding", "base64");
mp.addBodyPart(mb);
mMessage.setContent(mp);
// if (attachment != null) {
// BodyPart attachmentBodyPart = new MimeBodyPart();
// DataSource source = new FileDataSource(attachment);
// attachmentBodyPart.setDataHandler(new DataHandler(source));
// 网上流传的解决文件名乱码的方法,其实用MimeUtility.encodeWord就可以很方便的搞定
// 这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码
//sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
//messageBodyPart.setFileName("=?GBK?B?" + enc.encode(attachment.getName().getBytes()) + "?=");
//MimeUtility.encodeWord可以避免文件名乱码
// attachmentBodyPart.setFileName(MimeUtility.encodeWord(attachment.getName()));
// multipart.addBodyPart(attachmentBodyPart);
// }
// mMessage.setText(mEmailInfo.getContent());/正文
Transport.send(mMessage);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
--------------------------------------------------------------
1.MimeMessage 发送邮件实体类
2.MimeMultipart 内容载体类 包含 多个MimeBodyPart 内容 附件
3.MimeBodyPart 内容附件 存放在此对象里 此对象可多个保存在 MimeMultipart