Loading [MathJax]/extensions/tex2jax.js

2012-01-10

Java: メール送信プログラム

JavaMailを利用したメール送信プログラムです。いちおう添付ファイルを2つ添付できますが、ここの処理はあまり参考にしないほうがいいです。
  1. import java.util.*;  
  2. import java.io.*;  
  3. import javax.mail.*;  
  4. import javax.mail.internet.*;  
  5. import javax.activation.*;  
  6.   
  7. public class MailSender{  
  8.     //日本語の文字コード  
  9.     public static final String JAPANESE_CODE = "ISO-2022-JP";  
  10.       
  11.     //通信情報  
  12.     private String to, from, host;  
  13.     private boolean debug;  
  14.       
  15.     //題名、本文、添付ファイル  
  16.     private String subject, text;  
  17.     private File file1, file2;  
  18.   
  19.     //Toと添付ファイル以外を初期化  
  20.     public MailSender()  
  21.     {  
  22.     }  
  23.       
  24.     public void setTo(String to)  
  25.     {  
  26.         this.to = to;  
  27.     }  
  28.       
  29.     public String getTo()  
  30.     {  
  31.         return to;  
  32.     }  
  33.       
  34.     public void setFrom(String from)  
  35.     {  
  36.         this.from = from;  
  37.     }  
  38.       
  39.     public String getFrom()  
  40.     {  
  41.         return from;  
  42.     }  
  43.       
  44.     public void setHost(String host)  
  45.     {  
  46.         this.host = host;  
  47.     }  
  48.       
  49.     public String getHost()  
  50.     {  
  51.         return host;  
  52.     }  
  53.       
  54.     public void setDebug(boolean b)  
  55.     {  
  56.         this.debug = b;  
  57.     }  
  58.       
  59.     public boolean getDebug()  
  60.     {  
  61.         return debug;  
  62.     }  
  63.       
  64.     public void setSubject(String subject)  
  65.     {  
  66.         this.subject = subject;  
  67.     }  
  68.       
  69.     public String getSubject()  
  70.     {  
  71.         return subject;  
  72.     }  
  73.       
  74.     public void setText(String text)  
  75.     {  
  76.         this.text = text;  
  77.     }  
  78.       
  79.     public String getText()  
  80.     {  
  81.         return text;  
  82.     }  
  83.       
  84.     public void setFile1(File file1)  
  85.     {  
  86.         this.file1 = file1;  
  87.     }  
  88.       
  89.     public File getFile1()  
  90.     {  
  91.         return file1;  
  92.     }  
  93.   
  94.     public void setFile2(File file2)  
  95.     {  
  96.         this.file2 = file2;  
  97.     }  
  98.   
  99.     public File getFile2()  
  100.     {  
  101.         return file2;  
  102.     }  
  103.       
  104.     //メールを送信する  
  105.     //戻り値:正常に送信された場合は true、そうでない場合は false   
  106.     public boolean send()  
  107.     {  
  108.         try  
  109.         {  
  110.             MimeMessage message = getMimeMessage();  
  111.             message.setContent( getMultipart() );  
  112.             Transport.send( message );  
  113.             return true;  
  114.         }  
  115.         catch(Exception e)  
  116.         {  
  117.             return false;  
  118.         }  
  119.     }  
  120.       
  121.     public Properties getProperties()  
  122.     {  
  123.         Properties props = new Properties();  
  124.         props.put("mail.smtp.host", host);  
  125.         props.put("mail.host", host);  
  126.         props.put("mail.from", from);  
  127.           
  128.         if(debug) props.put("mail.debug", debug);  
  129.           
  130.         return props;  
  131.     }  
  132.       
  133.     public Session getSession()  
  134.     {  
  135.         Properties props = getProperties();  
  136.         Session session = Session.getInstance(props);  
  137.         session.setDebug(debug);  
  138.         return session;  
  139.     }  
  140.       
  141.     public MimeMessage getMimeMessage() throws Exception  
  142.     {  
  143.         MimeMessage message = new MimeMessage( getSession() );  
  144.           
  145.         message.setFrom(new InternetAddress(from));  
  146.         InternetAddress[] address = InternetAddress.parse(to);  
  147.         message.setRecipients(Message.RecipientType.TO, address);  
  148.         message.setSubject(subject, JAPANESE_CODE);  
  149.         message.setSentDate(new Date());  
  150.           
  151.         return message;  
  152.     }  
  153.   
  154.     public Multipart getMultipart() throws Exception  
  155.     {  
  156.         Multipart mp = new MimeMultipart();  
  157.         mp.addBodyPart(getTextPart());  
  158.         mp.addBodyPart(getFilePart1());  
  159.         mp.addBodyPart(getFilePart2());  
  160.         return mp;  
  161.     }  
  162.   
  163.     public MimeBodyPart getTextPart() throws Exception  
  164.     {  
  165.         MimeBodyPart part = new MimeBodyPart();  
  166.         part.setText(text, JAPANESE_CODE);  
  167.         return part;  
  168.     }  
  169.   
  170.     public MimeBodyPart getFilePart1() throws Exception  
  171.     {  
  172.         MimeBodyPart part = new MimeBodyPart();  
  173.   
  174.         FileDataSource fds = new FileDataSource( file1.getPath() );  
  175.         part.setDataHandler(new DataHandler( fds ));  
  176.         part.setFileName(MimeUtility.encodeWord( fds.getName() ));  
  177.   
  178.         return part;  
  179.     }  
  180.   
  181.     public MimeBodyPart getFilePart2() throws Exception  
  182.     {  
  183.         MimeBodyPart part = new MimeBodyPart();  
  184.   
  185.         FileDataSource fds = new FileDataSource( file2.getPath() );  
  186.         part.setDataHandler(new DataHandler( fds ));  
  187.         part.setFileName(MimeUtility.encodeWord( fds.getName() ));  
  188.   
  189.         return part;  
  190.     }  
  191. }  
  192.       
上記クラスの利用例です。
  1. public  void test(){  
  2.     MailSender ms = new MailSender();  
  3.    //下記に適切なデータを記述  
  4.     ms.setFrom("");  
  5.     ms.setTo("");  
  6.     ms.setDebug(false);  
  7.     ms.setHost("");  
  8.     ms.setSubject("");  
  9.     ms.setText("");  
  10.     //添付ファイルがある場合(任意)  
  11.     ms.setFile1( new File("") );  
  12.     ms.setFile2( new File("") );  
  13.     //メール送信  
  14.     ms.send();  
  15. }  


0 件のコメント:

コメントを投稿