此範例單純寄信,不夾帶檔案
另外可能是google本身有GAE(google app engine)服務
Gmail不適合當作商業用的mail server
除了安全認證外,本身也有寄信上限100封 / per day
在Server上也可能有認證交換阻擋之類的限制
我在自己的電腦上使用沒問題,但也曾經在Linux的Server上碰到認證的Exception
如果要找商業使用的mail server最好還是另尋他途(例如:sendGrid)
Gmail 本身走TLS 465 port,使用時要特別注意防火牆是否有打開這個port
另外程式碼的部分也需要設定使用SSL
以下為程式碼範例,實際使用時可以使用java的property設定存取設定值
本例為簡單示範,設定值直接寫在裡面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.util.Properties" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.DataHandler" %>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Send Mail</title>
</head>
<body>
<%
//mail content
String recipients = "mail address";
String subject = "your subject";
String content = "your mail content";
//get properties and se
final String userName = "your mail user name";
final String password = "your mail password";
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.from", "test@gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.sendpartial",true);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.port", "465");
Session se = Session.getInstance(props, new Authenticator() { //use javax.mail.Session
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
try {
// Define message
MimeMessage message = new MimeMessage(se);
try{
message.setReplyTo(InternetAddress.parse(recipients));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients, false));
} catch(Exception e){
System.out.println("## Bad address:"+InternetAddress.parse(recipients, false));
return;
}
message.setSubject(subject);
Multipart multipart = new MimeMultipart();
// create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(content, "text/html; charset=utf-8");
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
// Send message
Transport.send(message);
} catch (MessagingException e) {
if (e.getMessage().equals("No recipient addresses")) {
} else {
throw new RuntimeException(e);
}
}
%>
</body>
</html>
另外可能是google本身有GAE(google app engine)服務
Gmail不適合當作商業用的mail server
除了安全認證外,本身也有寄信上限100封 / per day
在Server上也可能有認證交換阻擋之類的限制
我在自己的電腦上使用沒問題,但也曾經在Linux的Server上碰到認證的Exception
如果要找商業使用的mail server最好還是另尋他途(例如:sendGrid)
Gmail 本身走TLS 465 port,使用時要特別注意防火牆是否有打開這個port
另外程式碼的部分也需要設定使用SSL
以下為程式碼範例,實際使用時可以使用java的property設定存取設定值
本例為簡單示範,設定值直接寫在裡面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.util.Properties" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.DataHandler" %>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Send Mail</title>
</head>
<body>
<%
//mail content
String recipients = "mail address";
String subject = "your subject";
String content = "your mail content";
//get properties and se
final String userName = "your mail user name";
final String password = "your mail password";
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.from", "test@gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.sendpartial",true);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.port", "465");
Session se = Session.getInstance(props, new Authenticator() { //use javax.mail.Session
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
try {
// Define message
MimeMessage message = new MimeMessage(se);
try{
message.setReplyTo(InternetAddress.parse(recipients));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients, false));
} catch(Exception e){
System.out.println("## Bad address:"+InternetAddress.parse(recipients, false));
return;
}
message.setSubject(subject);
Multipart multipart = new MimeMultipart();
// create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(content, "text/html; charset=utf-8");
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
// Send message
Transport.send(message);
} catch (MessagingException e) {
if (e.getMessage().equals("No recipient addresses")) {
} else {
throw new RuntimeException(e);
}
}
%>
</body>
</html>
留言
張貼留言