忍者ブログ
Flex,Java,Html など、調べた事のメモ帳です。 間違ってたらゴメンなさい。
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

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("テストメールです。");
    }
}
この記事にコメントする
name
title
color
mail
URL
comment
pass   Vodafone絵文字 i-mode絵文字 Ezweb絵文字
secret (チェックを入れると管理人だけに表示できます)
ブログランキング
にほんブログ村 IT技術ブログ IT技術メモへ
 
カレンダー
03 2024/04 05
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
人気ブログランキング
ブログランキング ブログランキング
最新TB
ブログ内検索
PR
ATOM  
ATOM 
RSS  
RSS 
Copyright ©   プログラムメモ   All Rights Reserved
Design by MMIT  Powered by NINJA TOOLS
忍者ブログ [PR]