練功房推薦書單

  • 猛虎出柙雙劍合璧版--最新 OCA / OCP Java SE 7 Programmer 專業認證 (電子書)
  • 流浪教師存零股存到3000萬(全新增修版)(書+DVD)
  • 開始在關西自助旅行(京都‧大阪‧神戶‧奈良)(全新增訂版)
  • 不敗教主的300張股票存股術
JSP精選實用範例(一):郵件發送 RSS feed
Forum Index » 網頁程式設計 Web Development
Author Message
andowson

七段學員
[Avatar]

Joined: 2007/1/2
Messages: 706
Location: 台北
Offline
預先安裝函式庫:Apache Commons Email, Sun JavaMail API, JavaBeans Activation Framework (JAF)
程式碼:
<%@ page contentType="text/html; charset=big5"%>
<%@ page import="org.apache.commons.mail.SimpleEmail"%>
<%			
    String subject="歡迎光臨";
    String message = "這是一封測試信,收到請自行刪除";
    SimpleEmail simpleEmail = new SimpleEmail();
    simpleEmail.setHostName("smtp.mycompany.com");
    simpleEmail.setCharset("Big5");
    simpleEmail.addTo("you@yourcompany.com", "親愛的會員");				
    simpleEmail.setFrom("service@mycompany.com", "網站客服中心");
    simpleEmail.setSubject(subject);
    simpleEmail.setMsg(message);
    simpleEmail.send();
    out.println("郵件發送成功");
%>
 Filename mailtest.jsp [Disk] Download
 Description 郵件發送測試
 Filesize 608 bytes
 Downloaded:  125 time(s)


分享經驗 累積智慧
[WWW] [MSN]
andowson

七段學員
[Avatar]

Joined: 2007/1/2
Messages: 706
Location: 台北
Offline
使用 Gmail 的 SMTP 發信(通過SSL port: 465)
程式碼:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="org.apache.commons.mail.DefaultAuthenticator" %>
<%@ page import="org.apache.commons.mail.Email" %>
<%@ page import="org.apache.commons.mail.EmailException" %>
<%@ page import="org.apache.commons.mail.HtmlEmail" %>
<%
    String subject="測試使用 Gmail SMTP SSL發信";
    String message = "<html><head><title>測試</title></head><body>這是一封測試信,收到請自行刪除</body></html>"; 

    Email email = new HtmlEmail(); 
	String authuser = "username@gmail.com"; 
	String authpwd = "the_password";
	email.setHostName("smtp.gmail.com");
	email.setSmtpPort(465); 
	email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
	email.setDebug(true);
	email.setSSL(true);
	email.setSslSmtpPort("465");
	email.setCharset("UTF-8");
	email.setSubject(subject);
	try {
	    email.setFrom("username@gmail.com", "網站客服中心");
	    email.setMsg(message); 
	    email.addTo("somebody@30elite.com", "親愛的會員");
	    email.send();
	    out.println("郵件發送成功"); 
	} catch (EmailException e) {
	    e.printStackTrace();
	}	
%>


參考資料:
http://stackoverflow.com/questions/1783710/sending-an-email-using-commons-email-to-gmail
 Filename gmailsmtp_ssl.jsp [Disk] Download
 Description Send an email via Gmail SMTP through SSL port(465)
 Filesize 1 Kbytes
 Downloaded:  103 time(s)


分享經驗 累積智慧
[WWW] [MSN]
andowson

七段學員
[Avatar]

Joined: 2007/1/2
Messages: 706
Location: 台北
Offline
使用 Gmail 的 SMTP 發信(通過TLS port: 587)
程式碼:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="org.apache.commons.mail.DefaultAuthenticator" %>
<%@ page import="org.apache.commons.mail.Email" %>
<%@ page import="org.apache.commons.mail.EmailException" %>
<%@ page import="org.apache.commons.mail.HtmlEmail" %>
<%
    String subject="測試使用 Gmail SMTP TLS發信";
    String message = "<html><head><title>測試</title></head><body>這是一封測試信,收到請自行刪除</body></html>"; 

    Email email = new HtmlEmail(); 
	String authuser = "username@gmail.com"; 
	String authpwd = "the_password";
	email.setHostName("smtp.gmail.com");
	email.setSmtpPort(587); 
	email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
	email.setDebug(true);
	email.setTLS(true);
	email.setSslSmtpPort("587");
	email.setCharset("UTF-8");
	email.setSubject(subject);
	try {
	    email.setFrom("username@gmail.com", "網站客服中心");
	    email.setMsg(message); 
	    email.addTo("somebody@30elite.com", "親愛的會員");
	    email.send();
	    out.println("郵件發送成功"); 
	} catch (EmailException e) {
	    e.printStackTrace();
	}	
