GAEは無料で一日2000通のメールを送信する可能ですので、本当に便利な機能です。
SendmailServlet.java
package sendmail;
import java.io.IOException;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.*;
import com.google.appengine.api.mail.MailService;
import com.google.appengine.api.mail.MailServiceFactory;
@SuppressWarnings("serial")
public class SendmailServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException
{
resp.setContentType("text/plain");
resp.setCharacterEncoding("utf-8");
String cmd = req.getParameter("cmd");
if (cmd.equals("1"))
{
MailService.Message msg = new MailService.Message();
msg.setSender("XXXXX@gmail.com");
msg.setTo("YYYYY@gmail.com");
msg.setSubject("テストメール11111");
msg.setTextBody("メール内容11111");
MailService mailservice = MailServiceFactory.getMailService();
mailservice.send(msg);
resp.getWriter().println("Mail Sended.");
}
else if (cmd.equals("2"))
{
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage msg = new MimeMessage(session);
try
{
msg.setFrom(new InternetAddress("XXXXX@gmail.com"));
msg.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress("YYYYY@gmail.com"));
msg.addRecipient(javax.mail.Message.RecipientType.CC, new InternetAddress("ZZZZZ@gmail.com"));
msg.setSubject("メール送信テスト22222", "ISO-2022-JP");
msg.setText("メール内容22222");
Transport.send(msg);
resp.getWriter().println("Mail Sended.");
}
catch (AddressException e)
{
e.printStackTrace();
}
catch (MessagingException e)
{
e.printStackTrace();
}
}
}
}
0 件のコメント:
コメントを投稿