If I have a complete message in an InputStream, how can I construct a Message from it?

John Zukowski

Just create an InputStream with the messge and call the MimeMessage constructor that accepts the input stream. For instance, if your message was in the file message.txt:

Subject: Testing 
To: Me <foo@example.net> 
From: "You" <bar@example.com>

The content.
You can send with the following program, passing in your SMTP server and filename as args:
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.*;

public class MailExample {
  public static void main (String args[]) throws Exception {
    String host = args[0];
    String filename = args[1];

    // Get system properties
    Properties props = System.getProperties();

    // Setup mail server
    props.put("mail.smtp.host", host);

    // Get session
    Session session = Session.getDefaultInstance(props, null);

    // Get InputStream
    FileInputStream fis = new FileInputStream(filename);

    // Create message
    MimeMessage message = new MimeMessage(session, fis);

    // Send message
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact