Saturday, January 10, 2009

JavaMail API Replying to Mail

JavaMail API Replying to Mail

Tasks

------

The skeleton code already includes the code to get the list of messages from the folder and prompt you to create a reply.

When answered affirmitively, create a new MimeMessage from the original message.

Set the from field to your email address.

Create the text for the reply. Include a canned message to start. When the original message is plain text, add each line of the original message, prefix each line with the "> " characters.

Set the message's content, once the message content is fully determined.

Send the message.

Compile and run the program, passing your mail server, SMTP server, username, password, and from address on the command line. Answer YES to the messages you want to send replies. Just hit ENTER if you don't. If you want to stop going through your mail before making your way through all the messages, enter QUIT.

Check to make sure you received the message with your normal mail reader (Eudora, Outlook Express, pine, ...).

import java.io.*;

import java.util.Properties;

import javax.mail.*;

import javax.mail.internet.*;

public class ReplyExample {

public static void main (String args[]) throws Exception {

String host = args[0];

String sendHost = args[1];

String username = args[2];

String password = args[3];

String from = args[4];

// Create empty properties

Properties props = System.getProperties();

props.put("mail.smtp.host", sendHost);

// Get session

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

// Get the store

Store store = session.getStore("pop3");

store.connect(host, username, password);

// Get folder

Folder folder = store.getFolder("INBOX");

folder.open(Folder.READ_ONLY);

BufferedReader reader = new BufferedReader (

new InputStreamReader(System.in));

// Get directory

Message message[] = folder.getMessages();

for (int i=0, n=message.length; i

System.out.println(i + ": " + message[i].getFrom()[0]

+ "\t" + message[i].getSubject());

System.out.println("Do you want to reply to the message? [YES to reply/QUIT to end]");

String line = reader.readLine();

if ("YES".equals(line)) {

// Create a reply message

MimeMessage reply = (MimeMessage)message[i].reply(false);

// Set the from field

reply.setFrom(new InternetAddress(from));

// Create the reply content, copying over the original if text

MimeMessage orig = (MimeMessage)message[i];

StringBuffer buffer = new StringBuffer("Thanks\n\n");

if (orig.isMimeType("text/plain")) {

String content = (String)orig.getContent();

StringReader contentReader = new StringReader(content);

BufferedReader br = new BufferedReader(contentReader);

String contentLine;

while ((contentLine = br.readLine()) != null) {

buffer.append("> ");

buffer.append(contentLine);

buffer.append("\r\n");

}

}

// Set the content

reply.setText(buffer.toString());

// Send the message

Transport.send(reply);

} else if ("QUIT".equals(line)) {

break;

}

}

// Close connection

folder.close(false);

store.close();

}

}

Demonstration

----------------

After executing the following command (replacing the mail server, SMTP server, username, password, and from address):

java ReplyExample POP.Server SMTP.Server username

password from@address

You'll be prompted to reply to each of the messages in your INBOX. Enter YES to have a reply sent.

0: president@whitehouse.gov Thanks.

Do you want to reply to the message? [YES to reply/QUIT to end]

YES

1: billg@microsoft.com No Thanks.

Do you want to reply to the message? [YES to reply/QUIT to end]

YES


************************************************************************

No comments:

Post a Comment