Java发邮件简单实现

给出一个Java发送邮件的简单实现。下载

        

Java代码    收藏代码
  1. 1、  接口  
  2. public interface MailSendServDu {  
  3.   
  4.     public void sendEmail(String addressee, String subject, String content);  
  5. }  
  6.   
  7. 2、  实现  
  8. import javax.mail.*;  
  9. import javax.mail.internet.InternetAddress;  
  10. import javax.mail.internet.MimeMessage;  
  11. import java.util.Date;  
  12. import java.util.Map;  
  13. import java.util.Properties;  
  14.   
  15.   
  16. /** 
  17.  * 邮件发送接口 
  18.  * @param 
  19.  * @author Wu Jianguo 
  20.  * @version V1.0 
  21.  * @Description: 
  22.  * @modificationHistory=================重大变更调整记录 
  23.  * @modify by user: Wu Jianguo 
  24.  * @modify by reason:{方法名}:{原因} 
  25.  * @return 
  26.  * @throws 
  27.  */  
  28. public class MailSendServDuImpl implements MailSendServDu{  
  29.   
  30.     private Logger logger = Logger.getLogger(this.getClass());  
  31.   
  32.     // 初始化连接邮件服务器的会话信息  
  33.     private Properties properties = null;  
  34.     // 创建Session实例对象  
  35.     private Session session = null;  
  36.   
  37.     String fromAddress = null;  
  38.     String fromName = null;  
  39.     String charset = null;  
  40.   
  41.     public MailSendServDuImpl() {  
  42.         logger.info("发送邮件相关配置初始化.......");  
  43.   
  44.         Map<String,String> map = PropertiesUtils.getPropertiesValues("/properties/mail.properties");  
  45.         String turnon = map.get("mail.turnon");  
  46.         if (Boolean.parseBoolean(turnon)) {  
  47.             String protocol = map.get("mail.protocol");  
  48.             fromAddress = map.get("mail.fromAddress");  
  49.             fromName = map.get("mail.fromName");  
  50.             String host = map.get("mail.host");  
  51.             String port = map.get("mail.port");  
  52.             String auth = map.get("mail.auth");  
  53.             String username = map.get("mail.username");  
  54.             String password = map.get("mail.password");  
  55.             String debug = map.get("mail.debug");  
  56.             charset = map.get("mail.charset");  
  57.   
  58.             properties = new Properties();  
  59.             properties.setProperty("mail.transport.protocol", protocol);  
  60.             properties.setProperty("mail.smtp.host", host);  
  61.             properties.setProperty("mail.smtp.port", port);  
  62.             properties.setProperty("mail.smtp.auth", auth);  
  63.             properties.setProperty("mail.debug", debug);  
  64.   
  65.             if (Boolean.parseBoolean(auth)) {  
  66.                 session = Session.getDefaultInstance(properties, new HatomAuthenticator(username, password));  
  67.             } else {  
  68.                 session = Session.getDefaultInstance(properties, new HatomAuthenticator());  
  69.             }  
  70.         }  
  71.     }  
  72.   下载
  73.     @Override  
  74.     public void sendEmail(String addressee, String subject, String content) {  
  75.         logger.info("发送邮件");  
  76.   
  77.         MailSendServDuImpl ps = new MailSendServDuImpl();  
  78.   
  79.         try {  
  80.             if (null != properties) {  
  81.                 // 创建MimeMessage实例对象  
  82.                 MimeMessage message = new MimeMessage(session);  
  83.                 // 设置发件人  
  84.                 message.setFrom(new InternetAddress(fromAddress, fromName));  
  85.                 // 设置邮件主题  
  86.                 message.setSubject(subject);  
  87.                 // 设置收件人  
  88.                 message.setRecipient(Message.RecipientType.TO, new InternetAddress(addressee));  
  89.                 // 设置发送时间  
  90.                 message.setSentDate(new Date());  
  91.                 // 设置html内容为邮件正文,指定MIME类型为text/html类型,并指定字符编码  
  92.                 message.setContent(content, "text/html;charset=" + charset);  
  93.                 // 保存并生成最终的邮件内容  
  94.                 message.saveChanges();  
  95.                 // 发送邮件  
  96.                 Transport.send(message);  
  97.             }  
  98.         } catch (Exception e) {  
  99.             System.err.println(e.getMessage());  
  100.             logger.info("发送邮件异常");  
  101.         }  
  102.   
  103.     }  
  104.   
  105.     /** 
  106.      * 向邮件服务器提交认证信息 
  107.      */  
  108.     class HatomAuthenticator extends Authenticator {  
  109.         private String username;  
  110.         private String password;  
  111.   
  112.         public HatomAuthenticator() {  
  113.             super();  
  114.         }  
  115.   
  116.         public HatomAuthenticator(String username, String password) {  
  117.             super();  
  118.             this.username = username;  
  119.             this.password = password;  
  120.         }  
  121.   
  122.         @Override  
  123.         protected PasswordAuthentication getPasswordAuthentication() {  
  124.             return new PasswordAuthentication(username, password);  
  125.         }  
  126.     }  
  127. }  
  128.   
  129. 3、  配置文件  
  130. # 是否打开邮件发送  
  131. mail.turnon=true  
  132. # 邮件发送协议  
  133. mail.protocol=smtp  
  134. # 发信邮箱  
  135. mail.fromAddress=XXXX@163.com  
  136. # 发信人  
  137. mail.fromName=XX  
  138. # smtp端口号  
  139. mail.host=smtp.163.com  
  140. mail.port=25  
  141. # 是否需要验证  
  142. mail.auth=true  
  143. # smtp账号  
  144. mail.username=XXXX@163.com  
  145. # smtp密码  
  146. mail.password=  
  147. # 调试级别,0 关闭,1 一般,2较高  
  148. mail.debug=0  
  149. # 编码  
  150. mail.charset=UTF-8 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值