JavaMailメモ
サーブレット参考
http://qiita.com/yoh-nak/items/bff51637fa4f558b37ac
http://qiita.com/rubytomato@github/items/b106ff8011bcad60bce2#%E6%BA%96%E5%82%99
SMTP参考
ダウンロードURL
JavaMail
FakeSMTP
Servlet
SendMail.java
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SendInquiry
*/
@WebServlet("/SendMail")
public class SendMail extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String title = request.getParameter("title");
String message = request.getParameter("message");
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
try{
Properties property = new Properties();
//GmailのSMTPを使う場合
// String host = "smtp.gmail.com";
// String port = "587";
// String starttls = "true";
//一般的なSMTPを使う場合
//ポートが25の場合は省略可能
String host = "localhost";
String port = "25";
String starttls = "false";
property.put("mail.smtp.auth", "true");
property.put("mail.smtp.debug", "true");
property.put("mail.smtp.starttls.enable", starttls);
property.put("mail.smtp.host", host);
property.put("mail.smtp.port", port);
Session session = Session.getInstance(property, new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("Gmailの場合はユーザ名", "Gmailの場合はパスワード");
}
});
MimeMessage mimeMessage = new MimeMessage(session);
InternetAddress toAddress =
new InternetAddress("★送信先アドレス★","★送信先名★");
mimeMessage.setRecipient(Message.RecipientType.TO, toAddress);
InternetAddress fromAddress =
new InternetAddress("★送信元アドレス★","★送信元名★");
mimeMessage.setFrom(fromAddress);
mimeMessage.setSubject(title, "ISO-2022-JP");
mimeMessage.setText(message, "ISO-2022-JP");
Transport.send(mimeMessage);
out.println("<htm><body>");
out.println("■お問い合わせ内容を担当者へ送信しました。");
out.println("<body></html>");
}
catch(Exception e){
out.println("<html><body>");
out.println("■担当者への送信に失敗しました");
out.println("<br>エラーの内容" + e);
out.println("</body></html>");
}
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request,response);
}
}
JSP
mailForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>メール送信</title>
</head>
<body>
<h1>メール送信</h1>
<form action="SendMail" method="post">
タイトル<br>
<input type="text" name="title" size="20"><br>
本文<br>
<textarea name="message" rows="20" cols="40"></textarea><br>
<input type="submit" value="送信"><input type="reset" value="リセット">
</form>
</body>
</html>
SendMail.java
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SendInquiry
*/
@WebServlet("/SendMail")
public class SendMail extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String title = request.getParameter("title");
String message = request.getParameter("message");
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
try{
Properties property = new Properties();
//GmailのSMTPを使う場合
// String host = "smtp.gmail.com";
// String port = "587";
// String starttls = "true";
//一般的なSMTPを使う場合
//ポートが25の場合は省略可能
String host = "localhost";
String port = "25";
String starttls = "false";
property.put("mail.smtp.auth", "true");
property.put("mail.smtp.debug", "true");
property.put("mail.smtp.starttls.enable", starttls);
property.put("mail.smtp.host", host);
property.put("mail.smtp.port", port);
Session session = Session.getInstance(property, new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("Gmailの場合はユーザ名", "Gmailの場合はパスワード");
}
});
MimeMessage mimeMessage = new MimeMessage(session);
InternetAddress toAddress =
new InternetAddress("★送信先アドレス★","★送信先名★");
mimeMessage.setRecipient(Message.RecipientType.TO, toAddress);
InternetAddress fromAddress =
new InternetAddress("★送信元アドレス★","★送信元名★");
mimeMessage.setFrom(fromAddress);
mimeMessage.setSubject(title, "ISO-2022-JP");
mimeMessage.setText(message, "ISO-2022-JP");
Transport.send(mimeMessage);
out.println("<htm><body>");
out.println("■お問い合わせ内容を担当者へ送信しました。");
out.println("<body></html>");
}
catch(Exception e){
out.println("<html><body>");
out.println("■担当者への送信に失敗しました");
out.println("<br>エラーの内容" + e);
out.println("</body></html>");
}
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request,response);
}
}
mailForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>メール送信</title>
</head>
<body>
<h1>メール送信</h1>
<form action="SendMail" method="post">
タイトル<br>
<input type="text" name="title" size="20"><br>
本文<br>
<textarea name="message" rows="20" cols="40"></textarea><br>
<input type="submit" value="送信"><input type="reset" value="リセット">
</form>
</body>
</html>
Author And Source
この問題について(JavaMailメモ), 我々は、より多くの情報をここで見つけました https://qiita.com/aria168/items/a4f481f3b6c5c371b21a著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .