FileDocCategorySizeDatePackage
Sender2.javaAPI DocExample4206Sat Mar 20 14:52:36 GMT 2004None

Sender2

public class Sender2 extends Object
sender -- send an email message. If you give more than one file, each file will be sent to the same recipient with the same subject, so you generally don't want to.
author
Ian F. Darwin
version
$Id: Sender2.java,v 1.8 2004/03/20 20:52: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 message body
protected Session
session
The JavaMail session object
protected Message
mesg
The JavaMail message object
Properties
props
Properties object used to pass props into the MAIL API
Constructors Summary
public Sender2()
Construct a Sender2 object


	     
	    

		// Your LAN must define the local SMTP as "mailhost"
		// for this simple-minded version to be able to send mail...
		props.put("mail.smtp.host", "mailhost");
		finish();
	
public Sender2(String hostName)
Construct a Sender2 object.

param
hostName - the name of the host to send to/via.


		props.put("mail.smtp.host", hostName);
		finish();
	
Methods Summary
public voidaddCCRecipient(java.lang.String message_cc)

 
		// CC Address
		InternetAddress ccAddress = new InternetAddress(message_cc);
		mesg.addRecipient(Message.RecipientType.CC, ccAddress);
	
public voidaddRecipient(java.lang.String message_recip)

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


		// Create the Session object
		session = Session.getDefaultInstance(props, null);
		// session.setDebug(true);		// Verbose!
		
		// create a message
		mesg = new MimeMessage(session);
	
public static voidmain(java.lang.String[] args)
Driver to parse options and control Sender

		try {
			Sender2 sm = new Sender2();
			GetOpt go = new GetOpt("c:f:t:s:");
			char c;
			while ((c =go.getopt(args)) != 0) {
				switch(c) {
				case 'h":
					// XXX sm.setMailHost();
					sm.props.put("mail.smtp.host", go.optarg());
					break;
				case 't":
					sm.addRecipient(go.optarg());
					break;
				case 'c":
					sm.addCCRecipient(go.optarg());
					break;
				case 'f":
					sm.setFrom(go.optarg());
					break;
				case 's":
					sm.setSubject(go.optarg());
					break;
				default:
					System.err.println("Unknown option character " + c);
					usage(1);
				}
			}
			if (go.getOptInd() == args.length) {
				sm.sendFile("(standard input)");
			} else for (int i=go.getOptInd(); i<args.length; i++)
				sm.sendFile(args[i]);
		} catch (MessagingException e) {
			System.err.println(e);
		}
	
public voidsendFile(java.lang.String fileName)

		// Now the message body.
		setBody(message_body);
			
		sendFile();
	
public voidsendFile()
Send the file with no filename, assuming you've already called the setBody() method.

		try {
			
			// Finally, send the message! (use static Transport method)
			Transport.send(mesg);

		} catch (MessagingException ex) {
			while ((ex = (MessagingException)ex.getNextException()) != null) {
				ex.printStackTrace();
			}
		}
	
public voidsetBody(java.lang.String message_body)
Set the message body.

		mesg.setText(message_body);
		// XXX I18N: use setText(msgText.getText(), charset)
	
public voidsetFrom(java.lang.String sender)

		// From Address - this should come from a Properties...
		mesg.setFrom(new InternetAddress(sender));
	
public voidsetSubject(java.lang.String message_subject)

		// The Subject
		mesg.setSubject(message_subject);
	
protected static voidusage(int returnValue)
Stub for providing help on usage You can write a longer help than this, certainly.

		System.err.println("Usage: Sender2 [-t to][-c cc][-f from][-s subj] file ...");
		System.exit(returnValue);