java简单的邮件发送

最近因项目的需要,做了注册邮件验证的功能,所以整理了一下java简单的邮件发送,需要用的mail.jar 请自己在网上下载


一。定义一个封装邮件内容的类,代码如下

Java代码
  1. package net.bolue.mail;  
  2.   
  3. /** 
  4.  * 简单邮件 
  5.  * @author bruisefree 
  6.  * 
  7.  */  
  8. public class SimpleMail {  
  9.     protected String subject;//邮件主题  
  10.     protected String content;//邮件内容  
  11.       
  12.     public String getSubject() {  
  13.         return subject;  
  14.     }  
  15.     public void setSubject(String subject) {  
  16.         this.subject = subject;  
  17.     }  
  18.     public String getContent() {  
  19.         return content;  
  20.     }  
  21.     public void setContent(String content) {  
  22.         this.content = content;  
  23.     }  
  24. }  


二,定义验证邮件的实体类,即用来验证邮件地址和密码是否可用

Java代码
  1. package net.bolue.mail;  
  2.   
  3. import javax.mail.Authenticator;  
  4. import javax.mail.PasswordAuthentication;  
  5.   
  6. /** 
  7.  * 邮箱验证实体 
  8.  * @author bruisefree 
  9.  * 
  10.  */  
  11. public class MailAuthenticator extends Authenticator{  
  12.     /** 
  13.      * 用户名(登录邮箱) 
  14.      */  
  15.     private String username;  
  16.     /** 
  17.      * 密码 
  18.      */  
  19.     private String password;  
  20.   
  21.     /** 
  22.      * 初始化邮箱和密码 
  23.      *  
  24.      * @param username 
  25.      *            邮箱 
  26.      * @param password 
  27.      *            密码 
  28.      */  
  29.     public MailAuthenticator(String username, String password) {  
  30.         this.username = username;  
  31.         this.password = password;  
  32.     }  
  33.   
  34.     String getPassword() {  
  35.         return password;  
  36.     }  
  37.   
  38.     protected PasswordAuthentication getPasswordAuthentication() {  
  39.         return new PasswordAuthentication(username, password);  
  40.     }  
  41.   
  42.     String getUsername() {  
  43.         return username;  
  44.     }  
  45.   
  46.     public void setPassword(String password) {  
  47.         this.password = password;  
  48.     }  
  49.   
  50.     public void setUsername(String username) {  
  51.         this.username = username;  
  52.     }  
  53.   
  54. }  



