邮件的发送
2015-11-14 21:53
253 查看
邮件发送
1、配置文件设置
2、发送邮件核心代码
3、工具类
4、导包
1、配置文件设置
2、发送邮件核心代码
/* * 发邮件 * 准备配置文件! */ // 获取配置文件内容 Properties props = new Properties(); props.load(this.getClass().getClassLoader() .getResourceAsStream("email_template.properties")); String host = props.getProperty("host");//获取服务器主机 String uname = props.getProperty("uname");//获取用户名 String pwd = props.getProperty("pwd");//获取密码 String from = props.getProperty("from");//获取发件人 String to = form.getEmail();//获取收件人 String subject = props.getProperty("subject");//获取主题 String content = props.getProperty("content");//获取邮件内容 content = MessageFormat.format(content, form.getCode());//替换{0} Session session = MailUtils.createSession(host, uname, pwd);//得到session Mail mail = new Mail(from, to, subject, content);//创建邮件对象 try { MailUtils.send(session, mail);//发邮件! } catch (MessagingException e) { }
3、工具类
package cn.itcast.mail; import java.io.IOException; import java.util.List; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message.RecipientType; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; 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.MimeMultipart; import javax.mail.internet.MimeUtility; /** * * @author itcast 本类只有这么一个方法,用来发邮件! */ public class MailUtils { public static Session createSession(String host, final String username, final String password) { Properties prop = new Properties(); prop.setProperty("mail.host", host);// 指定主机 prop.setProperty("mail.smtp.auth", "true");// 指定验证为true // 创建验证器 Authenticator auth = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }; // 获取session对象 return Session.getInstance(prop, auth); } /** * 发送指定的邮件 * * @param mail */ public static void send(Session session, final Mail mail) throws MessagingException, IOException { MimeMessage msg = new MimeMessage(session);// 创建邮件对象 msg.setFrom(new InternetAddress(mail.getFrom()));// 设置发件人 msg.addRecipients(RecipientType.TO, mail.getToAddress());// 设置收件人 // 设置抄送 String cc = mail.getCcAddress(); if (!cc.isEmpty()) { msg.addRecipients(RecipientType.CC, cc); } // 设置暗送 String bcc = mail.getBccAddress(); if (!bcc.isEmpty()) { msg.addRecipients(RecipientType.BCC, bcc); } msg.setSubject(mail.getSubject());// 设置主题 MimeMultipart parts = new MimeMultipart();// 创建部件集对象 MimeBodyPart part = new MimeBodyPart();// 创建一个部件 part.setContent(mail.getContent(), "text/html;charset=utf-8");// 设置邮件文本内容 parts.addBodyPart(part);// 把部件添加到部件集中 /////////////////////////////////////////// // 添加附件 List<AttachBean> attachBeanList = mail.getAttachs();// 获取所有附件 if (attachBeanList != null) { for (AttachBean attach : attachBeanList) { MimeBodyPart attachPart = new MimeBodyPart();// 创建一个部件 attachPart.attachFile(attach.getFile());// 设置附件文件 attachPart.setFileName(MimeUtility.encodeText(attach .getFileName()));// 设置附件文件名 String cid = attach.getCid(); if(cid != null) { attachPart.setContentID(cid); } parts.addBodyPart(attachPart); } } msg.setContent(parts);// 给邮件设置内容 Transport.send(msg);// 发邮件 } }
4、导包
相关文章推荐
- Java json && 框架中的 泛型 + 反射
- CKFinder(Java版)以时间作上传文件的保存路径
- 汉诺塔递归实现
- 前端进阶试题
- 读《不要等到毕业以后》有感②
- 九度oj-1037-Powerful Calculator
- 代理模式-Proxy
- 安装opencv3.0.0与配置Visual studio 2008
- Java Socket实现多客户端通信(通过DOS命令窗口模拟)
- PHP类相关函数
- 补码加减法器
- fpm打包
- hdoj 2151 Worm 【dp】
- 插入排序Java代码实现
- EditText无法快速响应点击事件
- 装饰者模式-Decorator
- 确认字符ACK
- 软件架构师的12项修炼
- 【linux下配置安装】Utorrent安装使用
- ZOJ 3794 Greedy Driver