Java Mail发送邮件_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java Mail发送邮件

Java Mail发送邮件

 2013/7/18 12:14:08  qincidong  程序员俱乐部  我要评论(0)
  • 摘要:1.邮件验证importjavax.mail.Authenticator;importjavax.mail.PasswordAuthentication;/***@authorEX-QINCIDONG001*验证*/publicclassMailAuthorizationextendsAuthenticator{privateStringusername;privateStringpassword;publicMailAuthorization(Stringusername
  • 标签:Java 邮件 发送邮件

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("qincidong@163.com");

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

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

mi.setSender("qincidong@163.com");

mi.setRecvers(new String[]{"ex-qincidong001@pingan.com.cn","qincidong@qq.com"});

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();

}

}

?

}

发表评论
用户名: 匿名