%>
 Filename gmailsmtp_tls.jsp [Disk] Download
 Description Send an email via Gmail SMTP through TLS port(587)
 Filesize 1 Kbytes
 Downloaded:  45 time(s)


分享經驗 累積智慧
[WWW] [MSN]
andowson

七段學員
[Avatar]

Joined: 2007/1/2
Messages: 706
Location: 台北
Offline
發送HTML格式的郵件
預先安裝函式庫:Apache Commons IO
假設樣板檔名為message.html,放在網頁應用程式的根目錄下,發送給somebody@andowson.com。
程式碼:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.io.File" %>
<%@ page import="java.text.MessageFormat" %>
<%@ page import="org.apache.commons.io.FileUtils" %>
<%@ page import="org.apache.commons.mail.DefaultAuthenticator" %>
<%@ page import="org.apache.commons.mail.EmailException" %>
<%@ page import="org.apache.commons.mail.HtmlEmail" %>
<%    
    String subject="測試使用 Gmail SMTP SSL發信";
    String filename = application.getRealPath("/") + "message.html";
    String template = FileUtils.readFileToString(new File(filename), "UTF-8");
    String message = "";

    HtmlEmail email = new HtmlEmail(); 
	String authuser = "username@gmail.com"; 
	String authpwd = "the_password";
	email.setHostName("smtp.gmail.com");
	email.setSmtpPort(465); 
	email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
	email.setDebug(true);
	email.setSSL(true);
	email.setSslSmtpPort("465");
	email.setCharset("UTF-8");
	email.setSubject(subject);
	try {
	    email.setFrom("username@gmail.com", "網站客服中心");
	    message = MessageFormat.format(template, new Object[] { "somebody", "http://www.andowson.com/" });
	    email.setHtmlMsg(message); 
	    email.addTo("somebody@andowson.com", "親愛的會員");
	    email.send();
	    out.println("郵件發送成功"); 
	} catch (EmailException e) {
	    e.printStackTrace();
	}	
%>

message.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>e-paper</title>
</head>
<body>
親愛的{0}您好,請點選這個<a href="{1}">連結</a>至活動網頁。<br/>
這是一封測試信,收到請自行刪除
</body>
</html>

 Filename gmailsmtp_ssl_htmlmsg.jsp [Disk] Download
 Description Send HTML Message
 Filesize 1 Kbytes
 Downloaded:  34 time(s)

 Filename message.html [Disk] Download
 Description HTML Message Template File
 Filesize 378 bytes
 Downloaded:  36 time(s)


分享經驗 累積智慧
[WWW] [MSN]
qq4111

十級學員

Joined: 2011/12/17
Messages: 1
Offline
請問,若是要多個寄件者的話,應該怎麼做呢?
我是一直出現這錯誤Caused by: javax.mail.internet.AddressException: Illegal address in string
andowson

七段學員
[Avatar]

Joined: 2007/1/2
Messages: 706
Location: 台北
Offline
寄件者只能有一個,可以透過Email類別的setFrom的method來設定,可接受三種參數呼叫方式:
setFrom(String email)
setFrom(String email, String name)
setFrom(String email, String name, String charset)

如果要收件者,可以透過Email類別的addTo的method來增加,類似setFrom有三種參數呼叫方式:
addTo(String email)
addTo(String email, String name)
addTo(String email, String name, String charset)

收件者可以有多個,只要連續呼叫多次的addTo即可(例如透過迴圈)或者先將收件者整理成一個清單(List),再呼叫setTo一次設定好
setTo(Collection aCollection)


至於您的錯誤訊息,可能是charset編碼問題,也可能是其他問題,得看到程式碼才能得知。



分享經驗 累積智慧
[WWW] [MSN]
 
Forum Index » 網頁程式設計 Web Development
Go to:   
Mobile view