java 发送邮件 配置_Java-发送邮件详细配置

packagecom.ata.task;/*** 邮件发�?�工具类

*

*@authorxxh

*@link*

*@version$Revision: 1.00 $ $Date: 2009-12-18*/

importjava.io.IOException;importjava.util.Date;importjava.util.Properties;importjavax.mail.Authenticator;importjavax.mail.Message;importjavax.mail.MessagingException;importjavax.mail.Multipart;importjavax.mail.PasswordAuthentication;importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.AddressException;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeBodyPart;importjavax.mail.internet.MimeMessage;importjavax.mail.internet.MimeMultipart;public classMailUtil {/*******

* 邮件发�?�,带用户名和密码验证,测试通过

*

*@paramto

*@paramfrom

*@paramtitle

*@paramcontent

*@paramsmtpServer

*@paramuser

*@parampassword

*@throwsAddressException

*@throwsMessagingException*/@SuppressWarnings({"unchecked"})public static voidsend(String from, String to,String copyTo, String title,

String content, String smtpServer, String user, String password,booleanisHTML)throwsAddressException, MessagingException {

Properties props= newProperties();

Authenticator auth= newMailAuthenticator(user, password);

Session sendMailSession;

Transport transport;

props.put("mail.smtp.host", smtpServer);

props.put("mail.smtp.auth", "true");

sendMailSession=Session.getInstance(props, auth);

Message newMessage= newMimeMessage(sendMailSession);

newMessage.setFrom(newInternetAddress(from));

newMessage.setRecipient(Message.RecipientType.TO,newInternetAddress(

to));

newMessage.addRecipient(Message.RecipientType.CC,newInternetAddress(

copyTo));

newMessage.setSubject(title);

newMessage.setSentDate(newDate());if(isHTML) {

newMessage.setContent(content,"text/html;charset=UTF-8");

}else{

newMessage.setText(content);

}

transport= sendMailSession.getTransport("smtp");

Transport.send(newMessage);

transport.close();//System.out.println("---------- Mail Send Success ----------");

}/*******************

* 邮件发�?�,不带用户名和密码验证

*@paramto

*@paramfrom

*@paramtitle

*@paramcontent

*@paramsmtpServer

*@throwsAddressException

*@throwsMessagingException*/@SuppressWarnings("static-access")public static voidsend(String to, String from, String title,

String content, String smtpServer)throwsAddressException,

MessagingException {

Properties props= newProperties();

Authenticator auth= newMailAuthenticator();

Session sendMailSession;

Transport transport;

sendMailSession=Session.getInstance(props, auth);

props.put("mail.smtp.host", smtpServer);

props.put("mail.smtp.auth", "true");

Message newMessage= newMimeMessage(sendMailSession);

newMessage.setFrom(newInternetAddress(from));

newMessage.setRecipient(Message.RecipientType.TO,newInternetAddress(

to));

newMessage.setSubject(title);

newMessage.setSentDate(newDate());

newMessage.setText(content);

transport= sendMailSession.getTransport("smtp");

transport.send(newMessage);

}/*********************

* 带附件邮件发送,带用户名和密码验证,测试通过

*@paramto

*@paramfrom

*@paramtitle

*@paramcontent

*@paramsmtpServer

*@paramuser

*@parampassword

*@paramfileNames

*@parampaths

*@throwsAddressException

*@throwsMessagingException*/@SuppressWarnings("static-access")public static voidsend(String to, String from, String title,

String content, String smtpServer, String user, String password,

String fileNames[], String[] paths)throwsAddressException,

MessagingException {try{

Properties props= newProperties();

Authenticator auth= newMailAuthenticator(user, password);

Session sendMailSession;

Transport transport;

props.put("mail.smtp.host", smtpServer);

props.put("mail.smtp.auth", "true");

sendMailSession=Session.getInstance(props, auth);

MimeMessage newMessage= newMimeMessage(sendMailSession);

newMessage.setFrom(newInternetAddress(from));

newMessage.setRecipient(Message.RecipientType.TO,newInternetAddress(to));//创建 Multipart 并放入每�? MimeBodyPart

Multipart mp = newMimeMultipart();if (title != null && !title.equals("")) {//设置邮件主题

newMessage.setSubject(title);

}else{

newMessage.setSubject("无主�?");

}//第一部分信息

MimeBodyPart mbp1 = newMimeBodyPart();if (content != null && !content.equals("")) {

mbp1.setText(content,"GBK");

}else{

mbp1.setText(new String(), "GBK");

}

mp.addBodyPart(mbp1);// //在第二部分信息中附加文件//if (paths != null) {//for (int i = 0; i < paths.length; i++) {//MimeBodyPart mbp2 = new MimeBodyPart();//FileDataSource fds = new FileDataSource(CommonConstants.ROOTPATH//+ paths[i]);//mbp2.setDataHandler(new DataHandler(fds));//mbp2.setFileName(fileNames[i]);//mp.addBodyPart(mbp2);//}//}//增加 Multipart 到信息体

newMessage.setContent(mp);

newMessage.setSentDate(newDate());

transport= sendMailSession.getTransport("smtp");

transport.send(newMessage);

}catch(Exception e) {

e.printStackTrace();

}

}

}class MailAuthenticator extendsAuthenticator {privateString user;privateString password;publicMailAuthenticator() {

}publicMailAuthenticator(String user, String password) {this.user =user;this.password =password;

}protectedPasswordAuthentication getPasswordAuthentication() {return newPasswordAuthentication(user, password);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值