org/sonews/mlgw/SMTPTransport.java
author cli
Sun, 29 Aug 2010 17:03:21 +0200
changeset 33 f9bf183447d1
parent 28 15d14b110240
permissions -rw-r--r--
Article(javax.mail.Message) now has a safe method to read the byte body from the given message object (fixes #16).
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package org.sonews.mlgw;
    20 
    21 import java.io.BufferedOutputStream;
    22 import java.io.BufferedReader;
    23 import java.io.IOException;
    24 import java.io.InputStreamReader;
    25 import java.net.Socket;
    26 import java.net.UnknownHostException;
    27 import org.sonews.config.Config;
    28 import org.sonews.storage.Article;
    29 import org.sonews.util.io.ArticleInputStream;
    30 
    31 /**
    32  * Connects to a SMTP server and sends a given Article to it.
    33  * @author Christian Lins
    34  * @since sonews/1.0
    35  */
    36 class SMTPTransport
    37 {
    38 
    39   protected BufferedReader       in;
    40   protected BufferedOutputStream out;
    41   protected Socket               socket;
    42 
    43   public SMTPTransport(String host, int port)
    44     throws IOException, UnknownHostException
    45   {
    46     socket = new Socket(host, port);
    47     this.in  = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    48     this.out = new BufferedOutputStream(socket.getOutputStream());
    49 
    50     // Read helo from server
    51     String line = this.in.readLine();
    52     if(line == null || !line.startsWith("220 "))
    53     {
    54       throw new IOException("Invalid helo from server: " + line);
    55     }
    56 
    57     // Send HELO to server
    58     this.out.write(
    59       ("HELO " + Config.inst().get(Config.HOSTNAME, "localhost") + "\r\n").getBytes("UTF-8"));
    60     this.out.flush();
    61     line = this.in.readLine();
    62     if(line == null || !line.startsWith("250 "))
    63     {
    64       throw new IOException("Unexpected reply: " + line);
    65     }
    66   }
    67 
    68   public SMTPTransport(String host)
    69     throws IOException
    70   {
    71     this(host, 25);
    72   }
    73 
    74   public void close()
    75     throws IOException
    76   {
    77     this.out.write("QUIT".getBytes("UTF-8"));
    78     this.out.flush();
    79     this.in.readLine();
    80 
    81     this.socket.close();
    82   }
    83 
    84   public void send(Article article, String mailFrom, String rcptTo)
    85     throws IOException
    86   {
    87     assert(article != null);
    88     assert(mailFrom != null);
    89     assert(rcptTo != null);
    90 
    91     this.out.write(("MAIL FROM: " + mailFrom).getBytes("UTF-8"));
    92     this.out.flush();
    93     String line = this.in.readLine();
    94     if(line == null || !line.startsWith("250 "))
    95     {
    96       throw new IOException("Unexpected reply: " + line);
    97     }
    98 
    99     this.out.write(("RCPT TO: " + rcptTo).getBytes("UTF-8"));
   100     this.out.flush();
   101     line  = this.in.readLine();
   102     if(line == null || !line.startsWith("250 "))
   103     {
   104       throw new IOException("Unexpected reply: " + line);
   105     }
   106 
   107     this.out.write("DATA".getBytes("UTF-8"));
   108     this.out.flush();
   109     line = this.in.readLine();
   110     if(line == null || !line.startsWith("354 "))
   111     {
   112       throw new IOException("Unexpected reply: " + line);
   113     }
   114 
   115     ArticleInputStream   artStream = new ArticleInputStream(article);
   116     for(int b = artStream.read(); b >= 0; b = artStream.read())
   117     {
   118       this.out.write(b);
   119     }
   120 
   121     // Flush the binary stream; important because otherwise the output
   122     // will be mixed with the PrintWriter.
   123     this.out.flush();
   124     this.out.write("\r\n.\r\n".getBytes("UTF-8"));
   125     this.out.flush();
   126     line = this.in.readLine();
   127     if(line == null || !line.startsWith("250 "))
   128     {
   129       throw new IOException("Unexpected reply: " + line);
   130     }
   131   }
   132 
   133 }