!!!JavaMailでのメール送信 !!添付ファイルなし {{code Java, 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(); } } } }} !!添付ファイルあり(マルチパート) {{code Java, 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(); } } } }} {{category2 プログラミング言語,Java}}