推广 热搜:   中国  公司  行业  设备  快速  企业  上海  未来  技术 

javamail群发邮件源码

   日期:2024-11-19     移动:http://www78564.xrbh.cn/mobile/quote/24116.html
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(); } }
本文地址:http://www78564.xrbh.cn/quote/24116.html    迅博思语 http://www78564.xrbh.cn/ , 查看更多

特别提示:本信息由相关用户自行提供,真实性未证实,仅供参考。请谨慎采用,风险自负。


相关最新动态
推荐最新动态
点击排行
网站首页  |  二维码  |  关于我们  |  联系方式  |  使用协议  |  版权隐私  |  网站地图  |  排名推广  |  广告服务  |  积分换礼  |  网站留言  |  RSS订阅  |  违规举报  |  粤ICP备2023022329号