Transportクラス

広告

では先ほどのサンプルプログラムの中身を1つ1つ確認していきます。

メールを実際に送信しているのはTransport.send(msg);の部分になります。そこでまずTransportクラスについて見てみましょう。

  • java.lang.Object
  • javax.mail.Service
  • javax.mail.Transport
  • public abstract class Transport extends Service

このクラスはabstractクラスですので、そのままオブジェクトを作ることはできないのですが、staticメソッドとしてsendが用意されています。

Send a message. The message will be sent to all recipient addresses 
specified in the message (as returned from the Message method 
getAllRecipients), using message transports appropriate to each address. 
The send method calls the saveChanges method on the message before 
sending it. 

If any of the recipient addresses is detected to be invalid by the 
Transport during message submission, a SendFailedException is thrown. 
Clients can get more detail about the failure by examining the exception. 
Whether or not the message is still sent succesfully to any valid 
addresses depends on the Transport implementation. See SendFailedException 
for more details. Note also that success does not imply that the message 
was delivered to the ultimate recipient, as failures may occur in later 
stages of delivery. Once a Transport accepts a message for delivery to a 
recipient, failures that occur later should be reported to the user via 
another mechanism, such as returning the undeliverable message. 

Parameters:
  msg - the message to send
Throws:
  SendFailedException - if the message could not be sent to some or any of 
    the recipients.
  MessagingException -  

送りたいメールの中身を引数のMessageクラスのオブジェクトに入れてこのメソッドを呼ぶと、メールが送信されます。

try{
  /* メールの受け皿を作る */
  MimeMessage msg = new MimeMessage(session);
  ...(msgにメールで送りたい情報を色々入れる)...

  /* メールを実際に送信する */
  Transport.send(msg);
}catch(MessagingException mex){
  System.out.println("¥n--Exception handling in msgsendsample.java");
  mex.printStackTrace();
}

では次の頁でMessageクラスについて見てみます。

( Written by Tatsuo Ikura )