1. /* Copyright Notice ===================================================*
  2. * This file contains proprietary information of Trust-Mart Inc which is
  3. * owned by Trust-mart, Inc.
  4. * Copying or reproduction without prior written approval is prohibited.
  5. * Copyright (c) 2004 =================================================*/
  6.  
  7. /* File: EmailUtil.java<p>
  8. * Usage: send email
  9. * Created: 2004-03
  10. */
  11. package com.jk.util;
  12.  
  13. import java.text.DecimalFormat;
  14. import java.util.*;
  15.  
  16. import javax.mail.*;
  17. import javax.mail.internet.*;
  18.  
  19. public class EmailUtil {
  20.  
  21. /* ======================================== *
  22. * Constants
  23. * ======================================== */
  24.  
  25. private static final Properties sy_prop = new Properties();
  26.  
  27. // default key/value
  28. static {
  29. sy_prop.put("mail.transport.protocol", "smtp");
  30. sy_prop.put("mail.debug", "true"); // debug info.
  31. sy_prop.put("mail.smtp.auth", "true"); // 如果server 需要验证
  32. }
  33.  
  34. /* ======================================== *
  35. * Class Methods
  36. * ======================================== */
  37.  
  38. /** Sends plain/text email.
  39. *
  40. * @param session javax.mail.Session object, might got from jboss jndi.
  41. * @param recipients {to, cc1, cc2, ...}
  42. * @param subject
  43. * @param content
  44. * @throw IllegalArgumentException
  45. * @throw MessagingException
  46. */
  47. public static void sendMail(Session session, String[] recipients,
  48. String subject, String content) throws
  49. MessagingException {
  50.  
  51. if (session == null || recipients == null || recipients.length == 0) {
  52. throw new IllegalArgumentException();
  53. }
  54.  
  55. // Message msg = new MimeMessage(session);
  56. MimeMessage msg=new MimeMessage(session);
  57. // msg.setFrom(new InternetAddress(SupplierMail.from));
  58. msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients[0]));
  59. for (int i = 1; i < recipients.length; i++) {
  60. msg.setRecipient(Message.RecipientType.CC,
  61. new InternetAddress(recipients[i]));
  62. }
  63. msg.setSubject(subject,"ISO8859_1");
  64. msg.setText(content,"ISO8859_1");
  65. msg.setSentDate(new java.util.Date());
  66. Transport.send(msg);
  67. }
  68.  
  69. /** Sends plain/text email.
  70. *
  71. * @param session javax.mail.Session object, might got from jboss jndi.
  72. * @param recipients {to, cc1, cc2, ...}
  73. * @param subject
  74. * @param content
  75. * @throw IllegalArgumentException
  76. * @throw MessagingException
  77. */
  78. public static void sendMail(Session session, String from, List recipients,
  79. String subject, String content) throws
  80. MessagingException {
  81.  
  82. if (session == null || recipients == null || recipients.size() == 0) {
  83. throw new IllegalArgumentException();
  84. }
  85.  
  86. // Message msg = new MimeMessage(session);
  87. MimeMessage msg=new MimeMessage(session);
  88. msg.setFrom(new InternetAddress(from));
  89. msg.setRecipient(Message.RecipientType.TO, new InternetAddress((String)recipients.get(0)));
  90. for (int i = 1; i < recipients.size(); i++) {
  91. if(recipients.get(i)!=null)
  92. msg.setRecipient(Message.RecipientType.CC,
  93. new InternetAddress((String)recipients.get(i)));
  94. }
  95. msg.setSubject(subject,"GBK");
  96. msg.setText(content,"GBK");
  97. msg.setSentDate(new java.util.Date());
  98. Transport.send(msg);
  99. }
  100.  
  101. /** User provides info. to
  102. *
  103. * get the Session object to send mail. Uses
  104. * WebApp.getMailSession() instead, if you use jboss.
  105. *
  106. * @param prop
  107. * @param user
  108. * @param password
  109. * @return
  110. * @see javax.mail.Session#getDefaultInstance()
  111. */
  112. public static Session getSession(Properties prop, String user,
  113. String password) {
  114. if (prop == null || user == null || password == null || "".equals(user)) {
  115. throw new IllegalArgumentException();
  116. }
  117.  
  118. //1. copy
  119. Properties prop0 = new Properties(sy_prop);
  120. for (Enumeration enum1 = prop.keys(); enum1.hasMoreElements(); ) {
  121. String key = (String) enum1.nextElement();
  122. String val = (String) prop.getProperty(key);
  123. prop0.setProperty(key, val);
  124. }
  125.  
  126. prop0.put("mail.user", user);
  127. prop0.put("mail.from", user);
  128. //2. get
  129. final String s1 = new String(user), s2 = new String(password);
  130. Authenticator auth = new Authenticator() {
  131. protected PasswordAuthentication getPasswordAuthentication() {
  132. return new PasswordAuthentication(s1, s2);
  133. }
  134. };
  135. Session session = Session.getInstance(prop0, auth);
  136. return session;
  137. }
  138.  
  139. /* ======================================== *
  140. * Testing Methods
  141. * ======================================== */
  142.  
  143. public static void main(String[] args) throws Exception {
  144. Properties prop = new Properties();
  145. prop.put("mail.smtp.host", "10.77.78.10");
  146. //Session session = Session.getDefaultInstance(prop);
  147. Session session = Session.getInstance(prop,null);
  148. String from = "admin@admin.com";
  149. List recipients = new ArrayList();
  150. recipients.add("admin@admin.com");
  151. String subject = "EmailUtil testing--测试";
  152. String content = "13:40:23,437 INFO [STDOUT] User_Name:admin Login_Name:admin IP_Address:127.0.0.1";
  153.  
  154. sendMail(session, from, recipients, subject, content);
  155. System.out.println("OK");
  156. int a=00;
  157. String aa=new DecimalFormat("00").format(a);
  158. System.out.println(new DecimalFormat("00").format(a));
  159. }
  160.  
  161. }

