- /*
- * 邮件发送类
- */
- public class MailSenderUtil{
- /**
- * 以HTML格式发送邮件,可带附件,本方法可作为对外接口提供。
- * @param mailInfo 待发送的邮件信息
- * @throws MessagingException
- * @throws AddressException
- * @throws UnsupportedEncodingException
- */
- public static void sendHtmlMailWithLocalAttach(MailSenderInfo mailInfo) throws AddressException, MessagingException, UnsupportedEncodingException {
- // 判断是否需要身份认证
- MyAuthenticator authenticator = null;
- // 如果需要身份认证,则创建一个密码验证器
- if (mailInfo.isValidate()) {
- authenticator = new MyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
- }
- // 根据邮件会话属性和密码验证器构造一个发送邮件的session
- Properties pro = mailInfo.getProperties();
- Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
- // 根据session创建一个邮件消息
- Message mailMessage = new MimeMessage(sendMailSession);
- // 设置发送人
- mailMessage.setFrom(new InternetAddress(mailInfo.getFromAddress()));
- // 设置收件人
- String[] toAddress = mailInfo.getToAddress();
- if(toAddress == null || toAddress.length < 1){
- throw new RuntimeException("收件人不得为空!");
- }
- for (int i = 0; i < toAddress.length; i++) {
- mailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress[i]));
- }
- //设置抄送人
- String[] ccAddress = mailInfo.getCcAddress();
- if(ccAddress != null && ccAddress.length > 0){
- for (int i = 0; i < ccAddress.length; i++) {
- mailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(ccAddress[i]));
- }
- }
- // 设置邮件消息的主题
- mailMessage.setSubject(mailInfo.getTitle());
- // 设置邮件消息发送的时间
- mailMessage.setSentDate(new Date());
- // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
- Multipart mainPart = new MimeMultipart();
- // 创建一个包含HTML内容的MimeBodyPart
- BodyPart html = new MimeBodyPart();
- // 设置HTML内容
- html.setContent(mailInfo.getContent(), "text/html; charset=GBK");
- mainPart.addBodyPart(html);
- // 添加附件
- addAttachment(mailInfo, mainPart);
- // 将MiniMultipart对象设置为邮件内容
- mailMessage.setContent(mainPart);
- // 发送邮件
- Transport.send(mailMessage);
- }
- /*
- * 添加邮件附件,用本地的文件作为附件
- */
- private static void addAttachment(MailSenderInfo mailInfo,Multipart mainPart) throws MessagingException, UnsupportedEncodingException{
- String[] attachFileNames = mailInfo.getAttachFileNames();
- if(attachFileNames != null && attachFileNames.length > 0){
- for (int i = 0; i < attachFileNames.length; i++) {
- MimeBodyPart mdp = new MimeBodyPart();
- // FileDataSource/DataHandler 会用到
- // activation-1.1.jar,jdk6中已经包含了该jar中内容,不再需要单独下载
- // activation-1.1.jar
- FileDataSource fds = new FileDataSource(attachFileNames[i]);
- DataHandler dh = new DataHandler(fds);
- mdp.setDataHandler(dh);
- // 保持附件名称与原文件名称一致的写法,MimeUtility.encodeText()可以解决中文附件乱码。
- mdp.setFileName(MimeUtility.encodeText(fds.getName()));
- mainPart.addBodyPart(mdp);
- }
- }
- }
- }
- /**
- * MyAuthenticator bean
- */
- class MyAuthenticator extends Authenticator{
- String userName=null;
- String password=null;
- public MyAuthenticator(){
- }
- public MyAuthenticator(String username, String password) {
- this.userName = username;
- this.password = password;
- }
- protected PasswordAuthentication getPasswordAuthentication(){
- return new PasswordAuthentication(userName, password);
- }
- }
- /**
- * MailSenderInfo bean
- */
- class MailSenderInfo {
- // 发送邮件的服务器的IP和端口
- private String mailServerHost;
- private String mailServerPort;;
- // 邮件发送者的地址
- private String fromAddress;
- //接收人地址
- private String[] toAddress;
- //抄送人地址
- private String[] ccAddress;
- // 登陆邮件发送服务器的用户名和密码
- private String userName;
- private String password;
- // 是否需要身份验证
- private boolean validate = false;
- // 邮件主题
- private String title;
- // 邮件的文本内容
- private String content;
- // 邮件附件的文件名,含磁盘物理路径,如D:\\Desktop\\index.html
- private String[] attachFileNames;
- /**
- * 获得邮件会话属性
- * @return Properties
- */
- public Properties getProperties() {
- Properties p = new Properties();
- p.put("mail.smtp.host", this.mailServerHost);
- p.put("mail.smtp.port", this.mailServerPort);
- p.put("mail.smtp.auth", validate ? "true" : "false");
- return p;
- }
- public String[] getCcAddress() {
- return ccAddress;
- }
- public void setCcAddress(String[] ccAddress) {
- this.ccAddress = ccAddress;
- }
- public String getMailServerHost() {
- return mailServerHost;
- }
- public void setMailServerHost(String mailServerHost) {
- this.mailServerHost = mailServerHost;
- }
- public String getMailServerPort() {
- return mailServerPort;
- }
- public void setMailServerPort(String mailServerPort) {
- this.mailServerPort = mailServerPort;
- }
- public boolean isValidate() {
- return validate;
- }
- public void setValidate(boolean validate) {
- this.validate = validate;
- }
- public String[] getAttachFileNames() {
- return attachFileNames;
- }
- public void setAttachFileNames(String[] fileNames) {
- this.attachFileNames = fileNames;
- }
- public String getFromAddress() {
- return fromAddress;
- }
- public void setFromAddress(String fromAddress) {
- this.fromAddress = fromAddress;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String[] getToAddress() {
- return toAddress;
- }
- public void setToAddress(String[] toAddress) {
- this.toAddress = toAddress;
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getContent() {
- return content;
- }
- public void setContent(String textContent) {
- this.content = textContent;
- }
- }
使用
- public static void main(String[] args) throws AddressException, UnsupportedEncodingException, MessagingException {
- MailSenderInfo mail = new MailSenderInfo();
- //设置附件
- mail.setAttachFileNames(new String[]{
- "D:\\Desktop\\首页.jpg",
- "D:\\Desktop\\测试账户.java"
- });
- //设置抄送人
- mail.setCcAddress(new String[]{"1@1.com","2@2.com"});
- //设置正文
- mail.setContent("<h3>http://www.baidu.com</h3>");
- //设置发件邮箱
- mail.setFromAddress("33@33.com");
- //设置发件邮箱密码
- mail.setPassword("123456");
- //设置邮件服务器
- mail.setMailServerHost("mail.XX.com");
- mail.setMailServerPort("25");
- //设置邮件主题
- mail.setTitle("我是主题");
- //设置收件人
- mail.setToAddress(new String[]{"44@44.com"});
- //设置用户名
- mail.setUserName("33");
- mail.setValidate(true);
- MailSenderUtil.sendHtmlMailWithLocalAttach(mail);
- }
有些邮件服务器中,username需要写全称(即包含@及之后的数据,如33@163.com),否则会报错:
javax.mail.AuthenticationFailedException: 535 5.7.0 Error: authentication failed: authentication failure
http://huangqiqing123.iteye.com/blog/1688119
时间: 2024-12-24 21:48:24