Java Mail发送邮件

1. 邮件验证

import javax.mail.Authenticator;

import javax.mail.PasswordAuthentication;

 

/**

*@author EX-QINCIDONG001

*验证

*/

publicclass MailAuthorizationextends Authenticator {

private Stringusername;

private Stringpassword;

 

public MailAuthorization(String username,String password) {

this.username = username;

this.password = password;

}

 

@Override

protected PasswordAuthentication getPasswordAuthentication() {

//TODO Auto-generated method stub

returnnew PasswordAuthentication(username,password);

}

 

}

 

 

2. 邮件信息

mport java.io.File;

import java.util.List;

 

/**

*@author EX-QINCIDONG001

*邮件信息

*/

publicclass MailInfo {

// 服务器地址

private StringhostName;

// 端口

private StringhostPort;

// 用户名

private StringuserName;

private Stringpassword;

public String getUserName() {

returnthis.userName;

}

publicvoid setUserName(String userName) {

this.userName = userName;

}

public String getPassword() {

returnthis.password;

}

publicvoid setPassword(String password) {

this.password = password;

}

// 是否需要验证

privatebooleanvalidate =false;

// 发件人

private Stringsender;

// 收件人

private String[]recvers = {};

// 抄送给哪些人

private String[]ccs = {};

// 邮件主题

private Stringsubject;

// 邮件内容

private Stringcontent;

// 附件

private List<File>attaches;

public String getHostName() {

returnthis.hostName;

}

publicvoid setHostName(String hostName) {

this.hostName = hostName;

}

public String getHostPort() {

returnthis.hostPort;

}

publicvoid setHostPort(String hostPort) {

this.hostPort = hostPort;

}

publicboolean isValidate() {

returnthis.validate;

}

publicvoid setValidate(boolean validate) {

this.validate = validate;

}

public String getSender() {

returnthis.sender;

}

publicvoid setSender(String sender) {

this.sender = sender;

}

 

public String[] getRecvers() {

returnthis.recvers;

}

publicvoid setRecvers(String[] recvers) {

this.recvers = recvers;

}

 

public String[] getCcs() {

returnthis.ccs;

}

publicvoid setCcs(String[] ccs) {

this.ccs = ccs;

}

public String getSubject() {

returnthis.subject;

}

publicvoid setSubject(String subject) {

this.subject = subject;

}

public String getContent() {

returnthis.content;

}

publicvoid setContent(String content) {

this.content = content;

}

public List<File> getAttaches() {

returnthis.attaches;

}

publicvoid setAttaches(List<File> attaches) {

this.attaches = attaches;

}

 

 

}

 

3. 邮件发送

import java.util.Properties;

 

import javax.mail.Address;

import javax.mail.Authenticator;

import javax.mail.BodyPart;

import javax.mail.MessagingException;

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.MimeMessage.RecipientType;

import javax.mail.internet.MimeMultipart;

 

/**

*@author EX-QINCIDONG001

*邮件发送。

*/

publicclass MailSender {

private MailInfomailInfo;

 

public MailSender(MailInfo mailInfo) {

this.mailInfo = mailInfo;

}

 

//发送邮件方法

publicvoid sendMail()throws MessagingException {

Properties props = System.getProperties();

props.setProperty("mail.transport.protocol","smtp");// smtp协议

props.put("mail.smtp.host",mailInfo.getHostName());

props.put("mail.smtp.port",mailInfo.getHostPort());

props.put("mail.smtp.auth",mailInfo.isValidate()?"true":"false");

 

Authenticator authenticator =new MailAuthorization(mailInfo.getUserName(),mailInfo.getPassword());

Session session = Session.getDefaultInstance(props, authenticator);

 

MimeMessage msg =new MimeMessage(session);

msg.setSubject(mailInfo.getSubject(),"utf-8");

Address address =new InternetAddress(mailInfo.getSender());

msg.setFrom(address);

 

//收信人

Address[] recvs =new Address[mailInfo.getRecvers().length];

int index = 0;

 

if (mailInfo.getRecvers() !=null) {

for (String s :mailInfo.getRecvers()) {

Address addr =new InternetAddress(s);

recvs[index] = addr;

index++;

}

}

msg.setRecipients(RecipientType.TO,recvs);

index = 0;

 

//抄送

Address[] ccs =new Address[mailInfo.getCcs().length];

if (mailInfo.getCcs() !=null) {

for (String s:mailInfo.getCcs()) {

Address addr =new InternetAddress(s);

ccs[index] = addr;

index++;

}

}

 

msg.setRecipients(RecipientType.CC, ccs);

 

Multipart mp =new MimeMultipart();

BodyPart part =new MimeBodyPart();

part.setContent(mailInfo.getContent(),"text/html;charset=utf-8");

 

//增加附件

if (mailInfo.getAttaches() !=null &&mailInfo.getAttaches().size() > 0) {// 有附件

for (int i=0;i<mailInfo.getAttaches().size();i++) {

BodyPart bp = new MimeBodyPart();

FileDataSource fds = new FileDataSource((File)mailInfo.getAttaches().get(i));

bp.setDataHandler(new DataHandler(fds));

mp.addBodyPart(bp);

}

}

 

mp.addBodyPart(part);

msg.setContent(mp);

 

//Transport transport = session.getTransport();

Transport.send(msg);

}

}

 

 

4. 邮件发送测试

import javax.mail.MessagingException;

 

/**

*@author EX-QINCIDONG001

*

*/

publicclass TestMailSender {

 

/**

*@param args

*/

publicstaticvoid main(String[] args) {

MailInfo mi =new MailInfo();

mi.setValidate(true);

mi.setUserName("[email protected]");

mi.setPassword("*******");

mi.setSubject("测试邮件发送");

mi.setSender("[email protected]");

mi.setRecvers(new String[]{"[email protected]","[email protected]"});

mi.setHostPort("25");

mi.setHostName("smtp.163.com");

mi.setContent("<a href='www.baidu.com'>百度</a>百度知道");

// 添加附件

List<File> attaches = new ArrayList<File>();

attaches.add(new File("D:\\qincidong\\资料\\Ejb\\EJB3.0入门经典-weblogic.pdf"));

attaches.add(new File("D:\\qincidong\\资料\\Ejb\\编写第一个EJB应用程序.doc"));

mi.setAttaches(attaches);

 

 

MailSender sender =new MailSender(mi);

try {

sender.sendMail();

System.out.println("邮件发送成功。");

}catch (MessagingException e) {

System.err.println("邮件发送失败");

e.printStackTrace();

}

}

 

}

你可能感兴趣的:(java)