/* Copyright Notice ===================================================*
* This file contains proprietary information of Trust-Mart Inc which is
* owned by Trust-mart, Inc.
* Copying or reproduction without prior written approval is prohibited.
* Copyright (c) 2004 =================================================*/ /* File: EmailUtil.java<p>
* Usage: send email
* Created: 2004-03
*/
package com.jk.util; import java.text.DecimalFormat;
import java.util.*; import javax.mail.*;
import javax.mail.internet.*; public class EmailUtil { /* ======================================== *
* Constants
* ======================================== */ private static final Properties sy_prop = new Properties(); // default key/value
static {
sy_prop.put("mail.transport.protocol", "smtp");
sy_prop.put("mail.debug", "true"); // debug info.
sy_prop.put("mail.smtp.auth", "true"); // 如果server 需要验证
} /* ======================================== *
* Class Methods
* ======================================== */ /** Sends plain/text email.
*
* @param session javax.mail.Session object, might got from jboss jndi.
* @param recipients {to, cc1, cc2, ...}
* @param subject
* @param content
* @throw IllegalArgumentException
* @throw MessagingException
*/
public static void sendMail(Session session, String[] recipients,
String subject, String content) throws
MessagingException { if (session == null || recipients == null || recipients.length == 0) {
throw new IllegalArgumentException();
} // Message msg = new MimeMessage(session);
MimeMessage msg=new MimeMessage(session);
// msg.setFrom(new InternetAddress(SupplierMail.from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients[0]));
for (int i = 1; i < recipients.length; i++) {
msg.setRecipient(Message.RecipientType.CC,
new InternetAddress(recipients[i]));
}
msg.setSubject(subject,"ISO8859_1");
msg.setText(content,"ISO8859_1");
msg.setSentDate(new java.util.Date());
Transport.send(msg);
} /** Sends plain/text email.
*
* @param session javax.mail.Session object, might got from jboss jndi.
* @param recipients {to, cc1, cc2, ...}
* @param subject
* @param content
* @throw IllegalArgumentException
* @throw MessagingException
*/
public static void sendMail(Session session, String from, List recipients,
String subject, String content) throws
MessagingException { if (session == null || recipients == null || recipients.size() == 0) {
throw new IllegalArgumentException();
} // Message msg = new MimeMessage(session);
MimeMessage msg=new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress((String)recipients.get(0)));
for (int i = 1; i < recipients.size(); i++) {
if(recipients.get(i)!=null)
msg.setRecipient(Message.RecipientType.CC,
new InternetAddress((String)recipients.get(i)));
}
msg.setSubject(subject,"GBK");
msg.setText(content,"GBK");
msg.setSentDate(new java.util.Date());
Transport.send(msg);
} /** User provides info. to
*
* get the Session object to send mail. Uses
* WebApp.getMailSession() instead, if you use jboss.
*
* @param prop
* @param user
* @param password
* @return
* @see javax.mail.Session#getDefaultInstance()
*/
public static Session getSession(Properties prop, String user,
String password) {
if (prop == null || user == null || password == null || "".equals(user)) {
throw new IllegalArgumentException();
} //1. copy
Properties prop0 = new Properties(sy_prop);
for (Enumeration enum1 = prop.keys(); enum1.hasMoreElements(); ) {
String key = (String) enum1.nextElement();
String val = (String) prop.getProperty(key);
prop0.setProperty(key, val);
} prop0.put("mail.user", user);
prop0.put("mail.from", user);
//2. get
final String s1 = new String(user), s2 = new String(password);
Authenticator auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(s1, s2);
}
};
Session session = Session.getInstance(prop0, auth);
return session;
} /* ======================================== *
* Testing Methods
* ======================================== */ public static void main(String[] args) throws Exception {
Properties prop = new Properties();
prop.put("mail.smtp.host", "10.77.78.10");
//Session session = Session.getDefaultInstance(prop);
Session session = Session.getInstance(prop,null);
String from = "admin@admin.com";
List recipients = new ArrayList();
recipients.add("admin@admin.com");
String subject = "EmailUtil testing--测试";
String content = "13:40:23,437 INFO [STDOUT] User_Name:admin Login_Name:admin IP_Address:127.0.0.1"; sendMail(session, from, recipients, subject, content);
System.out.println("OK");
int a=00;
String aa=new DecimalFormat("00").format(a);
System.out.println(new DecimalFormat("00").format(a));
} }

邮件工具类--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. asp.net mvc 实体类成员变量标识示例

    检查不能为空 [Required] public string ID { get; set; } 检查最大长度 [StringLength(36, ErrorMessage = "长度不可超 ...

  2. 【转】Maven实战(六)--- dependencies与dependencyManagement的区别

    原博文出自于:http://blog.csdn.net/liutengteng130/article/details/46991829   感谢! 在上一个项目中遇到一些jar包冲突的问题,之后还有很 ...

  3. homework-04 单词方阵

    问题描述 本次作业的题目要求利用给定的一组单词生成一个矩阵,矩阵的每个位置由一个字母填充,单词表中的每一个单词可以匹配矩阵中一段连续的序列,这段序列可以是横向,纵向或者是45度斜角方向,单词可以由左向 ...

  4. Java设计模式系列之中介者模式

    中介者模式(Mediator)的定义 用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 中介者模式(Mediator)的适 ...

  5. Cognos 增加全局类

    Cognos使用版本10.1.1 由于我服务器装的是linux系统下的,所以window系统下的方法,提一下,但是没有实现过. 1.Linux系统下增加全局类 ●修改GlobalReportStyle ...

  6. UVALive 7281 Saint John Festival (凸包+O(logn)判断点在凸多边形内)

    Saint John Festival 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/J Description Porto's ...

  7. POJ 3672 Long Distance Racing (模拟)

    题意:给定一串字符,u表示是上坡,d表示下坡,f表示平坦的,每个有不同的花费时间,问你从开始走,最远能走到. 析:直接模拟就好了,没什么可说的,就是记下时间时要记双倍的,因为要返回来的. 代码如下: ...

  8. <a href="javascript:void(0);" id='test' onclick="javascript:alert('即将上线,敬请期待!');"><em class="rmwd"></em>征稿平台</a>

    <a href="javascript:void(0);" id='test' onclick="javascript:alert('即将上线,敬请期待!');&q ...

  9. 使用WinDbg获得托管方法的汇编代码

    概述:有时候,我们需要查看一个托管方法的汇编指令是怎么样的.记得在大学的时候,我们使用gcc -s和objdump来获得一个c程序代码的汇编指令.但是对于.NET程序来说,我们肯定无法轻松地获得这些内 ...

  10. HttpContext讲解

    http://www.cnblogs.com/scy251147/p/3549503.html http://www.360doc.com/content/14/0526/10/17655805_38 ...