邮件工具类--EmailUtil的更多相关文章

  1. 01_JavaMail_05_创建邮件工具类MailUtils等方便发送邮件

    [工程截图] [代码实现] [Mail.java] package com.Higgin.Utils; import java.util.ArrayList; import java.util.Lis ...

  2. java邮件工具类【最终版】

    http://www.xdemo.org/java-mail/ 对比链接中,添加了抄送和暗抄送功能(已解决,如图代码:抄送不能多个用户,会报错,未解之谜) sendHtmlmail方法可以发送附件以及 ...

  3. java发送邮件完整实例 java邮件工具类

    http://yuncode.net/code/c_552a2e2dc593894 package com.srie.mail; import java.util.Properties; import ...

  4. Java Utils工具类大全(转)

    源码和jar见:https://github.com/evil0ps/utils #Java Utils --- 封装了一些常用Java操作方法,便于重复开发利用. 另外希望身为Java牛牛的你们一起 ...

  5. Java Utils工具类大全

    源码和jar见:https://github.com/evil0ps/utils #Java Utils --- 封装了一些常用Java操作方法,便于重复开发利用. 另外希望身为Java牛牛的你们一起 ...

  6. java中使用javaMail工具类发送邮件

    1.引入依赖 <!--javaMail--> <dependency> <groupId>javax.mail</groupId> <artifa ...

  7. 邮件发送工具类 SendMail.java

    package com.util; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.Simp ...

  8. java 邮件发送工具类

    首先需要下载mail.jar文件,我个人通常是使用maven中心库的那个: <dependency> <groupId>javax.mail</groupId> & ...

  9. 基于JavaMail开发邮件发送器工具类

    基于JavaMail开发邮件发送器工具类 在开发当中肯定会碰到利用Java调用邮件服务器的服务发送邮件的情况,比如账号激活.找回密码等功能.本人之前也碰到多次这样需求,为此特意将功能封装成一个简单易用 ...

随机推荐

  1. Python相关工具清单[持续更新]

    SublimeJEDI : awesome Python autocompletion with SublimeText. Awesome Python : A curated list of awe ...

  2. leetcode@ [316] Remove Duplicate Letters (Stack & Greedy)

    https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase ...

  3. linux内核地址mapping

    linux内核采用页式存储管理,虚拟地址空间划分成固定大小的页面,由MMU(memory manager unit)在运行时将virtual address mapping to (或者说是变化成)某 ...

  4. homework08

    1.局部变量生命周期 #include "iostream.h" ;} int main() { ; add1(n); cout << n << endl; ...

  5. Could not bind factory to JNDI

    将hibernate.cfg.xml中 <session-factory name="SessionFactory">的name属性去掉即可

  6. Java集合框架之LinkedList-----用LinkedList模拟队列和堆栈

    LinkedList的特有方法: (一)添加方法 addFisrt(E e):将指定元素插入此列表的开头.//参数e可以理解成Object对象,因为列表可以接收任何类型的对象,所以e就是Object对 ...

  7. HDU 4882 ZCC Loves Codefires (贪心)

    ZCC Loves Codefires 题目链接: http://acm.hust.edu.cn/vjudge/contest/121349#problem/B Description Though ...

  8. Linux下常用的shell命令记录

     硬件篇 CPU相关 lscpu #查看的是cpu的统计信息. cat /proc/cpuinfo #查看CPU信息详细信息,如每个CPU的型号,主频等 内存相关 free -m #概要查看内存情况 ...

  9. 修改eclipse默认编码方式

     设置js文件的默认编码格式为UTF-8                在Windows->Preference页面中,选择General->Content Types           ...

  10. PsLookupProcessByProcessId分析

    本文是在讨论枚举进程的时候产生的,枚举进程有很多方法,Ring3就是ZwQuerySystemInformation(),传入SysProcessesAndThreadsInformation这个宏, ...