JavaMail

  簡単なメール送信



  
<HTML>
<%@ page
	language="java"
	contentType="text/html;charset=shift_jis"
	import="java.util.*"
	import="javax.mail.*"
	import="javax.mail.internet.*" 
%>
<jsp:useBean id="Basic" scope="session" class="lightbox.basic" />
<%!
// *********************************************************
// 認証用のプライベートクラス
// *********************************************************
private class SimpleAuthenticator extends Authenticator {

	private String user_string = null;
	private String pass_string = null;

	public SimpleAuthenticator( String user_s, String pass_s ) {
		super();
		user_string = user_s;
		pass_string = pass_s;
	}

	protected PasswordAuthentication getPasswordAuthentication(){
		return new PasswordAuthentication( this.user_string, this.pass_string );
	}
}
%>
<BODY>
<H2>メール送信</H2>
<FORM method=post>
	<INPUT type=text name=from><br>
	<INPUT type=text name=to><br>
	<INPUT type=text name=subject><br>
	<TEXTAREA name=comment cols=40 rows=10></TEXTAREA><br>
	<INPUT name=SendButton type=submit value=送信>
</FORM>

<%
	// *****************************************************
	// * フォームのデータを取得して連想配列に保存
	// *****************************************************
	Map requestMap = request.getParameterMap();
	int size = requestMap.size();
	Set requestKeySet = request.getParameterMap().keySet();
	Object KeyArray[] = requestKeySet.toArray();
	Hashtable Form = new Hashtable();
	int i;
	for ( i = 0; i < size; i++ ) {
		Form.put(
			(String)KeyArray[i],
			Basic.toShiftjis(
				request.getParameter(
					(String)KeyArray[i] 
				) 
			) 
		);
	}

	if ( Form.get("SendButton") != "null" ) {
		// *****************************************************
		// プロパティオブジェクトを作成
		// プロパティオブジェクトは、extends Hashtable(連想配列)
		// *****************************************************
		Properties props = new Properties();

		// *****************************************************
		// * 連想配列に送信用サーバのアドレスをセット
		// *****************************************************
		props.put("mail.smtp.host","サーバー");	// ホスト名または IPアドレス
		props.put("mail.smtp.port","587");	// サブミッションポート
		props.put("mail.smtp.auth", "true" );	// SMTP 認証を行う

		// *****************************************************
		// メール用のセッションを作成
		// *****************************************************
		SimpleAuthenticator sa =
			new SimpleAuthenticator("ユーザ","パスワード");
		Session MailSession = 
			Session.getInstance( props, sa );

		try {

			// *****************************************************
			// メール用のメッセージオブジェクトを作成
			// *****************************************************
			MimeMessage msg = new MimeMessage(MailSession);

			// *****************************************************
			// 宛先
			// *****************************************************
			msg.setRecipients(
				Message.RecipientType.TO,
				(String)Form.get("to") 
			);

			// *****************************************************
			// 送信者
			// *****************************************************
			msg.setFrom(
				new InternetAddress( (String)Form.get("from")) 
			);

			// *****************************************************
			// 件名
			// *****************************************************
			msg.setSubject(
				MimeUtility.encodeText(
					(String)Form.get("subject"),
					"iso-2022-jp",
					"B"
				)
			);

			// *****************************************************
			// 本文
			// *****************************************************
			msg.setContent(
				Form.get(
					"comment"
				),
				"text/plain; charset=\"iso-2022-jp\""
			);

			// *****************************************************
			// 送信
			// *****************************************************
			Transport.send( msg );

		}
		catch (Exception e) {
			out.println("送信エラー");
		}
	}
	else {
		out.println("メール送信データを入力して下さい<br>");
	}

%>

</BODY>
</HTML>

  



  リクエストデータ処理を Bean に移行



  
package lightbox;

import javax.servlet.http.*;
import java.util.*;
import java.io.*;

public class basic {

	// *****************************************************
	// フォームから受け取った文字列が、ISO_8859_1でデコード
	// されてしまったているので、文字列を元へ戻す為に、いっ
	// たんISO_8859_1でエンコードし、それを再びShift_JISで
	// デコードする
	// *****************************************************
	public String toShiftjis( String strData ) {

		String ret;

		if ( strData != null ) {
			try {
				ret = new String( 
							strData.getBytes( "ISO_8859_1" ),
							 "Shift_JIS"
						);
			}
			catch( UnsupportedEncodingException e ) {
				ret = "UnsupportedEncoding";
			}
		}
		else {
			ret = "null";
		}

		return ret;

	}

