發送郵件html
package main;java import java.util.Date;瀏覽器 import java.util.Properties;安全 import javax.mail.Message;session import javax.mail.Multipart;app import javax.mail.Session;測試 import javax.mail.Transport;ui import javax.mail.internet.InternetAddress;this import javax.mail.internet.MimeBodyPart;spa import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart;
public class MailOperation { /** * @author goming *TODO:發送郵件 *@param user *@param password *@param host *@param from *@param to *@param subject *@param content *@return *@throws Exception */ public String sendMail(String user,String password,String host,String from,String to,String subject,String content) throws Exception { // TODO Auto-generated method stub if(to!=null) { Properties props=System.getProperties(); props.put("mail.smtp.host", host); props.put("mail.smtp.auth", "true");
MailAuthenticator auth=new MailAuthenticator(); MailAuthenticator.USERNAME=user; MailAuthenticator.PASSWORD=password; Session session=Session.getInstance(props,auth); session.setDebug(true); try{ MimeMessage message=new MimeMessage(session); //message.setDataHandler(new DataHandler(content, "text/html; charset=utf-8"));//設置郵件內容 message.setFrom(new InternetAddress(from)); if(!to.trim().equals("")) message.addRecipient(Message.RecipientType.TO, new InternetAddress(to.trim())); message.setSubject(subject); // message.setContent(mp) //message.setText("this is 一個測試郵件"); MimeBodyPart mbp1=new MimeBodyPart(); //正文 mbp1.setContent(content,"text/html;charset=utf-8");
Multipart mp=new MimeMultipart(); //整個郵件:正文+附件 mp.addBodyPart(mbp1); //mp.addBodyPart(mbp2); message.setContent(mp); message.setSentDate(new Date()); message.saveChanges();
Transport trans=session.getTransport("smtp"); trans.send(message); System.out.println(message.toString()); }catch(Exception e) { e.printStackTrace(); return "failure"; } return "success"; } else return "failure";
}
public static void main(String[] args) { MailOperation operation = new MailOperation(); String user = "admin@xiamengoldenway.com"; String password = "xmG0ldenway"; String host = "smtp.qq.com"; String from = "admin@qq.com"; String to = "******@qq.com";//收件人 String subject = "Test"; String content = "測試郵件";
try { String res = operation.sendMail(user, password, host, from, to, subject, content); System.out.println(res); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(new Date()); }
} |
import javax.mail.Authenticator; import javax.mail.PasswordAuthentication;
/** * * @author goming * */ public class MailAuthenticator extends Authenticator
{ public static String USERNAME = "";
public static String PASSWORD = "";
public MailAuthenticator(){} protected PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(USERNAME, PASSWORD); } } |
/** * 發送郵件 * @author goming * @param to * @param uuid */ @RequestMapping(params = "sendMail") @ResponseBody private void sendMail(String to,HttpServletRequest request){ MailOperation operation = new MailOperation(); //發件人 String user = "*****@qq.com"; String password = "******"; String host = "smtp.qq.com"; String from = "1915960810@qq.com"; String subject = "感謝您註冊創意生態資源集成系統軟件"; //郵箱內容 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/"; StringBuffer sb = new StringBuffer(); Date date=new Date(); Long longtimeLong=date.getTime(); sb.append("<!DOCTYPE>"+"<div bgcolor='#f1fcfa' style='border:1px solid #d9f4ee; font-size:12px; line-height:22px; color:#005aa0;padding-left:1px;padding-top:5px; padding-bottom:5px;'><span style='font-weight:bold;'>安全提示:</span>" + "<div style='width:950px;font-family:arial;'>爲了您的帳戶安全,咱們建議您點擊如下連接驗證郵箱:<br/><a href='"+basePath+"UserRegister.do?ValidateEmail&time="+longtimeLong+"&token="+to+"'>"+basePath+"UserRegister.do?ValidateEmail&time="+longtimeLong+"&token="+to+"</a><br/>請在24小時內點擊該連接,您也能夠將連接複製到瀏覽器地址欄訪問。<br/>本郵件由系統自動發出,請勿回覆。<br/>感謝您的使用。<br/>廈門交叉媒體科技有限公司</div>" +"</div>"); try { String res = operation.sendMail(user, password, host, from, to, subject, sb.toString()); // System.out.println(res); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
|
如今把demo 鏈接http://pan.baidu.com/s/1jGLzfvg
只須在MailOperation.java修改發件人,收件人,就能夠直接運行測試!