java实现邮件发送功能工具类

使用mail-user.jar, mail.jar实现java发送邮件的功能。

发送TEXT方式和发送Html方式。

Java代码 复制代码 收藏代码
  1. package com.***.***.core.util;
  2. import java.io.BufferedInputStream;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.util.Properties;
  8. import com.java.entity.Email;
  9. import com.java.mail.SimpleMailSender;
  10. public class EmailUtil {
  11. private static Email mailInfo = new Email();
  12. private static Properties prop = new Properties();
  13. //获取发送服务器配置
  14. static {
  15. InputStream in = null;
  16. try {
  17. String filePath = EmailProperties.class.getResource("/").getPath()+"email.properties";
  18. in = new BufferedInputStream(new FileInputStream(filePath));
  19. } catch (FileNotFoundException e1) {
  20. e1.printStackTrace();
  21. }
  22. try {
  23. prop.load(in);
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. /**
  29. * 根据key获取properties的value
  30. *
  31. * @param key
  32. * @return
  33. */
  34. public static String getValue(String key) {
  35. return prop.getProperty(key).trim();
  36. }
  37. /**
  38. * 封装邮件发送对象
  39. *
  40. * @param subject
  41. * @param content
  42. * @param toAddress
  43. */
  44. private static void getMailInfo(String subject, String content,
  45. String toAddress) {
  46. mailInfo.setMailServerHost(getValue("mailServerHost"));//发送服务器地址
  47. mailInfo.setMailServerPort(getValue("mailServerPort"));//发送服务端口
  48. mailInfo.setValidate(true);
  49. mailInfo.setUserName(getValue("userName"));//发送者账号
  50. mailInfo.setPassword(getValue("password"));// 发送者邮箱密码
  51. mailInfo.setFromAddress(getValue("fromAddress"));//发送地址
  52. mailInfo.setToAddress(toAddress.trim());//接收地址,邮箱地址
  53. if (subject != null && !"".equals(subject)) {
  54. mailInfo.setSubject(subject);//邮件主题
  55. } else {
  56. mailInfo.setSubject(getValue("subject"));
  57. }
  58. if (content != null && !"".equals(content)) {
  59. mailInfo.setContent(content);//邮件内容
  60. } else {
  61. mailInfo.setContent(getValue("content"));
  62. }
  63. }
  64. /**
  65. * 以文本文件的形式发送邮件
  66. *
  67. * @param subject
  68. * 主题
  69. * @param content
  70. * 内容
  71. * @param toAddress
  72. * 目标地址
  73. * @return
  74. */
  75. public static boolean sendContentEmail(String subject, String content,
  76. String toAddress) {
  77. if (toAddress != null && !"".equals(toAddress)) {
  78. getMailInfo(subject, content, toAddress);
  79. SimpleMailSender sms = new SimpleMailSender();
  80. return sms.sendTextMail(mailInfo);
  81. }
  82. return false;
  83. }
  84. /**
  85. * 以html格式发送邮件信息
  86. *
  87. * @param subject
  88. * 主题
  89. * @param content
  90. * 内容
  91. * @param toAddress
  92. * 目标地址
  93. * @return
  94. */
  95. public static boolean sendHtmlEmail(String subject, String content,
  96. String toAddress) {
  97. if (toAddress != null && !"".equals(toAddress)) {
  98. getMailInfo(subject, content, toAddress);
  99. return SimpleMailSender.sendHtmlMail(mailInfo);
  100. }
  101. return false;
  102. }
  103. public static String contentToHtml(String url) {
  104. StringBuffer email = new StringBuffer(
  105. "<html xmlns='http://www.w3.org/1999/xhtml'>");
  106. email.append("<head>");
  107. email.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
  108. email.append("<title>**订单支付</title>");
  109. email.append("</head><body>");
  110. email.append("<p><u><a href=\""+url+"\">"+url+"</a></u> &#13;</p>");
  111. email.append("</body></html>");
  112. return email.toString();
  113. }
  114. public static void main(String[] args) {
  115. }
  116. }  
posted on 2013-02-08 17:36  蜜雪薇琪  阅读(295)  评论(0编辑  收藏  举报