1、  接口 &"/>

Java发邮件简单实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 


以上是关于Java发邮件简单实现的主要内容,如果未能解决你的问题,请参考以下文章

java mail 发邮件连接不上smtp服务器怎么办

如何用java实现发邮件功能

发邮件出错javamail到底错哪儿了

Java:发邮件

python发邮件

Java实现发邮件功能