JavaMail でPDF or PNG を添付すると、届いた先でテキストファイルと認識されてしまいます。
タイプの指定方法がいまいち分からないので、
ご存知の方おりましたらお知恵を拝借できませんでしょうか?
ちなみにメールはサンダーバードで見ています。
java
1public class MailSend { 2 3 4 /** 5 * メール送信を行う。 6 * 7 * @param args 8 * 第1引数:プロパティファイル 第2引数以降:添付ファイル 9 * @throws Exception 10 * 異常終了した場合 11 */ 12 13 private static final String ENCODE = "UTF-8"; 14 private static final int SUCCESS = 0; 15 private static final int FAILED = 9; 16 public static void main(String[] args) throws Exception { 17 try { 18 Properties config = new Properties(); 19 if (args.length == 0) { 20 throw new IllegalArgumentException("引数が不正です。"); 21 } 22 23 InputStreamReader propFile = new InputStreamReader(new FileInputStream(args[0]), ENCODE); 24 config.load(propFile); 25 26 Session mailSession = Session.getInstance(config, new javax.mail.Authenticator() { 27 protected PasswordAuthentication getPasswordAuthentication() { 28 return new PasswordAuthentication(config.getProperty("mail.smtp.user"), 29 config.getProperty("mail.smtp.pass")); 30 } 31 }); 32 33 MimeMessage msg = new MimeMessage(mailSession); 34 msg.setFrom(new InternetAddress(config.getProperty("From"))); 35 InternetAddress[] toAddress = InternetAddress.parse(config.getProperty("To")); 36 InternetAddress[] ccAddress = InternetAddress.parse(config.getProperty("Cc")); 37 msg.setRecipients(Message.RecipientType.TO, toAddress); 38 msg.setRecipients(Message.RecipientType.CC, ccAddress); 39 40 msg.setSentDate(new Date()); 41 msg.setSubject(config.getProperty("Subject"), ENCODE); 42 43 // 本文 44 MimeMultipart multiPart = new MimeMultipart(); 45 MimeBodyPart textPart = new MimeBodyPart(); 46 textPart.setText(config.getProperty("Body"), ENCODE); 47 multiPart.addBodyPart(textPart); 48 49 // 添付ファイル(引数) 50 for (int i = 1; i < args.length; i++) { 51 String filePath = args[i]; 52 MimeBodyPart attachPart = new MimeBodyPart(); 53 DataSource fds = new FileDataSource(filePath); 54 DataHandler data = new DataHandler(fds); 55 attachPart.setDataHandler(data); 56 attachPart.setFileName(MimeUtility.encodeWord(fds.getName())); 57 multiPart.addBodyPart(attachPart); 58 } 59 // メールにマルチパートを設定 60 msg.setContent(multiPart); 61 62 Transport.send(msg); 63 System.exit(SUCCESS); 64 } catch (Exception e) { 65 e.printStackTrace(); 66 System.exit(FAILED); 67 } 68 69 } 70 71}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/12 21:56 編集
2015/12/13 12:31