三。就是编写我们真正用来发送邮件的类
Java代码
  1. package net.bolue.mail;  
  2.   
  3. import java.util.List;  
  4. import java.util.Properties;  
  5.   
  6. import javax.mail.MessagingException;   
  7. import javax.mail.Session;   
  8. import javax.mail.Transport;   
  9. import javax.mail.internet.AddressException;   
  10. import javax.mail.internet.InternetAddress;   
  11. import javax.mail.internet.MimeMessage;   
  12. import javax.mail.internet.MimeMessage.RecipientType;   
  13. /** 
  14.  * 发送邮件的类 
  15.  * @author Administrator 
  16.  * 
  17.  */  
  18. public class SimpleMailSender {  
  19.     /** 
  20.      * 发送邮件的props文件 
  21.      */  
  22.     private final transient Properties props = System.getProperties();  
  23.     /** 
  24.      * 邮件服务器登录验证 
  25.      */  
  26.     private transient MailAuthenticator authenticator;  
  27.     
  28.     /** 
  29.      * 邮箱session 
  30.      */  
  31.     private transient Session session;  
  32.     
  33.     /** 
  34.      * 初始化邮件发送器 
  35.      *  
  36.      * @param smtpHostName 
  37.      *                SMTP邮件服务器地址 
  38.      * @param username 
  39.      *                发送邮件的用户名(地址) 
  40.      * @param password 
  41.      *                发送邮件的密码 
  42.      */  
  43.     public SimpleMailSender(final String smtpHostName,final String username,  
  44.         final String password) {  
  45.         init(username, password, smtpHostName);  
  46.     }  
  47.     
  48.     /** 
  49.      * 初始化邮件发送器 
  50.      *  
  51.      * @param username 
  52.      *                发送邮件的用户名(地址),并以此解析SMTP服务器地址 
  53.      * @param password 
  54.      *                发送邮件的密码 
  55.      */  
  56.     public SimpleMailSender(final String username,final String password) {  
  57.         //通过邮箱地址解析出smtp服务器,对大多数邮箱都管用  
  58.         final String smtpHostName ="smtp."+ username.split("@")[1];  
  59.         init(username, password, smtpHostName);  
  60.     }  
  61.     
  62.     /** 
  63.      * 初始化 
  64.      *  
  65.      * @param username 
  66.      *                发送邮件的用户名(地址) 
  67.      * @param password 
  68.      *                密码 
  69.      * @param smtpHostName 
  70.      *                SMTP主机地址 
  71.      */  
  72.     private void init(String username, String password, String smtpHostName) {  
  73.         // 初始化props  
  74.         props.put("mail.smtp.auth","true");  
  75.         props.put("mail.smtp.host", smtpHostName);  
  76.         // 验证  
  77.         authenticator =new MailAuthenticator(username, password);  
  78.         // 创建session  
  79.         session = Session.getInstance(props, authenticator);  
  80.     }  
  81.     
  82.     /** 
  83.      * 发送邮件 
  84.      *  
  85.      * @param recipient 
  86.      *                收件人邮箱地址 
  87.      * @param subject 
  88.      *                邮件主题 
  89.      * @param content 
  90.      *                邮件内容 
  91.      * @throws AddressException 
  92.      * @throws MessagingException 
  93.      */  
  94.     public void send(String recipient, String subject, Object content)  throws AddressException, MessagingException {  
  95.         // 创建mime类型邮件  
  96.         final MimeMessage message =new MimeMessage(session);  
  97.         // 设置发信人  
  98.         message.setFrom(new InternetAddress(authenticator.getUsername()));  
  99.         // 设置收件人  
  100.         message.setRecipient(RecipientType.TO,new InternetAddress(recipient));  
  101.         // 设置主题  
  102.         message.setSubject(subject);  
  103.         // 设置邮件内容  
  104.         message.setContent(content.toString(),"text/html;charset=utf-8");  
  105.         // 发送  
  106.         Transport.send(message);  
  107.     }  
  108.     
  109.     /** 
  110.      * 群发邮件 
  111.      *  
  112.      * @param recipients 
  113.      *                收件人们 
  114.      * @param subject 
  115.      *                主题 
  116.      * @param content 
  117.      *                内容 
  118.      * @throws AddressException 
  119.      * @throws MessagingException 
  120.      */  
  121.     public void send(List<String> recipients, String subject, Object content)  
  122.         throws AddressException, MessagingException {  
  123.         // 创建mime类型邮件  
  124.         final MimeMessage message =new MimeMessage(session);  
  125.         // 设置发信人  
  126.         message.setFrom(new InternetAddress(authenticator.getUsername()));  
  127.         // 设置收件人们  
  128.         final int num = recipients.size();  
  129.         InternetAddress[] addresses =new InternetAddress[num];  
  130.         for(int i =0; i < num; i++) {  
  131.             addresses[i] =new InternetAddress(recipients.get(i));  
  132.         }  
  133.         message.setRecipients(RecipientType.TO, addresses);  
  134.         // 设置主题  
  135.         message.setSubject(subject);  
  136.         // 设置邮件内容  
  137.         message.setContent(content.toString(),"text/html;charset=utf-8");  
  138.         // 发送  
  139.         Transport.send(message);  
  140.     }  
  141.     
  142.     /** 
  143.      * 发送邮件 
  144.      *  
  145.      * @param recipient 
  146.      *                收件人邮箱地址 
  147.      * @param mail 
  148.      *                邮件对象 
  149.      * @throws AddressException 
  150.      * @throws MessagingException 
  151.      */  
  152.     public void send(String recipient, SimpleMail mail) throws AddressException, MessagingException {  
  153.         send(recipient, mail.getSubject(), mail.getContent());  
  154.     }  
  155.     
  156.     /** 
  157.      * 群发邮件 
  158.      *  
  159.      * @param recipients 
  160.      *                收件人们 
  161.      * @param mail 
  162.      *                邮件对象 
  163.      * @throws AddressException 
  164.      * @throws MessagingException 
  165.      */  
  166.     public void send(List<String> recipients, SimpleMail mail)  
  167.         throws AddressException, MessagingException {  
  168.         send(recipients, mail.getSubject(), mail.getContent());  
  169.     }  
  170.     
  171.   
  172. }  


好了,下面我们来写个测试类来发送邮件

Java代码
  1. package net.bolue.mail;  
  2.   
  3. import javax.mail.MessagingException;  
  4. import javax.mail.internet.AddressException;  
  5.   
  6. public class MailSendFactory {  
  7.     /**  
  8.      * 服务邮箱  
  9.      */  
  10.     private static SimpleMailSender serviceSms = null;   
  11.     
  12.     /**  
  13.      * 获取邮箱  
  14.      *   
  15.      * @param type 邮箱类型  
  16.      * @return 符合类型的邮箱  
  17.      */  
  18.     public static SimpleMailSender getSender() {  
  19.             if (serviceSms == null) {  
  20.             serviceSms = new SimpleMailSender("wlf_service@163.com",   "123456");   
  21.             }   
  22.             return serviceSms;   
  23.     }  
  24.       
  25.       
  26.     public static void main(String[] args) throws AddressException, MessagingException{  
  27.         SimpleMailSender mailSender = MailSendFactory.getSender();  
  28.         SimpleMail mail = new SimpleMail();  
  29.         mail.setContent("你可以收到邮件了");  
  30.         mail.setSubject("邮件测试");  
  31.         mailSender.send("czy.keith@qq.com", mail);  
  32.     }  
  33.   
  34. }  


好了可以成功收到短信了,在myeclipse下编写此功能的时候可能会无法成功,并且报错,如果发生错误可能是jar包冲突,那么你删除myeclipse下自带的mail功能的jar包即可
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值