FileDocCategorySizeDatePackage
Sender.javaAPI DocExample2436Sat Nov 25 12:55:00 GMT 2000None

Sender

public class Sender extends Object
sender -- send an email message.
author
Ian F. Darwin
version
$Id: Sender.java,v 1.2 2000/11/25 17:55:00 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 message body
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...
		Properties props = new Properties();

		// Your LAN must define the local SMTP server as "mailhost"
		// for this simple-minded version to be able to send mail...
		props.put("mail.smtp.host", "mailhost");

		// 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.
			mesg.setText(message_body);
			// XXX I18N: use setText(msgText.getText(), charset)
			
			// Finally, send the message!
			Transport.send(mesg);

		} catch (MessagingException ex) {
			while ((ex = (MessagingException)ex.getNextException()) != null) {
				ex.printStackTrace();
			}
		}
	
public static voidmain(java.lang.String[] av)
Simple test case driver

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