FileDocCategorySizeDatePackage
SendMime.javaAPI DocExample3109Sat May 31 17:18:36 BST 2003None

SendMime

public class SendMime extends Object
SendMime -- send a multi-part MIME email message.
author
Ian F. Darwin
version
$Id: SendMime.java,v 1.8 2003/05/31 21:18:35 ian Exp $

Fields Summary
protected String
message_recip
The message recipient.
protected String
message_subject
protected String
message_cc
The message CC recipient.
protected String
message_body
The text/plain message body
protected String
html_data
protected Session
session
The JavaMail session object
protected Message
mesg
The JavaMail message object
Constructors Summary
Methods Summary
public voiddoSend()
Do the work: send the mail to the SMTP server.


	            
	      

		// We need to pass info to the mail server as a Properties, since
		// JavaMail (wisely) allows room for LOTS of properties...
		FileProperties props = 
			new FileProperties(MailConstants.PROPS_FILE_NAME);

		// Copy the value of Mail.send.host into mail.smtp.host
		props.setProperty("mail.smtp.host", 
			props.getProperty(MailConstants.SEND_HOST));

		// Create the Session object
		session = Session.getDefaultInstance(props, null);
		session.setDebug(true);		// Verbose!
		
		try {
			// create a message
			mesg = new MimeMessage(session);

			// From Address - this should come from a Properties...
			mesg.setFrom(new InternetAddress("nobody@host.domain"));

			// TO Address 
			InternetAddress toAddress = new InternetAddress(message_recip);
			mesg.addRecipient(Message.RecipientType.TO, toAddress);

			// CC Address
			InternetAddress ccAddress = new InternetAddress(message_cc);
			mesg.addRecipient(Message.RecipientType.CC, ccAddress);

			// The Subject
			mesg.setSubject(message_subject);

			// Now the message body.
			Multipart mp = new MimeMultipart();

			BodyPart textPart = new MimeBodyPart();
			textPart.setText(message_body);	// sets type to "text/plain"

			BodyPart pixPart = new MimeBodyPart();
			pixPart.setContent(html_data, "text/html");

			// Collect the Parts into the MultiPart
			mp.addBodyPart(textPart);
			mp.addBodyPart(pixPart);

			// Put the MultiPart into the Message
			mesg.setContent(mp);
			
			// Finally, send the message!
			Transport.send(mesg);

		} catch (MessagingException ex) {
			System.err.println(ex);
			ex.printStackTrace(System.err);
		}
	
public static voidmain(java.lang.String[] av)
Simple test case driver

		SendMime sm = new SendMime();
		sm.doSend();