【JavaMail开发总结】配置文件形式--发送邮件程序
2015-08-09 17:10
204 查看
在上一篇中简单的实现了一个发送邮件功能的程序,今天用配置文件的方式来实现,大致思路一致,示例代码如下:
配置文件如下(一般放在src目录下):
"mail.smtp.host=smtp.qq.com"指的是用qq.com的smtp服务器,除此之外还有163.com等等
"mail.smtp.port=25"指的是SMTP服务器端口是25
package com.javamail.test.demo; import java.io.IOException; import java.io.InputStream; import java.util.Date; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class EmailUtil { /** * 发送邮件 * @param to * 收信人 * @param subject * 邮件主题 * @param content * 邮件内容 * @throws IOException * @throws AddressException * @throws MessagingException */ public static void send_email(String to, String subject, String content) throws IOException, AddressException, MessagingException { Properties properties = new Properties(); InputStream resourceAsStream = null; try { // 获取配置文件 resourceAsStream = EmailUtil.class.getClassLoader().getResourceAsStream("email.properties"); properties.load(resourceAsStream); } finally { if (resourceAsStream != null) { resourceAsStream.close(); } } // System.err.println("properties:"+properties); properties.put("mail.smtp.host", properties.get("mail.smtp.host")); properties.put("mail.smtp.port", properties.get("mail.smtp.port")); properties.put("mail.smtp.auth", "true"); Authenticator authenticator = new Email_Authenticator(properties.get( "username").toString(), properties.get("password").toString()); javax.mail.Session sendMailSession = javax.mail.Session .getInstance(properties, authenticator); MimeMessage mailMessage = new MimeMessage(sendMailSession); //设置发信人 mailMessage.setFrom(new InternetAddress(properties.get("username") .toString(),"***")); // 设置收信人,Message.RecipientType.TO 收信人,Message.RecipientType.CC抄送人 mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress( to,properties.get("username").toString())); //主题 mailMessage.setSubject(subject, "UTF-8"); //设置邮件发送日期 mailMessage.setSentDate(new Date()); // MiniMultipart类是一个容器类 Multipart mainPart = new MimeMultipart(); // 创建一个邮件体对象 BodyPart html = new MimeBodyPart(); html.setContent(content.trim(), "text/html; charset=utf-8"); mainPart.addBodyPart(html); mailMessage.setContent(mainPart); Transport.send(mailMessage); } } //通过构造函数传入身份验证信息 class Email_Authenticator extends Authenticator { String userName = null; String password = null; public Email_Authenticator() { } public Email_Authenticator(String username, String password) { this.userName = username; this.password = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } }
配置文件如下(一般放在src目录下):
#email Configuration #need account and password #author cash #host mail.smtp.host=smtp.qq.com #port mail.smtp.port=25 #your email account username=***@qq.com #your email password password=***
"mail.smtp.host=smtp.qq.com"指的是用qq.com的smtp服务器,除此之外还有163.com等等
"mail.smtp.port=25"指的是SMTP服务器端口是25
相关文章推荐
- 智渔课堂官方免费教程二(拓展):关于解决新建文件没有扩展名的问题
- Java:JSON和Map相互转换
- java中static作用
- 华为上机 亮灯的个数 java
- Java反射机制之获取方法信息
- Java中extends 与 implement 区别(转)
- java使用阻塞队列实现生产者消费者模式
- HDU 5237 Base64 (Java大法好)
- 智渔课堂官方免费教程二(拓展):Java软件开发环境的安装与配置
- I学霸官方免费教程二(拓展):Java软件开发环境的安装与配置
- Java的date和以datetime为字段的数据库的处理办法
- Java查看List扩容后的容量
- 廖雪峰javascrip学习笔记<高级>
- [笔记][Java7并发编程实战手册]2.5使用Lock实现同步一
- Java构造方法
- corejava知识---【一】
- 属性名前两个字母要么都是大写的要么都是小写的(当然数字与_、$算小写)
- java synchronized静态同步方法与非静态同步方法,同步语句块
- Java回顾之I/O
- Java io文件操作(按字符、字节、行、随机读取,追加,文件操作)