package mail;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**Authenticator接口实现类 :send Mail 的 session 中用到 */
public class AuthenticatorImpl extends Authenticator {
private String name;
private String password;
public AuthenticatorImpl() {
super();
}
public AuthenticatorImpl(String name, String password) {
super();
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(name, password);
}
}
/********/
package mail;
import java.io.Serializable;
import java.util.ArrayList;
public class Mail implements Serializable{
/** 发件人地址 */
private String sendFrom;
/** 收件人地址 */
private String sendTo;
/** 抄送地址 */
private String sendCC;
/** 邮件服务器地址 */
private String hostSMTP;
/** 用户名 */
private String userName;
/** 密码 */
private String password;
/** 邮件主题 */
private String mailSubject;
/** 邮件正文内容 */
private String mailContent;
/** 附件集合 : 最多为5个 */
private ArrayList<String> attfile = new ArrayList<String>();
public String getSendFrom(){return sendFrom;}
public void setSendFrom(String sendFrom){this.sendFrom = sendFrom;}
public String getSendTo(){return sendTo;}
public void setSendTo(String sendTo){this.sendTo = sendTo;}
public String getSendCC(){return sendCC;}
public void setSendCC(String sendCC){this.sendCC = sendCC;}
public String getHostSMTP(){return hostSMTP;}
public void setHostSMTP(String hostSMTP){this.hostSMTP = hostSMTP;}
public String getUserName(){return userName;}
public void setUserName(String userName){this.userName = userName;}
public String getPassword(){return password;}
public void setPassword(String password){this.password = password;}
public String getMailSubject(){return mailSubject;}
public void setMailSubject(String mailSubject){this.mailSubject = mailSubject;}
public String getMailContent(){return mailContent;}
public void setMailContent(String mailContent){this.mailContent = mailContent;}
public ArrayList<String> getAttfile(){return attfile;}
public void setAttfile(ArrayList<String> attfile){this.attfile = attfile;}
}
/************/
package mail;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import mail.MailDao;
import mail.Mail;
/**邮件接口实现类 */
public class MailBean implements MailDao {
private static MailBean mjd;
public MailBean() {
super();
}
/** 单例模式 */
public static MailBean getInstance() {
if (mjd == null)
mjd = new MailBean();
return mjd;
}
/**发送邮件具体方法 */
public Boolean send(Mail mail) {
/** 创建session中所需要的Properties对象 */
Properties props = System.getProperties();
props.put("mail.smtp.host", mail.getHostSMTP());
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.timeout", 2500);
/** 创建邮件会话对象session */
AuthenticatorImpl authenticatorImpl = new AuthenticatorImpl();
authenticatorImpl.setName(mail.getUserName());
authenticatorImpl.setPassword(mail.getPassword());
Session mailSession = Session.getInstance(props,authenticatorImpl);
/** 构造MimeMessage<实际发送的电子邮件信息>,并设定基本的值 */
MimeMessage message = new MimeMessage(mailSession);
try {
/** 设置发件人 */
message.setFrom(new InternetAddress(mail.getSendFrom()));
/** 设置收件人 */
message.setRecipients(Message.RecipientType.TO, mail.getSendTo());
/** 设置抄送人 */
//message.setRecipients(Message.RecipientType.CC, mail.getSendCC());
/** 设置邮件主题 : 编码 */
message.setSubject(new String(mail.getMailSubject().getBytes("ISO-8859-1")), "GBK");
/** 构造Multipart : 该类是用于保存电子邮件内容的容器 :定义了添加、删除、获得电子邮件内容的方法 */
Multipart mp = new MimeMultipart();
/** 设置邮件的正文信息,并添加正文 : 编码 */
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(new String(mail.getMailContent().getBytes("ISO-8859-1")),"GBK");
mp.addBodyPart(mbp);
/** 将邮件内容添加到MimeMessage */
message.setContent(mp);
/** 设置邮件的发送日期 */
message.setSentDate(new Date());
/** 发送电子邮件 */
Transport.send(message);
return true;
} catch (AddressException e) {
System.err
.println("MailJdbcDao : send(MailBean mail) : AddressException : "
+ e.getMessage());
} catch (MessagingException e) {
System.err
.println("MailJdbcDao : send(MailBean mail) : MessagingException : "
+ e.getMessage());
} catch (UnsupportedEncodingException e) {
System.err
.println("MailJdbcDao : send(MailBean mail) : UnsupportedEncodingException : "
+ e.getMessage());
}
return false;
}
}
/***************/
package mail;
import mail.Mail;
public interface MailDao {
Boolean send(Mail mail);
}
/**************/
package mail;
import mail.Mail;
import mail.MailDao;
import mail.MailBean;
/** 邮件接口业务逻辑实现类 */
public class MailService {
private static MailService mailService;
public static MailDao md = new MailBean();
/** 单例模式 */
public static MailService getInstance() {
if (mailService == null)
mailService = new MailService();
return mailService;
}
private MailService() {
super();
}
/**发送邮件业务逻辑方法 */
public Boolean send() {
Mail mail=new Mail();
mail.setUserName("");
mail.setPassword("");
mail.setHostSMTP("");
mail.setSendFrom("");
mail.setSendTo("");
mail.setMailSubject("hello test ");
mail.setMailContent("this is a ..");
return md.send(mail);
}
public static void main(String[] args){
Boolean f=MailService.getInstance().send();
if(f) System.out.println("sucess");
else System.out.println("fail");
}
}
/***************************/
package mail;
import java.io.UnsupportedEncodingException;
public class Util {
// 在页面显示时用from8859()
public static String from8859(String source) {
try {
return new String(source.getBytes("iso-8859-1"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
// 往数据写入时用to8859()
public static String to8859(String source) {
try {
return new String(source.getBytes(), "iso-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}