41
42

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaMailを使ってGmailのSMTPでメールを送信するとき

Last updated at Posted at 2014-07-12

JavaMailのダウンロード

JavaMailはJAF(JavaBeans Activation Framework)に依存しているので
javamail1_4_7.zipとjaf-1_1_1.zipを以下のアドレスからダウンロード

2つともこのページからダウンロードできる

zipファイルを解凍して得られるmail.jarとactivation.jarをWebContent\WEB-INF\libにコピー

以下省略...

SendMail.java
package com.sample.simpleinquiry;

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#HttpServlet()
     */
    public SendInquiry() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		request.setCharacterEncoding("UTF-8");
		
		String title = request.getParameter("title");
		String message = request.getParameter("message");
		
		//System.out.println("タイトル:" + title);
		//System.out.println("メッセージ" + message);
		
		response.setContentType("text/html; charset=UTF-8");
		
		PrintWriter out = response.getWriter();
		
		try{
			Properties property = new Properties();
			
			property.put("mail.smtp.host","smtp.gmail.com");
			
			//GmailのSMTPを使う場合
			property.put("mail.smtp.auth", "true");
			property.put("mail.smtp.starttls.enable", "true");
			property.put("mail.smtp.host", "smtp.gmail.com");
			property.put("mail.smtp.port", "587");
			property.put("mail.smtp.debug", "true");
			
	        Session session = Session.getInstance(property, new javax.mail.Authenticator(){
	            protected PasswordAuthentication getPasswordAuthentication(){
	                return new PasswordAuthentication("Gmailのユーザ名", "Gmailのパスワード");
	            }
	        });
	        
	        /*
	        //一般的なSMTPを使う場合
	        
			//ポートが25の場合は省略可能
			property.put("mail.smtp.port", 25);
			
			Session session = 
					Session.getDefaultInstance(property, null);
			*/
			
			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();
	}

}
41
42
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
41
42

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?