package cnbriccrmutil;
import javautilDate;
import javautilEnumeration;
import javautilProperties;
import javautilVector;
import javaxactivation*;
import javaxmailAuthenticator;
import javaxmailMessage;
import javaxmailMultipart;
import javaxmailPasswordAuthentication;
import javaxmailSession;
import javaxmailTransport;
import javaxmailinternetInternetAddress;
import javaxmailinternetMimeBodyPart;
import javaxmailinternetMimeMessage;
import javaxmailinternetMimeMultipart;
import javaxmailinternetMimeUtility;
import javaioFileInputStream;
import javaioIOException;
import javatextParseException;
import javautilArrayList;
import javautilDate;
import javautilHashSet;
import javautilIterator;
import javautilList;
import javautilLocale;
import javautilSet;
import javautilregexPattern;
/**
* <p>
* Title: 使用javamail发送邮件
* </p>
*/
public class MailUtils {
String to = ;// 收件人
String from = ;// 发件人
String host = ;// smtp主机
String username = ;
String password = ;
String filename = ;// 附件文件名
String subject = ;// 邮件主题
String content = ;// 邮件正文
Vector file = new Vector();// 附件文件集合
/**
* <br>
* 方法说明默认构造器 <br>
* 输入参数 <br>
* 返回类型
*/
public MailUtils() {
}
/**
* <br>
* 方法说明构造器提供直接的参数传入 <br>
* 输入参数 <br>
* 返回类型
*/
public MailUtils(String to String from String smtpServer
String username String password String subject String content) {
thisto = to;
thisfrom = from;
thishost = smtpServer;
thisusername = username;
thispassword = password;
thissubject = subject;
ntent = content;
}
/**
* <br>
* 方法说明设置邮件服务器地址 <br>
* 输入参数String host 邮件服务器地址名称 <br>
* 返回类型
*/
public void setHost(String host) {
thishost = host;
}
/**
* <br>
* 方法说明设置登录服务器校验密码 <br>
* 输入参数 <br>
* 返回类型
*/
public void setPassWord(String pwd) {
thispassword = pwd;
}
/**
* <br>
* 方法说明设置登录服务器校验用户 <br>
* 输入参数 <br>
* 返回类型
*/
public void setUserName(String usn) {
thisusername = usn;
}
/**
* <br>
* 方法说明设置邮件发送目的邮箱 <br>
* 输入参数 <br>
* 返回类型
*/
public void setTo(String to) {
thisto = to;
}
/**
* <br>
* 方法说明设置邮件发送源邮箱 <br>
* 输入参数 <br>
* 返回类型
*/
public void setFrom(String from) {
thisfrom = from;
}
/**
* <br>
* 方法说明设置邮件主题 <br>
* 输入参数 <br>
* 返回类型
*/
public void setSubject(String subject) {
thissubject = subject;
}
/**
* <br>
* 方法说明设置邮件内容 <br>
* 输入参数 <br>
* 返回类型
*/
public void setContent(String content) {
ntent = content;
}
/**
* <br>
* 方法说明把主题转换为中文 <br>
* 输入参数String strText <br>
* 返回类型
*/
public String transferChinese(String strText) {
try {
strText = MimeUtilityencodeText(new String(strTextgetBytes()
GB) GB B);
} catch (Exception e) {
eprintStackTrace();
}
return strText;
}
/**
* <br>
* 方法说明往附件组合中添加附件 <br>
* 输入参数 <br>
* 返回类型
*/
public void attachfile(String fname) {
fileaddElement(fname);
}
/**
* <br>
* 方法说明发送邮件 <br>
* 输入参数 <br>
* 返回类型boolean 成功为true反之为false
*/
public boolean sendMail() {
// 构造mail session
Properties props = new Properties() ;
propsput(mailsmtphost host);
propsput(mailsmtpauth true);
Session session = SessiongetDefaultInstance(props
new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username password);
}
});
//Session session = SessiongetDefaultInstance(props);
// Session session = SessiongetDefaultInstance(props null);
try {
// 构造MimeMessage 并设定基本的值
MimeMessage msg = new MimeMessage(session);
//MimeMessage msg = new MimeMessage();
msgsetFrom(new InternetAddress(from));
//msgaddRecipients(MessageRecipientTypeTO address); //这个只能是给一个人发送email
msgsetRecipients(MessageRecipientTypeBCC InternetAddressparse(to)) ;
subject = transferChinese(subject);
msgsetSubject(subject);
// 构造Multipart
Multipart mp = new MimeMultipart();
// 向Multipart添加正文
MimeBodyPart mbpContent = new MimeBodyPart();
mbpContentsetContent(content text/html;charset=gb);
// 向MimeMessage添加(Multipart代表正文)
mpaddBodyPart(mbpContent);
// 向Multipart添加附件
Enumeration efile = fileelements();
while (efilehasMoreElements()) {
MimeBodyPart mbpFile = new MimeBodyPart();
filename = efilenextElement()toString();
FileDataSource fds = new FileDataSource(filename);
mbpFilesetDataHandler(new DataHandler(fds));
mbpFilesetFileName(fdsgetName());
// 向MimeMessage添加(Multipart代表附件)
mpaddBodyPart(mbpFile);
}
fileremoveAllElements();
// 向Multipart添加MimeMessage
msgsetContent(mp);
msgsetSentDate(new Date());
msgsaveChanges() ;
// 发送邮件
Transport transport = sessiongetTransport(smtp);
nnect(host username password);
transportsendMessage(msg msggetAllRecipients());
transportclose();
} catch (Exception mex) {
mexprintStackTrace();
// Exception ex = null;
// if ((ex = mexgetNextException()) != null) {
// exprintStackTrace();
// }
return false;
}
return true;
}
/**
* <br>
* 方法说明主方法用于测试 <br>
* 输入参数 <br>
* 返回类型
*/
public static void main(String[] args) {
MailUtils sendmail = new MailUtils();
sendmailsetHost();
sendmailsetUserName(se);
sendmailsetPassWord(bric);
sendmailsetTo(che);
sendmailsetFrom(se);
sendmailsetSubject(你好这是测试!);
sendmailsetContent(你好这是一个带多附件的测试!);
// Mail sendmail = new
// Mail(du_jiang你好胃你好吗?);
sendmailattachfile(d:\\newsrar);
sendmailattachfile(d:\\jhjlrar);
Systemoutprintln(sendmailsendMail());
}
}