package system;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.omg.CORBA.SystemException;
/**
* 系统邮件处理
* <p>
* Title: 系统邮件处理
* </p>
* <p>
* Copyright: Copyright (c) 2007
* </p>
*
* @version 1.0
*/
public class MailHandle {
/*
本方法将邮件发送出去,邮件内容为文本内容,并带有附件
MailInfo是一个DTO类
*/
public static void send(MailInfo mail) throws Exception
{
try {
Properties props = new Properties();
String smtpServer = mail.getSmtpServer();
String SmtpUser = mail.getUserName();
String SmtpPwd = mail.getPassword();
String subject = "";
if(mail.getSubject()!=null) subject = mail.getSubject();
if(smtpServer==null)
{
// smtpServer = Config.getInstance().getProperties("defaultSmtpServer");
// SmtpUser = Config.getInstance().getProperties("defaultSmtpUser");
// SmtpPwd = Config.getInstance().getProperties("defaultSmtpPwd");
smtpServer = "mail.it363.com";
SmtpUser = "web@it363.com";
SmtpPwd = "123456";
}
props.put("mail.smtp.host", smtpServer); //设置邮件smtp服务器地址
props.put("mail.smtp.auth","true"); //设置服务器smtp需要验证
Session sendMailSession = Session.getInstance(props, null);
MimeMessage newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(mail.getFrom()));
InternetAddress[] address = {new InternetAddress(mail.getTo())};
newMessage.setRecipients(Message.RecipientType.TO, address);
if( mail.getCc()!=null ) //判断是否存在邮件抄送
{
InternetAddress[] addresscc = {new InternetAddress(mail.getCc())};
newMessage.setRecipients(Message.RecipientType.CC, addresscc);
}
if( mail.getBcc()!=null ) //判断是否存在邮件密送
{
InternetAddress[] addressbcc = {new InternetAddress(mail.getBcc())};
newMessage.setRecipients(Message.RecipientType.CC, addressbcc);
}
newMessage.setSubject(subject); //设定主题
newMessage.setSentDate(new Date()); //设定发送时间
newMessage.setContent(mail.getContent(),"text/html;charset=gbk");
//对邮件发送内容中分附近进行处理
if(mail.getAttachment()==null)
{
//newMessage.setDataHandler(new DataHandler(mail.getContent(),"text/html")); //普通文本邮件
}
else {
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(mail.getContent(),"text/html")); //普通文本邮件
messageBodyPart.setContent(mail.getContent(),"text/html;charset=gbk");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(mail.getAttachment());
messageBodyPart.setDataHandler(new DataHandler(source));
File fn = new File(mail.getAttachment());
String fileName = fn.getName();
if(fileName!=null&&!"".equals(fileName)){
fileName = new String(fileName.getBytes("gb2312"),"iso-8859-1");
}
messageBodyPart.setContent(mail.getContent(),"text/html;charset=gbk");
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
newMessage.setContent(multipart);
}
//开始连接smtp服务器,并检查是否通过smtp发信验证。
Transport transport = sendMailSession.getTransport("smtp");
transport.connect(smtpServer,SmtpUser,SmtpPwd);
transport.sendMessage(newMessage,newMessage.getAllRecipients());
transport.close();
}
catch(MessagingException e)
{
System.out.println("邮件发送失败");
e.printStackTrace();
}
}
/**
* 群发邮件.带附件
* @param mail
* @param to
* @throws Exception
*/
public static boolean sendMutliUser(MailInfo mail) throws Exception
{
boolean bool = false;
try
{
Properties props = new Properties();
String smtpServer = mail.getSmtpServer();
String SmtpUser = mail.getUserName();
String SmtpPwd = mail.getPassword();
String subject = "";
if(mail.getSubject()!=null) subject = mail.getSubject();
if(smtpServer==null)
{
// smtpServer = Config.getInstance().getProperties("defaultSmtpServer");
// SmtpUser = Config.getInstance().getProperties("defaultSmtpUser");
// SmtpPwd = Config.getInstance().getProperties("defaultSmtpPwd");
smtpServer = "mail.it363.com";
SmtpUser = "web@it363.com";
SmtpPwd = "123456";
}
props.put("mail.smtp.host", smtpServer); //设置邮件smtp服务器地址
props.put("mail.smtp.auth","true"); //设置服务器smtp需要验证
Session sendMailSession = Session.getInstance(props, null);
MimeMessage newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(mail.getFrom()));
// InternetAddress[] address = {new InternetAddress(mail.getTo())};
// 开始设置群发邮件地址
InternetAddress[] address = new InternetAddress[mail.getMutliTo().length];
for (int i = 0; i < mail.getMutliTo().length; i++) {
address[i] = new InternetAddress((mail.getMutliTo())[i]);
}
newMessage.setRecipients(Message.RecipientType.TO, address);
if( mail.getCc()!=null ) //判断是否存在邮件抄送
{
InternetAddress[] addresscc = {new InternetAddress(mail.getCc())};
newMessage.setRecipients(Message.RecipientType.CC, addresscc);
}
if( mail.getBcc()!=null ) //判断是否存在邮件密送
{
InternetAddress[] addressbcc = {new InternetAddress(mail.getBcc())};
newMessage.setRecipients(Message.RecipientType.CC, addressbcc);
}
newMessage.setSubject(subject); //设定主题
newMessage.setSentDate(new Date()); //设定发送时间
newMessage.setContent(mail.getContent(),"text/html;charset=gbk");
//对邮件发送内容中分附近进行处理
if(mail.getAttachment()==null)
{
//newMessage.setDataHandler(new DataHandler(mail.getContent(),"text/html")); //html邮件
}
else
{
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(mail.getContent(),"text/html")); //html邮件
messageBodyPart.setContent(mail.getContent(),"text/html;charset=gbk");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(mail.getAttachment());
messageBodyPart.setDataHandler(new DataHandler(source));
File fn = new File(mail.getAttachment());
String fileName = fn.getName();
if(fileName!=null&&!"".equals(fileName)){
fileName = new String(fileName.getBytes("gb2312"),"iso-8859-1");
}
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
newMessage.setContent(multipart);
}
//开始连接smtp服务器,并检查是否通过smtp发信验证。
Transport transport = sendMailSession.getTransport("smtp");
transport.connect(smtpServer,SmtpUser,SmtpPwd);
transport.sendMessage(newMessage,newMessage.getAllRecipients());
bool = true;
transport.close();
}
catch(MessagingException e)
{
System.out.println("邮件发送失败");
e.printStackTrace();
}
return bool;
}
/**
* 本方法将html格式的邮件内容发送出去
*/
public static void sendHtml(MailInfo mail) throws SystemException
{
try
{
Properties props = new Properties();
String smtpServer = mail.getSmtpServer();
String SmtpUser = mail.getUserName();
String SmtpPwd = mail.getPassword();
String subject = "";
if(mail.getSubject()!=null) subject = mail.getSubject();
if(smtpServer==null)
{
smtpServer = "mail.huntingpr.com";
SmtpUser = "media@huntingpr.com";
SmtpPwd = "media";
}
props.put("mail.smtp.host", smtpServer); //设置邮件smtp服务器地址
props.put("mail.smtp.auth","true"); //设置服务器smtp需要验证
Session sendMailSession = Session.getInstance(props, null);
MimeMessage newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(mail.getFrom()));
InternetAddress[] address = {new InternetAddress(mail.getTo())};
newMessage.setRecipients(Message.RecipientType.TO, address);
if( mail.getCc()!=null ) //判断是否存在邮件抄送
{
// InternetAddress[] addresscc = {new InternetAddress(mail.getCc())};
newMessage.setRecipients(Message.RecipientType.CC,InternetAddress.parse(mail.getCc()));
}
if( mail.getBcc()!=null ) //判断是否存在邮件密送
{
InternetAddress[] addressbcc = {new InternetAddress(mail.getBcc())};
newMessage.setRecipients(Message.RecipientType.CC, addressbcc);
}
newMessage.setSubject(subject); //设定主题
newMessage.setSentDate(new Date()); //设定发送时间
newMessage.setContent(mail.getContent(),"text/html;charset=gbk");
//对邮件发送内容中分附近进行处理
if(mail.getAttachment()==null)
{
//newMessage.setDataHandler(new DataHandler(mail.getContent(),"text/html"));
}
else
{
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(mail.getContent(),"text/html"));
messageBodyPart.setContent(mail.getContent(),"text/html;charset=gbk");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(mail.getAttachment());
messageBodyPart.setDataHandler(new DataHandler(source));
File fn = new File(mail.getAttachment());
messageBodyPart.setFileName(fn.getName());
multipart.addBodyPart(messageBodyPart);
newMessage.setContent(multipart);
}
//开始连接smtp服务器,并检查是否通过smtp发信验证。
Transport transport = sendMailSession.getTransport("smtp");
transport.connect(smtpServer,SmtpUser,SmtpPwd);
transport.sendMessage(newMessage,newMessage.getAllRecipients());
transport.close();
}
catch(MessagingException e)
{
System.out.println("邮件发送失败");
e.printStackTrace();
}
}
/**
* 本方法将html格式的邮件内容发送给一个用户
*
* @param to
* String
* @param from
* String
* @param subject
* String
* @param content
* String
* @throws SystemException
*/
public static void send(String to, String from, String subject,
String content) throws SystemException {
try {
Properties props = new Properties();
String smtpServer = "mail.it363.com";
String SmtpUser = "web@it363.com";
String SmtpPwd = "123456";
if (from == null || from.equals("")) {
from = "web@it363.com";
}
props.put("mail.smtp.host", smtpServer); // 设置邮件smtp服务器地址
props.put("mail.smtp.auth", "true"); // 设置服务器smtp需要验证
Session sendMailSession = Session.getInstance(props, null);
MimeMessage newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to) };
newMessage.setRecipients(Message.RecipientType.TO, address);
newMessage.setSubject(subject); // 设定主题
newMessage.setSentDate(new Date()); // 设定发送时间
// 对邮件发送内容中进行编码处理,支持中文
content = new String(content.getBytes("GB2312"), "ISO8859-1");
newMessage.setDataHandler(new DataHandler(content, "text/html"));
// 开始连接smtp服务器,并检查是否通过smtp发信验证。
Transport transport = sendMailSession.getTransport("smtp");
transport.connect(smtpServer, SmtpUser, SmtpPwd);
transport.sendMessage(newMessage, newMessage.getAllRecipients());
transport.close();
} catch (MessagingException ex) {
System.out.println("邮件发送失败");
ex.printStackTrace();
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
}
/**
* 本方法将html格式的邮件内容发送给多个用户
*
* @param to
* ArrayList
* @param from
* String
* @param subject
* String
* @param content
* String
* @throws SystemException
*/
public static void send(ArrayList to, String from, String subject,String content) throws SystemException {
try {
Properties props = new Properties();
String smtpServer = "mail.it363.com";
String SmtpUser = "web@it363.com";
String SmtpPwd = "123456";
if (from == null || from.equals("")) {
from = "web@it363.com";
}
props.put("mail.smtp.host", smtpServer); // 设置邮件smtp服务器地址
props.put("mail.smtp.auth", "true"); // 设置服务器smtp需要验证
Session sendMailSession = Session.getInstance(props, null);
MimeMessage newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(from));
// 开始设置群发邮件地址
InternetAddress[] address = new InternetAddress[to.size()];
for (int i = 0; i < to.size(); i++) {
address[i] = new InternetAddress((String) to.get(i));
}
newMessage.setRecipients(Message.RecipientType.TO, address);
newMessage.setSubject(subject); // 设定主题
newMessage.setSentDate(new Date()); // 设定发送时间
// 对邮件发送内容中进行编码处理,支持中文
content = new String(content.getBytes("GB2312"), "ISO8859-1");
newMessage.setDataHandler(new DataHandler(content, "text/html"));
// 开始连接smtp服务器,并检查是否通过smtp发信验证。
Transport transport = sendMailSession.getTransport("smtp");
transport.connect(smtpServer, SmtpUser, SmtpPwd);
transport.sendMessage(newMessage, newMessage.getAllRecipients());
transport.close();
} catch (MessagingException ex) {
System.out.println("邮件发送失败");
ex.printStackTrace();
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
}
/*
public static void main(String[] args) {
try{
//String[] usersMail = {"linuxok@yahoo.com.cn","xueok2000@163.com"};
// 如果要群发,需要usersMail ,并把mInfo.setTo("shigong@luckto.com")改为mInfo.setMutliTo(usersMail)
MailInfo mInfo = new MailInfo();
mInfo.setSubject("华讯天下群发邮件:");
mInfo.setFrom("media@huntingpr.com");
mInfo.setTo("linuxok@yahoo.com.cn");
// mInfo.setMutliTo(usersMail);
mInfo.setContent("<h1>我发的信</h1>");
//mInfo.setAttachment("c://厂商修改内容.txt");
mInfo.setSmtpServer("mail.huntingpr.com");
mInfo.setUserName("media@huntingpr.com");
mInfo.setPassword("media");
mInfo.setCc("xueok2000@163.com,media@huntingrp.com");
MailHandle mail = new MailHandle();
// mail.send("shigong@luckto.com","baozhijie@luckto.com","testMail","testMail");
mail.sendHtml(mInfo);
//mail.sendMutliUser(mInfo);
}catch (Exception e){
e.printStackTrace();
}
}
*/
}
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.omg.CORBA.SystemException;
/**
* 系统邮件处理
* <p>
* Title: 系统邮件处理
* </p>
* <p>
* Copyright: Copyright (c) 2007
* </p>
*
* @version 1.0
*/
public class MailHandle {
/*
本方法将邮件发送出去,邮件内容为文本内容,并带有附件
MailInfo是一个DTO类
*/
public static void send(MailInfo mail) throws Exception
{
try {
Properties props = new Properties();
String smtpServer = mail.getSmtpServer();
String SmtpUser = mail.getUserName();
String SmtpPwd = mail.getPassword();
String subject = "";
if(mail.getSubject()!=null) subject = mail.getSubject();
if(smtpServer==null)
{
// smtpServer = Config.getInstance().getProperties("defaultSmtpServer");
// SmtpUser = Config.getInstance().getProperties("defaultSmtpUser");
// SmtpPwd = Config.getInstance().getProperties("defaultSmtpPwd");
smtpServer = "mail.it363.com";
SmtpUser = "web@it363.com";
SmtpPwd = "123456";
}
props.put("mail.smtp.host", smtpServer); //设置邮件smtp服务器地址
props.put("mail.smtp.auth","true"); //设置服务器smtp需要验证
Session sendMailSession = Session.getInstance(props, null);
MimeMessage newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(mail.getFrom()));
InternetAddress[] address = {new InternetAddress(mail.getTo())};
newMessage.setRecipients(Message.RecipientType.TO, address);
if( mail.getCc()!=null ) //判断是否存在邮件抄送
{
InternetAddress[] addresscc = {new InternetAddress(mail.getCc())};
newMessage.setRecipients(Message.RecipientType.CC, addresscc);
}
if( mail.getBcc()!=null ) //判断是否存在邮件密送
{
InternetAddress[] addressbcc = {new InternetAddress(mail.getBcc())};
newMessage.setRecipients(Message.RecipientType.CC, addressbcc);
}
newMessage.setSubject(subject); //设定主题
newMessage.setSentDate(new Date()); //设定发送时间
newMessage.setContent(mail.getContent(),"text/html;charset=gbk");
//对邮件发送内容中分附近进行处理
if(mail.getAttachment()==null)
{
//newMessage.setDataHandler(new DataHandler(mail.getContent(),"text/html")); //普通文本邮件
}
else {
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(mail.getContent(),"text/html")); //普通文本邮件
messageBodyPart.setContent(mail.getContent(),"text/html;charset=gbk");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(mail.getAttachment());
messageBodyPart.setDataHandler(new DataHandler(source));
File fn = new File(mail.getAttachment());
String fileName = fn.getName();
if(fileName!=null&&!"".equals(fileName)){
fileName = new String(fileName.getBytes("gb2312"),"iso-8859-1");
}
messageBodyPart.setContent(mail.getContent(),"text/html;charset=gbk");
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
newMessage.setContent(multipart);
}
//开始连接smtp服务器,并检查是否通过smtp发信验证。
Transport transport = sendMailSession.getTransport("smtp");
transport.connect(smtpServer,SmtpUser,SmtpPwd);
transport.sendMessage(newMessage,newMessage.getAllRecipients());
transport.close();
}
catch(MessagingException e)
{
System.out.println("邮件发送失败");
e.printStackTrace();
}
}
/**
* 群发邮件.带附件
* @param mail
* @param to
* @throws Exception
*/
public static boolean sendMutliUser(MailInfo mail) throws Exception
{
boolean bool = false;
try
{
Properties props = new Properties();
String smtpServer = mail.getSmtpServer();
String SmtpUser = mail.getUserName();
String SmtpPwd = mail.getPassword();
String subject = "";
if(mail.getSubject()!=null) subject = mail.getSubject();
if(smtpServer==null)
{
// smtpServer = Config.getInstance().getProperties("defaultSmtpServer");
// SmtpUser = Config.getInstance().getProperties("defaultSmtpUser");
// SmtpPwd = Config.getInstance().getProperties("defaultSmtpPwd");
smtpServer = "mail.it363.com";
SmtpUser = "web@it363.com";
SmtpPwd = "123456";
}
props.put("mail.smtp.host", smtpServer); //设置邮件smtp服务器地址
props.put("mail.smtp.auth","true"); //设置服务器smtp需要验证
Session sendMailSession = Session.getInstance(props, null);
MimeMessage newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(mail.getFrom()));
// InternetAddress[] address = {new InternetAddress(mail.getTo())};
// 开始设置群发邮件地址
InternetAddress[] address = new InternetAddress[mail.getMutliTo().length];
for (int i = 0; i < mail.getMutliTo().length; i++) {
address[i] = new InternetAddress((mail.getMutliTo())[i]);
}
newMessage.setRecipients(Message.RecipientType.TO, address);
if( mail.getCc()!=null ) //判断是否存在邮件抄送
{
InternetAddress[] addresscc = {new InternetAddress(mail.getCc())};
newMessage.setRecipients(Message.RecipientType.CC, addresscc);
}
if( mail.getBcc()!=null ) //判断是否存在邮件密送
{
InternetAddress[] addressbcc = {new InternetAddress(mail.getBcc())};
newMessage.setRecipients(Message.RecipientType.CC, addressbcc);
}
newMessage.setSubject(subject); //设定主题
newMessage.setSentDate(new Date()); //设定发送时间
newMessage.setContent(mail.getContent(),"text/html;charset=gbk");
//对邮件发送内容中分附近进行处理
if(mail.getAttachment()==null)
{
//newMessage.setDataHandler(new DataHandler(mail.getContent(),"text/html")); //html邮件
}
else
{
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(mail.getContent(),"text/html")); //html邮件
messageBodyPart.setContent(mail.getContent(),"text/html;charset=gbk");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(mail.getAttachment());
messageBodyPart.setDataHandler(new DataHandler(source));
File fn = new File(mail.getAttachment());
String fileName = fn.getName();
if(fileName!=null&&!"".equals(fileName)){
fileName = new String(fileName.getBytes("gb2312"),"iso-8859-1");
}
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
newMessage.setContent(multipart);
}
//开始连接smtp服务器,并检查是否通过smtp发信验证。
Transport transport = sendMailSession.getTransport("smtp");
transport.connect(smtpServer,SmtpUser,SmtpPwd);
transport.sendMessage(newMessage,newMessage.getAllRecipients());
bool = true;
transport.close();
}
catch(MessagingException e)
{
System.out.println("邮件发送失败");
e.printStackTrace();
}
return bool;
}
/**
* 本方法将html格式的邮件内容发送出去
*/
public static void sendHtml(MailInfo mail) throws SystemException
{
try
{
Properties props = new Properties();
String smtpServer = mail.getSmtpServer();
String SmtpUser = mail.getUserName();
String SmtpPwd = mail.getPassword();
String subject = "";
if(mail.getSubject()!=null) subject = mail.getSubject();
if(smtpServer==null)
{
smtpServer = "mail.huntingpr.com";
SmtpUser = "media@huntingpr.com";
SmtpPwd = "media";
}
props.put("mail.smtp.host", smtpServer); //设置邮件smtp服务器地址
props.put("mail.smtp.auth","true"); //设置服务器smtp需要验证
Session sendMailSession = Session.getInstance(props, null);
MimeMessage newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(mail.getFrom()));
InternetAddress[] address = {new InternetAddress(mail.getTo())};
newMessage.setRecipients(Message.RecipientType.TO, address);
if( mail.getCc()!=null ) //判断是否存在邮件抄送
{
// InternetAddress[] addresscc = {new InternetAddress(mail.getCc())};
newMessage.setRecipients(Message.RecipientType.CC,InternetAddress.parse(mail.getCc()));
}
if( mail.getBcc()!=null ) //判断是否存在邮件密送
{
InternetAddress[] addressbcc = {new InternetAddress(mail.getBcc())};
newMessage.setRecipients(Message.RecipientType.CC, addressbcc);
}
newMessage.setSubject(subject); //设定主题
newMessage.setSentDate(new Date()); //设定发送时间
newMessage.setContent(mail.getContent(),"text/html;charset=gbk");
//对邮件发送内容中分附近进行处理
if(mail.getAttachment()==null)
{
//newMessage.setDataHandler(new DataHandler(mail.getContent(),"text/html"));
}
else
{
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(mail.getContent(),"text/html"));
messageBodyPart.setContent(mail.getContent(),"text/html;charset=gbk");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(mail.getAttachment());
messageBodyPart.setDataHandler(new DataHandler(source));
File fn = new File(mail.getAttachment());
messageBodyPart.setFileName(fn.getName());
multipart.addBodyPart(messageBodyPart);
newMessage.setContent(multipart);
}
//开始连接smtp服务器,并检查是否通过smtp发信验证。
Transport transport = sendMailSession.getTransport("smtp");
transport.connect(smtpServer,SmtpUser,SmtpPwd);
transport.sendMessage(newMessage,newMessage.getAllRecipients());
transport.close();
}
catch(MessagingException e)
{
System.out.println("邮件发送失败");
e.printStackTrace();
}
}
/**
* 本方法将html格式的邮件内容发送给一个用户
*
* @param to
* String
* @param from
* String
* @param subject
* String
* @param content
* String
* @throws SystemException
*/
public static void send(String to, String from, String subject,
String content) throws SystemException {
try {
Properties props = new Properties();
String smtpServer = "mail.it363.com";
String SmtpUser = "web@it363.com";
String SmtpPwd = "123456";
if (from == null || from.equals("")) {
from = "web@it363.com";
}
props.put("mail.smtp.host", smtpServer); // 设置邮件smtp服务器地址
props.put("mail.smtp.auth", "true"); // 设置服务器smtp需要验证
Session sendMailSession = Session.getInstance(props, null);
MimeMessage newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to) };
newMessage.setRecipients(Message.RecipientType.TO, address);
newMessage.setSubject(subject); // 设定主题
newMessage.setSentDate(new Date()); // 设定发送时间
// 对邮件发送内容中进行编码处理,支持中文
content = new String(content.getBytes("GB2312"), "ISO8859-1");
newMessage.setDataHandler(new DataHandler(content, "text/html"));
// 开始连接smtp服务器,并检查是否通过smtp发信验证。
Transport transport = sendMailSession.getTransport("smtp");
transport.connect(smtpServer, SmtpUser, SmtpPwd);
transport.sendMessage(newMessage, newMessage.getAllRecipients());
transport.close();
} catch (MessagingException ex) {
System.out.println("邮件发送失败");
ex.printStackTrace();
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
}
/**
* 本方法将html格式的邮件内容发送给多个用户
*
* @param to
* ArrayList
* @param from
* String
* @param subject
* String
* @param content
* String
* @throws SystemException
*/
public static void send(ArrayList to, String from, String subject,String content) throws SystemException {
try {
Properties props = new Properties();
String smtpServer = "mail.it363.com";
String SmtpUser = "web@it363.com";
String SmtpPwd = "123456";
if (from == null || from.equals("")) {
from = "web@it363.com";
}
props.put("mail.smtp.host", smtpServer); // 设置邮件smtp服务器地址
props.put("mail.smtp.auth", "true"); // 设置服务器smtp需要验证
Session sendMailSession = Session.getInstance(props, null);
MimeMessage newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(from));
// 开始设置群发邮件地址
InternetAddress[] address = new InternetAddress[to.size()];
for (int i = 0; i < to.size(); i++) {
address[i] = new InternetAddress((String) to.get(i));
}
newMessage.setRecipients(Message.RecipientType.TO, address);
newMessage.setSubject(subject); // 设定主题
newMessage.setSentDate(new Date()); // 设定发送时间
// 对邮件发送内容中进行编码处理,支持中文
content = new String(content.getBytes("GB2312"), "ISO8859-1");
newMessage.setDataHandler(new DataHandler(content, "text/html"));
// 开始连接smtp服务器,并检查是否通过smtp发信验证。
Transport transport = sendMailSession.getTransport("smtp");
transport.connect(smtpServer, SmtpUser, SmtpPwd);
transport.sendMessage(newMessage, newMessage.getAllRecipients());
transport.close();
} catch (MessagingException ex) {
System.out.println("邮件发送失败");
ex.printStackTrace();
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
}
/*
public static void main(String[] args) {
try{
//String[] usersMail = {"linuxok@yahoo.com.cn","xueok2000@163.com"};
// 如果要群发,需要usersMail ,并把mInfo.setTo("shigong@luckto.com")改为mInfo.setMutliTo(usersMail)
MailInfo mInfo = new MailInfo();
mInfo.setSubject("华讯天下群发邮件:");
mInfo.setFrom("media@huntingpr.com");
mInfo.setTo("linuxok@yahoo.com.cn");
// mInfo.setMutliTo(usersMail);
mInfo.setContent("<h1>我发的信</h1>");
//mInfo.setAttachment("c://厂商修改内容.txt");
mInfo.setSmtpServer("mail.huntingpr.com");
mInfo.setUserName("media@huntingpr.com");
mInfo.setPassword("media");
mInfo.setCc("xueok2000@163.com,media@huntingrp.com");
MailHandle mail = new MailHandle();
// mail.send("shigong@luckto.com","baozhijie@luckto.com","testMail","testMail");
mail.sendHtml(mInfo);
//mail.sendMutliUser(mInfo);
}catch (Exception e){
e.printStackTrace();
}
}
*/
}