packagecom;importjava.util.Properties;importjavax.mail.Address;importjavax.mail.BodyPart;importjavax.mail.Message;importjavax.mail.Multipart;importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeBodyPart;importjavax.mail.internet.MimeMessage;importjavax.mail.internet.MimeMultipart;/*** 发送邮件
**/
public classMail {privateMimeMessage mimeMsg;privateSession session;privateProperties props;privateString username;privateString password;privateMultipart mp;publicMail(String smtp){
setSmtpHost(smtp);
createMimeMessage();
}public voidsetSmtpHost(String hostName){
System.out.println("设置系统属性:mail.smtp.host="+hostName);if(props==null){
props=System.getProperties();
}
props.put("mail.smtp.host", hostName);
}public booleancreateMimeMessage(){try{
System.out.println("准备获取邮件会话对象!");
session= Session.getDefaultInstance(props,null);
}catch(Exception e){
System.out.println("获取邮件会话对象时发生错误!"+e);return false;
}
System.out.println("准备创建MIME邮件对象!");try{
mimeMsg= newMimeMessage(session);
mp= newMimeMultipart();return true;
}catch(Exception e){
System.out.println("创建MIME邮件对象失败!"+e);return false;
}
}/*** 设置SMTP是否需要验证*/
public void setNeedAuth(booleanneed) {
System.out.println("设置smtp身份认证:mail.smtp.auth = "+need);if(props == null) props =System.getProperties();if(need){
props.put("mail.smtp.auth","true");
}else{
props.put("mail.smtp.auth","false");
}
}public voidsetNamePass(String name,String pass) {
username=name;
password=pass;
}/*** 设置邮件主题*/
public booleansetSubject(String mailSubject) {
System.out.println("设置邮件主题!");try{
mimeMsg.setSubject(mailSubject);return true;
}catch(Exception e) {
System.err.println("设置邮件主题发生错误!");return false;
}
}/*** 设置邮件正文*/
public booleansetBody(String mailBody) {try{
BodyPart bp= newMimeBodyPart();
bp.setContent(""+mailBody,"text/html;charset=GBK");
mp.addBodyPart(bp);return true;
}catch(Exception e){
System.err.println("设置邮件正文时发生错误!"+e);return false;
}
}/*** 设置发信人*/
public booleansetFrom(String from) {
System.out.println("设置发信人!");try{
mimeMsg.setFrom(new InternetAddress(from)); //设置发信人
return true;
}catch(Exception e) {return false;
}
}/*** 设置收信人*/
public booleansetTo(String to){if(to == null)return false;
System.out.println("设置收信人!");try{
mimeMsg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));return true;
}catch(Exception e) {return false;
}
}/*** 设置抄送人*/
public booleansetCopyTo(String copyto)
{if(copyto == null)return false;try{
mimeMsg.setRecipients(Message.RecipientType.CC,(Address[])InternetAddress.parse(copyto));return true;
}catch(Exception e)
{return false; }
}/*** 发送邮件*/
public booleansendOut()
{try{
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
System.out.println("正在发送邮件....");
Session mailSession= Session.getInstance(props,null);
Transport transport= mailSession.getTransport("smtp");
transport.connect((String)props.get("mail.smtp.host"),username,password);
transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO));
System.out.println("发送邮件成功!");
transport.close();return true;
}catch(Exception e) {
System.err.println("邮件发送失败!"+e);return false;
}
}/*** 调用sendOut方法完成邮件发送*/
public static booleansendAndCc(String smtp,String from,String to,String copyto,String subject,String content,String username,String password) {
Mail theMail= newMail(smtp);
theMail.setNeedAuth(true); //需要验证
if(!theMail.setSubject(subject)) return false;if(!theMail.setBody(content)) return false;if(!theMail.setTo(to)) return false;if(!theMail.setCopyTo(copyto)) return false;if(!theMail.setFrom(from)) return false;
theMail.setNamePass(username,password);if(!theMail.sendOut()) return false;return true;
}
}