FileDocCategorySizeDatePackage
Sender2.javaAPI DocExample3980Mon May 28 00:19:30 BST 2001None

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.6 2001/05/28 03:19:31 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 the Sender2 object


	     
	    

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

		// Create the Session object
		session = Session.getDefaultInstance(props, null);
		// session.setDebug(true);		// Verbose!
		
		// create a message
		mesg = new MimeMessage(session);
	
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);
	
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);