JMail是进行邮件开发的组件。由于所需要的jar非官方开发,需要自行下载所需jar包,我使用的是javamail-1.4.7。
不了解收发邮件过程的请先浏览我的另一片博客:电子邮件工作原理
1.步骤:
1. 创建Properties对象,并为该对象设置相应内容
2. 创建Session对象
3. 创建Message对象,这个对象封装邮件的信息,如发件人、收件人、主题等等
4. 创建Transport对象,利用这个对象发送邮件
总的来说就两个步骤:构造邮件对象(第3步,只不过在构建邮件时需要其它对象),发送邮件(第4步)
2.发送一封简单的邮件
代码:
- package com.zyh.demo;
- import java.util.Properties;
- import javax.mail.Address;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- public class Demo1 {
- public static void main(String[] args) throws MessagingException {
- Properties props = new Properties();
- props.setProperty("mail.smtp.auth", "true");//设置访问smtp服务器需要认证
- props.setProperty("mail.transport.protocol", "smtp"); //设置访问服务器的协议
- Session session = Session.getDefaultInstance(props);
- session.setDebug(true); //打开debug功能
- Message msg = new MimeMessage(session);
- msg.setFrom(new InternetAddress("[email protected]")); //设置发件人,163邮箱要求发件人与登录用户必须一致(必填),其它邮箱不了解
- msg.setText("Hello world!"); //设置邮件内容
- msg.setSubject("test"); //设置邮件主题
- Transport trans = session.getTransport();
- trans.connect("smtp.163.com", 25, "zyh5540", "test"); //连接邮箱smtp服务器,25为默认端口
- trans.sendMessage(msg, new Address[]{new InternetAddress("[email protected]")}); //发送邮件
- trans.close(); //关闭连接
- }
- }
3.群发一封邮件
源代码:
- package com.zyh.demo;
- import java.util.Properties;
- import javax.mail.Authenticator;
- import javax.mail.Message;
- 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.MimeMessage;
- /**
- * 使用另外一种方式群发一封普通邮件
- * @author Administrator
- *
- */
- public class Demo2 {
- public static void main(String[] args) throws MessagingException {
- Properties props = new Properties();
- props.setProperty("mail.smtp.auth", "true");
- props.setProperty("mail.transport.protocol", "smtp");
- props.setProperty("mail.host", "smtp.163.com");
- Session session = Session.getInstance(props, new Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication("zyh5540", "test");
- }
- });
- session.setDebug(true);
- Message msg = new MimeMessage(session);
- msg.setFrom(new InternetAddress("[email protected]"));
- msg.setSubject("中文主题测试1");
- //注意第二个参数要写成"text/html;charset=utf-8",表明这是一封html邮件
- msg.setContent("<span style='color:red;margin:0 auto'>中文测试群发</span>", "text/html;charset=utf-8");
- msg.setRecipients(RecipientType.TO, InternetAddress.parse("[email protected],[email protected]"));
- Transport.send(msg);
- }
- }
说明:
有些参数设置是比较灵活的,比如邮箱主机地址smtp.163.com、用户名、密码既可以利用props对象进行设置,也可以在Transport对象与服务器连接时进行指定(就像Demo1)。
附:
1.JavaMail开发jar包下载地址:http://download.csdn.net/download/zyh5540/6900667
2.参考代码下载地址:http://download.csdn.net/download/zyh5540/6907731