www.pudn.com > JMail4Email.rar > MailHelper.java


package ezmail; 
 
import javax.mail.*; 
import javax.mail.internet.*; 
import java.text.SimpleDateFormat; 
 
/** 
 *  Utility class for sending/retrieving messages using the JavaMail API. 
 * 
 *  @author Chad (shod) Darby, darby@j-nine.com 
 */ 
public class MailHelper { 
 
	/** 
	 *  Sends a simple text e-mail message. 
	 * 
	 *  @param mail session the JavaMail session 
	 *  @param from e-mail address of the sender 
	 *  @param to e-mail address of the recipient 
	 *  @param subject subject of the e-mail message 
	 *  @param messageText the actual text of the message 
	 * 
	 *  @throws javax.mail.MessagingFormatException problems sending message 
	 */ 
	public static void sendMessage(Session mailSession, 
								   								 String from, String to, 
								   								 String subject, String messageText) 
	  throws MessagingException { 
 
		// Construct the message 
		System.out.println("Constructing message -  from=" + from + "  to=" + to); 
		InternetAddress fromAddress = new InternetAddress(from); 
		InternetAddress toAddress = new InternetAddress(to); 
 
		MimeMessage testMessage = new MimeMessage(mailSession); 
		testMessage.setFrom(fromAddress); 
		testMessage.addRecipient(javax.mail.Message.RecipientType.TO, toAddress); 
	    testMessage.setSentDate(new java.util.Date()); 
		testMessage.setSubject(subject); 
		testMessage.setText(messageText); 
		System.out.println("Message constructed"); 
 
		// Now send the message 
		Transport.send(testMessage); 
		System.out.println("Message sent!"); 
	} 
 
	/** 
	 *  Retrieve a list of messages from user's inbox 
	 * 
	 *  @param inbox the user's inbox 
	 * 
	 *  @throws javax.mail.MessagingFormatException problems retrieving messages 
	 */ 
	public static Message[] getMessages(Session mailSession, UserInfo theUserInfo) 
		throws MessagingException { 
 
			String pop3host = theUserInfo.getPop3host(); 
			String user = theUserInfo.getUser(); 
			String password = theUserInfo.getPassword(); 
 
			// Retrieve and connect to the Store 
			System.out.println("Connecting to message store: " + pop3host); 
			Store msgStore = mailSession.getStore("pop3"); 
			msgStore.connect(pop3host, user, password); 
			System.out.println("Connected!"); 
 
			// Retrieve the INBOX  folder 
			Folder inbox = msgStore.getDefaultFolder().getFolder("INBOX"); 
			inbox.open(Folder.READ_ONLY); 
 
			// Retrieve a list of messages 
			Message[] theMessages = inbox.getMessages(); 
			FetchProfile profile = new FetchProfile(); 
			profile.add(FetchProfile.Item.ENVELOPE); 
			inbox.fetch(theMessages, profile); 
 
			// Close up shop 
			inbox.close(false); 
			msgStore.close(); 
 
			return theMessages; 
	} 
 
	/** 
	 *  Retrieves a message from user's inbox 
	 * 
	 *  @param inbox the user's inbox 
	 *  @param msgNum message number 
	 * 
	 *  @throws javax.mail.MessagingFormatException message number not found 
	 */ 
	public static Message getMessage(Session mailSession, UserInfo theUserInfo,  int msgNum) 
		throws MessagingException { 
 
			Message theMessage = null; 
 
			String pop3host = theUserInfo.getPop3host(); 
			String user = theUserInfo.getUser(); 
			String password = theUserInfo.getPassword(); 
 
			// Retrieve and connect to the Store 
			System.out.println("Connecting to message store: " + pop3host); 
			Store msgStore = mailSession.getStore("pop3"); 
			msgStore.connect(pop3host, user, password); 
			System.out.println("Connected!"); 
 
			// Retrieve the INBOX  folder 
			Folder inbox = msgStore.getDefaultFolder().getFolder("INBOX"); 
			inbox.open(Folder.READ_ONLY); 
 
				if (msgNum <= inbox.getMessageCount()) { 
					theMessage = inbox.getMessage(msgNum); 
				} 
				else { 
					// Close up shop 
					inbox.close(false); 
					msgStore.close(); 
					throw new MessagingException("Message number not found"); 
				} 
 
			return theMessage; 
	} 
 
	/** 
	 *  Utility methodfor formatting a date 
	 * 
	 *  @param theDate date to format 
	 */ 
	public static String formatDate(java.util.Date theDate) { 
		String result = dateFormatter.format(theDate); 
		return result; 
	} 
 
	/** 
	 *  Utility object for formatting the date 
	 */ 
	private static  SimpleDateFormat dateFormatter 
     						= new SimpleDateFormat ("dd.MMM.yyyy hh:mm:ss a"); 
 
}