Java邮件开发(一):使用JMail发送一封简单邮件

JMail是进行邮件开发的组件。由于所需要的jar非官方开发,需要自行下载所需jar包,我使用的是javamail-1.4.7

不了解收发邮件过程的请先浏览我的另一片博客:电子邮件工作原理  

1.步骤:

1. 创建Properties对象,并为该对象设置相应内容

2. 创建Session对象

3. 创建Message对象,这个对象封装邮件的信息,如发件人、收件人、主题等等

4. 创建Transport对象,利用这个对象发送邮件

总的来说就两个步骤:构造邮件对象(第3步,只不过在构建邮件时需要其它对象),发送邮件(第4步)

2.发送一封简单的邮件

代码:

[java]  view plain  copy
  1. package com.zyh.demo;  
  2.   
  3. import java.util.Properties;  
  4.   
  5. import javax.mail.Address;  
  6. import javax.mail.Message;  
  7. import javax.mail.MessagingException;  
  8. import javax.mail.Session;  
  9. import javax.mail.Transport;  
  10. import javax.mail.internet.InternetAddress;  
  11. import javax.mail.internet.MimeMessage;  
  12.   
  13. public class Demo1 {  
  14.       
  15.     public static void main(String[] args) throws MessagingException {  
  16.         Properties props = new Properties();  
  17.         props.setProperty("mail.smtp.auth""true");//设置访问smtp服务器需要认证  
  18.         props.setProperty("mail.transport.protocol""smtp"); //设置访问服务器的协议  
  19.           
  20.         Session session = Session.getDefaultInstance(props);  
  21.         session.setDebug(true); //打开debug功能  
  22.           
  23.         Message msg = new MimeMessage(session);  
  24.         msg.setFrom(new InternetAddress("[email protected]")); //设置发件人,163邮箱要求发件人与登录用户必须一致(必填),其它邮箱不了解  
  25.         msg.setText("Hello world!"); //设置邮件内容  
  26.         msg.setSubject("test"); //设置邮件主题  
  27.           
  28.         Transport trans = session.getTransport();  
  29.         trans.connect("smtp.163.com"25"zyh5540""test"); //连接邮箱smtp服务器,25为默认端口  
  30.         trans.sendMessage(msg, new Address[]{new InternetAddress("[email protected]")}); //发送邮件  
  31.           
  32.         trans.close(); //关闭连接  
  33.     }  
  34.   
  35. }  

3.群发一封邮件

源代码:

[java]  view plain  copy
  1. package com.zyh.demo;  
  2.   
  3. import java.util.Properties;  
  4.   
  5. import javax.mail.Authenticator;  
  6. import javax.mail.Message;  
  7. import javax.mail.Message.RecipientType;  
  8. import javax.mail.MessagingException;  
  9. import javax.mail.PasswordAuthentication;  
  10. import javax.mail.Session;  
  11. import javax.mail.Transport;  
  12. import javax.mail.internet.InternetAddress;  
  13. import javax.mail.internet.MimeMessage;  
  14.   
  15. /** 
  16.  * 使用另外一种方式群发一封普通邮件 
  17.  * @author Administrator 
  18.  * 
  19.  */  
  20. public class Demo2 {  
  21.   
  22.     public static void main(String[] args) throws MessagingException {  
  23.         Properties props = new Properties();  
  24.         props.setProperty("mail.smtp.auth""true");  
  25.         props.setProperty("mail.transport.protocol""smtp");  
  26.         props.setProperty("mail.host""smtp.163.com");  
  27.           
  28.         Session session = Session.getInstance(props, new Authenticator() {  
  29.             protected PasswordAuthentication getPasswordAuthentication() {  
  30.                 return new PasswordAuthentication("zyh5540""test");  
  31.             }  
  32.         });  
  33.         session.setDebug(true);  
  34.           
  35.         Message msg = new MimeMessage(session);  
  36.         msg.setFrom(new InternetAddress("[email protected]"));  
  37.         msg.setSubject("中文主题测试1");  
  38.         //注意第二个参数要写成"text/html;charset=utf-8",表明这是一封html邮件  
  39.         msg.setContent("<span style='color:red;margin:0 auto'>中文测试群发</span>""text/html;charset=utf-8");  
  40.         msg.setRecipients(RecipientType.TO, InternetAddress.parse("[email protected],[email protected]"));  
  41.           
  42.         Transport.send(msg);  
  43.     }  
  44. }   

说明:

    有些参数设置是比较灵活的,比如邮箱主机地址smtp.163.com、用户名、密码既可以利用props对象进行设置,也可以在Transport对象与服务器连接时进行指定(就像Demo1)。

附:

1.JavaMail开发jar包下载地址:http://download.csdn.net/download/zyh5540/6900667

2.参考代码下载地址:http://download.csdn.net/download/zyh5540/6907731

猜你喜欢

转载自blog.csdn.net/angel_g/article/details/76685656