トップ 差分 一覧 ソース 置換 検索 ヘルプ PDF RSS ログイン

Javaでメール送信

JavaMailでのメール送信

 添付ファイルなし

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.*;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.activation.DataSource;
import javax.mail.Multipart;
import javax.mail.util.ByteArrayDataSource;
import javax.mail.internet.MimeBodyPart;
import javax.activation.DataHandler;
import javax.mail.internet.MimeMultipart;

public class test {
  public static void main(String[] args) {
    try {
      
      
      Properties property = new Properties();
      
      property.put("mail.smtp.host","smtp.gmail.com");
      
      //GmailのSMTPを使う場合
      property.put("mail.smtp.auth", "true");
      property.put("mail.smtp.starttls.enable", "true");
      property.put("mail.smtp.host", "smtp.gmail.com");
      property.put("mail.smtp.port", "587");
           
      Session session = Session.getInstance(property, new javax.mail.Authenticator(){
          protected PasswordAuthentication getPasswordAuthentication(){
            return new PasswordAuthentication("メールアドレス@gmail.com", "password");
          }
      });
      
      MimeMessage mimeMessage = new MimeMessage(session);
      
      
      // InternetAddress toAddress = 
      // new InternetAddress("メールアドレス@gmail.com", "ほげ", "ISO-2022-JP");
      // mimeMessage.setRecipient(Message.RecipientType.TO, toAddress);
      
      // 複数アドレスに送信する可能性がある場合、
      // カンマ区切りのアドレスを解釈してくれるparseを使う。
      InternetAddress[] toAddress = 
      InternetAddress.parse("メールアドレス@gmail.com");
      mimeMessage.setRecipients(Message.RecipientType.TO, toAddress);
      
      // gmailでは認証した場合、fromのアドレスは認証のアドレスになるので
      // setFromで何を設定してもあまり意味は無い
      InternetAddress fromAddress = 
      new InternetAddress("メールアドレス@gmail.com",null);
      mimeMessage.setFrom(fromAddress);
      
      mimeMessage.setSubject("タイトルテスト", "ISO-2022-JP");
      
      //マルチパートではない場合は、これが本文になる。
      mimeMessage.setText("メッセージ\nほげほげ", "ISO-2022-JP");
            
      Transport.send(mimeMessage);
      
    } catch (Exception e){
      e.printStackTrace();
    }
  }
}


 添付ファイルあり(マルチパート)

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.util.*;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.activation.DataSource;
import javax.mail.Multipart;
import javax.mail.util.ByteArrayDataSource;
import javax.mail.internet.MimeBodyPart;
import javax.activation.DataHandler;
import javax.mail.internet.MimeMultipart;

public class test {
  public static void main(String[] args) {
    try {
      
      
      Properties property = new Properties();
      
      property.put("mail.smtp.host","smtp.gmail.com");
      
      //GmailのSMTPを使う場合
      property.put("mail.smtp.auth", "true");
      property.put("mail.smtp.starttls.enable", "true");
      property.put("mail.smtp.host", "smtp.gmail.com");
      property.put("mail.smtp.port", "587");
      
      // 日本語ファイル名のファイルを添付する場合はSystemのプロパティをいじる
      System.setProperty("mail.mime.encodefilename", "true");
      // System.setProperty("mail.mime.charset", "UTF-8");
      
      Session session = Session.getInstance(property, new javax.mail.Authenticator(){
          protected PasswordAuthentication getPasswordAuthentication(){
            return new PasswordAuthentication("メールアドレス@gmail.com", "password");
          }
      });
      
      MimeMessage mimeMessage = new MimeMessage(session);
      
      
      // InternetAddress toAddress = 
      // new InternetAddress("メールアドレス@gmail.com", "ほげ", "ISO-2022-JP");
      // mimeMessage.setRecipient(Message.RecipientType.TO, toAddress);
      
      // 複数アドレスに送信する可能性がある場合、
      // カンマ区切りのアドレスを解釈してくれるparseを使う。
      InternetAddress[] toAddress = 
      InternetAddress.parse("メールアドレス@gmail.com");
      mimeMessage.setRecipients(Message.RecipientType.TO, toAddress);
      
      // gmailでは認証した場合、fromのアドレスは認証のアドレスになるので
      // setFromで何を設定してもあまり意味は無い
      InternetAddress fromAddress = 
      new InternetAddress("メールアドレス@gmail.com",null);
      mimeMessage.setFrom(fromAddress);
      
      mimeMessage.setSubject("タイトルテスト", "ISO-2022-JP");
      
      //マルチパートのテスト
      String str1 = "添付ファイル 1 の内容です.";
      String str2 = "添付ファイル 2 の内容です.";
      
      //本文をマルチパートの先頭に置く
      MimeBodyPart attachmentPart = new MimeBodyPart();
      attachmentPart.setText("ほげ\nほげ", "ISO-2022-JP");
      
      DataSource ds = new ByteArrayDataSource(str1.getBytes("UTF-8"), "application/octet-stream");
      MimeBodyPart attachmentPart1 = new MimeBodyPart();
      attachmentPart1.setDataHandler(new DataHandler(ds));
      attachmentPart1.setFileName("テスト.txt");
      attachmentPart1.setText("ほげ2\nhoge", "ISO-2022-JP");
      
      DataSource ds2 = new ByteArrayDataSource(str2.getBytes("UTF-8"), "application/octet-stream");
      MimeBodyPart attachmentPart2 = new MimeBodyPart();
      attachmentPart2.setDataHandler(new DataHandler(ds2));
      attachmentPart2.setFileName("attchement2.txt");
      
      Multipart multipart = new MimeMultipart();
      multipart.addBodyPart(attachmentPart);
      multipart.addBodyPart(attachmentPart1);
      multipart.addBodyPart(attachmentPart2);
      mimeMessage.setContent(multipart);
      
      Transport.send(mimeMessage);
      
    } catch (Exception e){
      e.printStackTrace();
    }
  }
}


[カテゴリ: プログラミング言語 > Java]

[通知用URL]



  • Hatenaブックマークに追加
  • livedoorクリップに追加
  • del.icio.usに追加
  • FC2ブックマークに追加

最終更新時間:2017年01月03日 17時29分10秒