代码由纯Java写成,支持中文,一目了然,只要将Main函数中的相关信息填写正确就直接用了,便于修改,可以在此类基础上任意扩展成自己的类。
注意做HTML形式的邮件,最好把HTML,CSS都写全,只写一部分,不全,效果就不好看了。
邮件效果如图:
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
/**
* 邮件发送测试类
*
* 2013年9月25日18:12:54
*/
public class TestMailService{
protected String smtpServer;
protected String smtpUsername;
protected String smtpPassword;
protected String fromMailAddress;
protected String toMailAddress;
protected String ccMailAddress;
protected String bccMailAddress;
protected boolean testMode;
public boolean isTestMode() {
return testMode;
}
public void setTestMode(boolean testMode) {
this.testMode = testMode;
}
/**
* 无参构造函数
*/
public TestMailService(){
}
/**
* 发送邮件的关键函数
*
* @param title
* @param content
* @return
* @throws Exception
*/
protected boolean sendMail(String title,String content) throws Exception{
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", smtpServer);
// 与邮件服务器开始会话
Session session = Session.getDefaultInstance(props,new SmtpAuthenticator(smtpUsername, smtpPassword));
/** *************************************************** */
// 创建MIME邮件对象
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(fromMailAddress));// 发件人
// To收件人
mimeMessage.setRecipients(Message.RecipientType.TO, getInternetAddressArr(toMailAddress));
// Cc收件人
InternetAddress[] arr=getInternetAddressArr(ccMailAddress);
if(arr!=null && arr.length>0){
mimeMessage.setRecipients(Message.RecipientType.CC, getInternetAddressArr(ccMailAddress));
}
// Bcc收件人
arr=getInternetAddressArr(bccMailAddress);
if(arr!=null && arr.length>0){
mimeMessage.setRecipients(Message.RecipientType.BCC, getInternetAddressArr(bccMailAddress));
}
mimeMessage.setSubject(MimeUtility.encodeText(title,"gb2312","B"));
mimeMessage.setSentDate(new Date());// 发送日期
Multipart mp = new MimeMultipart("related");// related意味着可以发送html格式的邮件
/** *************************************************** */
BodyPart bodyPart = new MimeBodyPart();// 正文
bodyPart.setDataHandler(new DataHandler(content,"text/html;charset=gb2312"));// 网页格式
mp.addBodyPart(bodyPart);
mimeMessage.setContent(mp);// 设置邮件内容对象
Transport.send(mimeMessage);// 发送邮件
return true;
}
/**
* Mail地址转化
* @param mialAddr
* @return
* @throws Exception
*/
protected InternetAddress[] getInternetAddressArr(String mialAddr) throws Exception{
if(mialAddr==null){
return null;
}
String[] arr=mialAddr.split(",");
InternetAddress[] retval=new InternetAddress[arr.length];
for(int i=0;i<arr.length;i++){
retval[i]=new InternetAddress(arr[i]);
}
return retval;
}
/**
* 保留两位小数
* @param s
* @return
*/
protected String getFormatted(String s){
float f=Float.parseFloat(s);
java.text.DecimalFormat df =new java.text.DecimalFormat("#0.00");
return df.format(f);
}
public String getSmtpServer() {
return smtpServer;
}
public void setSmtpServer(String smtpServer) {
this.smtpServer = smtpServer;
}
public String getSmtpUsername() {
return smtpUsername;
}
public void setSmtpUsername(String smtpUsername) {
this.smtpUsername = smtpUsername;
}
public String getSmtpPassword() {
return smtpPassword;
}
public void setSmtpPassword(String smtpPassword) {
this.smtpPassword = smtpPassword;
}
public String getFromMailAddress() {
return fromMailAddress;
}
public void setFromMailAddress(String fromMailAddress) {
this.fromMailAddress = fromMailAddress;
}
public String getToMailAddress() {
return toMailAddress;
}
public void setToMailAddress(String toMailAddress) {
this.toMailAddress = toMailAddress;
}
public String getCcMailAddress() {
return ccMailAddress;
}
public void setCcMailAddress(String ccMailAddress) {
this.ccMailAddress = ccMailAddress;
}
public String getBccMailAddress() {
return bccMailAddress;
}
public void setBccMailAddress(String bccMailAddress) {
this.bccMailAddress = bccMailAddress;
}
public String getHTML(){
StringBuilder sb=new StringBuilder();
sb.append(" <html>");
sb.append(" <head>");
sb.append(" <title> New Document </title>");
sb.append(" </head>");
sb.append(" ");
sb.append(" <style type="text/css">");
sb.append(" table { ");
sb.append(" margin: 10px 0 30px 0;");
sb.append(" }");
sb.append(" ");
sb.append(" table caption { ");
sb.append(" text-align:left;");
sb.append(" }");
sb.append(" ");
sb.append(" table tr th { ");
sb.append(" background: #3B3B3B;");
sb.append(" color: #FFF;");
sb.append(" padding: 7px 4px;");
sb.append(" text-align: left;");
sb.append(" }");
sb.append(" ");
sb.append(" table tr td { ");
sb.append(" color: #FFF;");
sb.append(" padding: 7px 4px;");
sb.append(" text-align: left;");
sb.append(" }");
sb.append(" ");
sb.append(" table tr.odd{");
sb.append(" background-color:#cef;");
sb.append(" }");
sb.append(" ");
sb.append(" table tr.even{");
sb.append(" background-color:#ffc;");
sb.append(" }");
sb.append(" ");
sb.append(" table tr td { ");
sb.append(" color: #47433F;");
sb.append(" border-top: 1px solid #FFF;");
sb.append(" }");
sb.append(" </style>");
sb.append(" ");
sb.append(" <body>");
sb.append(" <table style="300px; border-spacing:0;"> ");
sb.append(" <tr> ");
sb.append(" <th>A</th> ");
sb.append(" <th>B</th> ");
sb.append(" <th>C</th> ");
sb.append(" <th>D</th> ");
sb.append(" <th>E</th> ");
sb.append(" </tr> ");
sb.append(" <tr class="odd"> ");
sb.append(" <td>1</td> ");
sb.append(" <td>2</td> ");
sb.append(" <td>3</td> ");
sb.append(" <td>4</td> ");
sb.append(" <td>5</td> ");
sb.append(" </tr> ");
sb.append(" <tr class="even"> ");
sb.append(" <td>11</td> ");
sb.append(" <td>22</td> ");
sb.append(" <td>33</td> ");
sb.append(" <td>44</td> ");
sb.append(" <td>55</td> ");
sb.append(" </tr> ");
sb.append(" <tr class="odd"> ");
sb.append(" <td>111</td> ");
sb.append(" <td>222</td> ");
sb.append(" <td>333</td> ");
sb.append(" <td>444</td> ");
sb.append(" <td>555</td> ");
sb.append(" </tr> ");
sb.append(" <tr class="even"> ");
sb.append(" <td>你好</td> ");
sb.append(" <td>2222</td> ");
sb.append(" <td>3333</td> ");
sb.append(" <td>4444</td> ");
sb.append(" <td>5555</td> ");
sb.append(" </tr> ");
sb.append(" </table> ");
sb.append(" </body>");
sb.append(" </html>");
return sb.toString();
}
public static void main(String[] args) throws Exception{
TestMailService s=new TestMailService();
s.setSmtpServer("***.smtp.173.com");
s.setSmtpUsername("***");
s.setSmtpPassword("*****");
s.setFromMailAddress("a@b.com");
s.setToMailAddress("c@d.com");
s.sendMail("测试邮件", s.getHTML());
}
}
要是感觉此类有问题,请留言。
《End》