package com.onlinebookstore.util.mail;import java.io.UnsupportedEncodingException;import java.util.Properties; import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;public class MailSenderInfo { // 發送郵件的伺服器的IP和連接埠 private String mailServerHost; private String mailServerPort = "25"; // 郵件寄件者的地址 private String fromAddress; // 郵件接收者的地址 private String toAddress; // 登陸郵件發送伺服器的使用者名稱和密碼 private String userName; private String password; // 是否需要身分識別驗證 private boolean validate = false; // 郵件主題 private String subject; // 郵件的常值內容 private String content; // 郵件附件的檔案名稱 private String[] attachFileNames; /** *//** * 獲得郵件會話屬性 */ public Properties getProperties(){ Properties p = new Properties(); p.put("mail.smtp.host", this.mailServerHost); p.put("mail.smtp.port", this.mailServerPort); p.put("mail.smtp.auth", validate ? "true" : "false"); return p; } public String getMailServerHost() { return mailServerHost; } public void setMailServerHost(String mailServerHost) { this.mailServerHost = mailServerHost; } public String getMailServerPort() { return mailServerPort; } public void setMailServerPort(String mailServerPort) { this.mailServerPort = mailServerPort; } public boolean isValidate() { return validate; } public void setValidate(boolean validate) { this.validate = validate; } public String[] getAttachFileNames() { return attachFileNames; } public void setAttachFileNames(String[] fileNames) { this.attachFileNames = fileNames; } public String getFromAddress() { return fromAddress; } public void setFromAddress(String fromAddress) { this.fromAddress = fromAddress; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getToAddress() { return toAddress; } public void setToAddress(String toAddress) { this.toAddress = toAddress; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String textContent) { this.content = textContent; }public void sendEmail(String toMailAdr,String transport,String mailSubject,String htmlContent) {try {Properties props = new Properties();props.put("mail.smtp.host", "smtp.qq.com");props.put("mail.smtp.auth", "true");//授權this.content=htmlContent;Session ses = Session.getDefaultInstance(props, new MyAuthenticator(fromAddress,password));//構建與mail.host 的會話InternetAddress from = new InternetAddress(fromAddress);//寄件者郵件地址InternetAddress to = new InternetAddress(toMailAdr);//接收者郵件地址MimeMessage msg = new MimeMessage(ses);//建立資訊msg.setFrom(from);//資訊由哪個地址發送msg.addRecipient(Message.RecipientType.TO, to);//資訊發送到哪個地址msg.setSubject("html mail");//資訊主題String content = createHtml(getContent());//資訊內容msg.setContent(content,"text/html;charset=GBK");Transport tran = ses.getTransport(transport);//擷取傳輸類的執行個體System.out.println("mailPassword: "+password);tran.connect(fromAddress,password); //串連到寄件者的郵箱Transport.send(msg);tran.close();} catch (MessagingException e) {e.printStackTrace();}}public String createHtml(String body){StringBuffer sb = new StringBuffer();sb.append("<html>");sb.append("<body>");sb.append(body);sb.append("</body>");sb.append("</html>");return sb.toString();}}