	// *****************************************************
	// 数値で表現された文字列を int に変換する
	// 数値で表現されていない場合は、エラーとなるのでゼロ
	// を返す
	// *****************************************************
	public int atoi( String strData ) {

		int ret;

		try {
			ret = Integer.parseInt(strData);
		}
		catch (NumberFormatException e) {
			ret = 0;
		}

		return ret;

	}

	// *****************************************************
	// String.valueOfで数値の型に依存しない変換ができるが
	// ソースを読んだ時の処理の意図を明確にする為に作成
	// *****************************************************
	public String itoa( int nData ) {

		String ret = String.valueOf( nData );

		return ret;

	}

	// *****************************************************
	// * リクエストデータを漢字変換して連想配列に保存
	// *****************************************************
	public Hashtable getRequest( HttpServletRequest request ) {
		Map requestMap = request.getParameterMap();
		int size = requestMap.size();
		Set requestKeySet = request.getParameterMap().keySet();
		Object KeyArray[] = requestKeySet.toArray();
		Hashtable tableReauest = new Hashtable();
		int i;
		for ( i = 0; i < size; i++ ) {
			tableReauest.put(
				(String)KeyArray[i],
				this.toShiftjis(
					request.getParameter(
						(String)KeyArray[i] 
					) 
				) 
			);
		}
		return tableReauest;
	}

}

  



  メール送信処理の変更

  
<HTML>
<%@ page
	language="java"
	contentType="text/html;charset=shift_jis"
	import="java.util.*"
	import="javax.mail.*"
	import="javax.mail.internet.*" 
%>
<jsp:useBean id="Basic" scope="session" class="lightbox.basic" />
<%!
// *********************************************************
// 認証用のプライベートクラス
// *********************************************************
private class SimpleAuthenticator extends Authenticator {

	private String user_string = null;
	private String pass_string = null;

	public SimpleAuthenticator( String user_s, String pass_s ) {
		super();
		user_string = user_s;
		pass_string = pass_s;
	}

	protected PasswordAuthentication getPasswordAuthentication(){
		return new PasswordAuthentication( this.user_string, this.pass_string );
	}
}
%>

<BODY>
<H2>メール送信</H2>
<FORM method=post>
	<INPUT type=text name=from><br>
	<INPUT type=text name=to><br>
	<INPUT type=text name=subject><br>
	<TEXTAREA name=comment cols=40 rows=10></TEXTAREA><br>
	<INPUT name=SendButton type=submit value=送信>
</FORM>

<%
	// *****************************************************
	// * リクエストデータを漢字変換して連想配列に保存
	// *****************************************************
	Hashtable Form = Basic.getRequest( request );

	if ( Form.get("SendButton") != null ) {
		// *****************************************************
		// プロパティオブジェクトを作成
		// プロパティオブジェクトは、extends Hashtable(連想配列)
		// *****************************************************
		Properties props = new Properties();

		// *****************************************************
		// * 連想配列に送信用サーバのアドレスをセット
		// *****************************************************
		props.put("mail.smtp.host","サーバー");	// ホスト名または IPアドレス
		props.put("mail.smtp.port","587");	// サブミッションポート
		props.put("mail.smtp.auth", "true" );	// SMTP 認証を行う

		// *****************************************************
		// メール用のセッションを作成
		// *****************************************************
		SimpleAuthenticator sa =
			new SimpleAuthenticator("ユーザ","パスワード");
		Session MailSession = 
			Session.getInstance( props, sa );

		try {

			// *****************************************************
			// メール用のメッセージオブジェクトを作成
			// *****************************************************
			MimeMessage msg = new MimeMessage(MailSession);

			// *****************************************************
			// 宛先
			// *****************************************************
			msg.setRecipients(
				Message.RecipientType.TO,
				(String)Form.get("to") 
			);

			// *****************************************************
			// 送信者
			// *****************************************************
			msg.setFrom(
				new InternetAddress( (String)Form.get("from")) 
			);

			// *****************************************************
			// 件名
			// *****************************************************
			msg.setSubject(
				MimeUtility.encodeText(
					(String)Form.get("subject"),
					"iso-2022-jp",
					"B"
				)
			);

			// *****************************************************
			// 本文
			// *****************************************************
			msg.setContent(
				Form.get(
					"comment"
				),
				"text/plain; charset=\"iso-2022-jp\""
			);

			// *****************************************************
			// 送信
			// *****************************************************
			Transport.send( msg );

		}
		catch (Exception e) {
			out.println("送信エラー");
		}
	}
	else {
		out.println("メール送信データを入力して下さい<br>");
	}

%>

</BODY>
</HTML>

  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