Java プログラムメモ


トップ

メール送信

Java でメールを送信する為には、JavaMail API が必要になります。
以下にメール送信の例を書きますが SMTP サーバーやメールアドレスは適宜設定してください。
テキストメールを送信する場合は mail.jar だけが必要になります。

テキストメール送信例

import java.util.*; import javax.mail.*; import javax.mail.internet.*; public class MailTest { private String charset_; private Session session_; public MailTest(String smtpHost, String charset) { if (smtpHost == null) { throw new NullPointerException("SMTP Host is null."); } if (charset == null) { throw new NullPointerException("Charctor set is null."); } charset_ = charset; Properties props = new Properties(); //SMTP サーバの設定 props.put("mail.smtp.host", smtpHost); session_ = Session.getDefaultInstance(props, null); } private void sendMail(String body) throws MessagingException { MimeMessage msg = new MimeMessage(session_); //送信先の設定 msg.setRecipient(Message.RecipientType.TO, new InternetAddress( "himtodo@infoseek.jp")); //送信元の設定 msg.setFrom(new InternetAddress("himtodo@infoseek.jp")); //送信日付の設定 msg.setSentDate(new Date()); //Subject の設定 msg.setSubject("テストメールです", charset_); //本文 の設定 msg.setText(body, charset_); //メールの送信 Transport.send(msg); } public static void main(String[] args) throws Exception { MailTest mail = new MailTest("localhost", "iso-2022-jp"); mail.sendMail("テストメールです。"); } }

添付メールを送信したい場合は mail.jar 及び activation.jar が必要になります。

添付メール送信例

import java.io.*; import java.util.*; import javax.activation.*; import javax.mail.*; import javax.mail.internet.*; public class MailTest { private String charset_; private Session session_; public MailTest(String smtpHost, String charset) { if (smtpHost == null) { throw new NullPointerException("SMTP Host is null."); } if (charset == null) { throw new NullPointerException("Charctor set is null."); } charset_ = charset; Properties props = new Properties(); //SMTP サーバの設定 props.put("mail.smtp.host", smtpHost); session_ = Session.getDefaultInstance(props, null); } private void sendMail(String body) throws MessagingException { MimeMessage msg = new MimeMessage(session_); //送信先の設定 msg.setRecipient(Message.RecipientType.TO, new InternetAddress( "himtodo@infoseek.jp")); //送信元の設定 msg.setFrom(new InternetAddress("himtodo@infoseek.jp")); //送信日付の設定 msg.setSentDate(new Date()); //Subject の設定 msg.setSubject("テストメールです", charset_); //本文 の設定 MimeBodyPart messageBody = new MimeBodyPart(); messageBody.setContent(body, "text/plain; charset=" + charset_); //添付ファイル の設定 MimeBodyPart attachBody = new MimeBodyPart(); FileDataSource source = new FileDataSource(new File("C:/work/test.txt")); attachBody.setDataHandler(new DataHandler(source)); attachBody.setFileName("test.txt"); //Multipart の作成 MimeMultipart mp = new MimeMultipart(); mp.addBodyPart(messageBody); mp.addBodyPart(attachBody); msg.setContent(mp); //メールの送信 Transport.send(msg); } public static void main(String[] args) throws Exception { MailTest mail = new MailTest("localhost", "iso-2022-jp"); mail.sendMail("テストメールです。"); } }


プログラムメモ アフリカ雑貨・珈琲豆・アールブリュット作品 マゴソスクール クラウドファンディング
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送