メール送信サーブレット
サーブレットでメールを送るには java.mail が必要です。
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.*;
public class Mailsend {
private String host = "localhost";
private String to = "";
private String from = "";
private String subject = "";
private String body = "";
public Mailsend(){
}
public int send(String to, String from, String subject, String body){
this.to = to;
this.from = from;
this.subjest = subject;
this.body = body;
try{
Properties prp = new Properties();
prp.put("mail.smtp.host",host):
prp.put("mail.host",host);
Session ses = Session.getInstance(prp,null);
MimeMessage mmsg = new MimeMessage(ses);
InternetAddress[] ia_to = {new InternetAddress(to,to)};
InternetAddress ia_frm = new InternetAddress(from,from);
mmsg.setRecipients(Message.RecipientType.TO,ia_to);
mmsg.setFrom(ia_frm);
mmsg.setSentDate(new Date());
mmsg.setSubject(MimeUtility.encodeText(subject,"iso-2022-jp","B"));
mmsg.setContent(body,"text/plain; charset=iso-2022-jp");
Transport.send(mmsg);
return(0);
}catch(Exception e){
return(0);
}
}
}
呼び出すJSP
<%@page import="Mailsend" %>
<%
String to = "送信先";
String from = "送信元";
String subject = "タイトル";
String body = "内容";
Mailsend mail = new Mailsend();
mail.send(to,from,subject,body);
%>