热门推荐
javamail群发邮件源码
2024-11-19 03:08
package com.zlf.javamail.mail;

javamail群发邮件源码

import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.Message.RecipientType; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; import com.zlf.javamail.Util.MailCfg; import com.zlf.javamail.Util.MailUtilBean; public class Mails { public MimeMessage setmail(){ Session session =Session.getInstance(new Properties());//创建环境 MimeMessage msg=new MimeMessage(session);//创建邮件 try { msg.setSubject(MailUtilBean.getSubject()); msg.setFrom(MailCfg.getFromaddress()); msg.setRecipients(RecipientType.TO,MailCfg.getToaddress()); } catch (MessagingException e1) { e1.printStackTrace(); } if(MailCfg.getEnclurl().size()>0||MailUtilBean.getTextstyle().equals("")||MailUtilBean.getTextstyle()!=null){ MimeMultipart msgmultipart=new MimeMultipart("mixed"); MimeBodyPart content=new MimeBodyPart(); try { msgmultipart.addBodyPart(content); for(int i=0;i<MailCfg.getEnclurl().size();i++){ MimeBodyPart attch=new MimeBodyPart(); msgmultipart.addBodyPart(attch); String urlstr=MailCfg.getEnclurl().get(i); DataSource ds=new FileDataSource(urlstr); DataHandler dh=new DataHandler(ds); attch.setDataHandler(dh); attch.setFileName(MimeUtility.encodeText(urlstr.substring(urlstr.lastIndexOf("\")+1))); } } catch (MessagingException e1) { System.out.println("附件加入异常"); e1.printStackTrace(); } catch (UnsupportedEncodingException e) { System.out.println("附件名字转换异常"); e.printStackTrace(); } try { msg.setContent(msgmultipart); } catch (MessagingException e) { System.out.println("载入附件异常"); e.printStackTrace(); } MimeMultipart bodymultipart=new MimeMultipart("related"); try { content.setContent(bodymultipart); } catch (MessagingException e) { System.out.println("设置内容模式异常"); e.printStackTrace(); } MimeBodyPart htmlpart=new MimeBodyPart(); try { bodymultipart.addBodyPart(htmlpart); } catch (MessagingException e) { System.out.println("载入内容异常"); e.printStackTrace(); } try { htmlpart.setContent(MailUtilBean.getContext(), MailUtilBean.getTextstyle()); msg.saveChanges(); } catch (MessagingException e) { System.out.println("保存邮件异常"); e.printStackTrace(); } }else{ try { msg.setText(MailUtilBean.getContext()); System.out.println("简单邮件"); } catch (MessagingException e) { System.out.println("设置简单邮件内容异常"); e.printStackTrace(); } } OutputStream ops=null; try { ops=new FileOutputStream(MailUtilBean.getUrl()); } catch (FileNotFoundException e) { System.out.println("打开文件流异常"); e.printStackTrace(); } try { msg.writeTo(ops); ops.close(); } catch (IOException e) { System.out.println("关闭文件流异常"); e.printStackTrace(); } catch (MessagingException e) { System.out.println("邮件写入到磁盘异常"); e.printStackTrace(); } return msg; } public void sendmail(MimeMessage message){ Properties props=new Properties(); props.setProperty("mail.stmp.auth","true"); props.setProperty("mail.transport.protocol","smtp"); Session session =Session.getInstance(props); session.setDebug(true); try { Transport ts=session.getTransport(); ts.connect(MailUtilBean.getHost(),MailUtilBean.getPort(),MailUtilBean.getUsername(), MailUtilBean.getPassword()); ts.sendMessage(message,message.getAllRecipients()); ts.close(); } catch (MessagingException e) { System.out.println("发送失败"); e.printStackTrace(); } }
    以上就是本篇文章【javamail群发邮件源码】的全部内容了,欢迎阅览 ! 文章地址:http://www78564.xrbh.cn/quote/24116.html 
     动态      相关文章      文章      同类文章      热门文章      栏目首页      网站地图      返回首页 迅博思语移动站 http://www78564.xrbh.cn/mobile/ , 查看更多