Posted on 2006-04-07 10:38
砖头 阅读(500)
评论(0) 编辑 收藏 所属分类:
JSP
/*
* MailKit.java
*
* Created on 2004年8月21日, 下午2:11
*/
package javatip;
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javatip.SMTPAuthenticator;
import javax.activation.FileDataSource;
/**
*/
public class MailKit {
private String host;
private String userMail;
private String pwd;
/** Creates a new instance of MailKit
*@param mailHost: 邮件服务器,如 smtp.163.com
*@param userAccount: 用户, 如 admin
*@param password: 登陆密码,如 123456
*/ private MailKit(String mailHost,String userMail, String password)
{
init(mailHost,userMail,password);
}
/** 初始化邮件服务器
*@param mailHost: 邮件服务器,如 smtp.163.com
*@param userAccount: 用户, 如 admin
*@param pwd: 登陆密码,如 123456
*/
private void init(String mailHost,String userMail, String password) {
if(mailHost == null || userMail == null || password == null) {
throw new IllegalArgumentException("传人的参数不能为空");
}
this.host = mailHost;
this.userMail = userMail;
this.pwd = password;
}
/** 发送邮件
* @param to 邮件接受者,可以有多个接受者 support@163.com
* @param subject 邮件主题
* @param msgContent 邮件正文
* @param attachFile 邮件附件,可以有多个附件;
* 传入的参数为文件数组,文件必须为完整路径名 c:\\11.jsp
* null表示没有附件
* @return 发送成功返回true,否则返回false
*/
public boolean sendMail(String[] to, String subject,String msgContent,String[] attachFile) {
try {
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth","true");
props.put("username",userMail);
props.put("password",pwd);
Authenticator auth = new SMTPAuthenticator(userMail, pwd);
Session sendMailSession = Session.getDefaultInstance(props, auth);
Message msg = new MimeMessage(sendMailSession);
msg.setFrom(new InternetAddress(userMail));
if(to != null && to.length > 0) {
InternetAddress[] address = new InternetAddress[to.length];
for(int i=0; i address[i] = new InternetAddress(to[i]);
}
msg.setRecipients(Message.RecipientType.TO, address);
}
else {
return false;
}
msg.setSubject(subject);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgContent);
// create the Multipart and add its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
// create the second message part
if(attachFile != null && attachFile.length >0) {
MimeBodyPart mbp2;
for(int i=0; i mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource(attachFile[i]);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
mp.addBodyPart(mbp2);
}
}
// add the Multipart to the message
msg.setContent(mp);
// set the Date: header
msg.setSentDate(new Date());
// send the message
Transport.send(msg);
}
catch(Exception ex) {
ex.printStackTrace();
return false;
}
return true;
}
public static void main(String []a)
{
// 使用API
MailKit mkit = createJavaSalonMailKit();
mkit.sendMail(new String[]{"support@163.com"},"u邮件标题","邮件正文",null);
System.out.println("sOK");
}
/** 创建163用户的邮件信息,发件人是 javalalon@163.com */
public static MailKit create163MailKit()
{
String mailHost = "smtp.163.com";
String userAccount = "username@163.com";
String password = "password";
return new MailKit(mailHost,userAccount,password);
}
}