001 /* 002 * Copyright 2001-2005 The Apache Software Foundation 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.apache.commons.net.smtp; 017 018 /*** 019 * This class is used to construct a bare minimum 020 * acceptable header for an email message. To construct more 021 * complicated headers you should refer to RFC 822. When the 022 * Java Mail API is finalized, you will be 023 * able to use it to compose fully compliant Internet text messages. 024 * <p> 025 * The main purpose of the class is to faciliatate the mail sending 026 * process, by relieving the programmer from having to explicitly format 027 * a simple message header. For example: 028 * <pre> 029 * writer = client.sendMessageData(); 030 * if(writer == null) // failure 031 * return false; 032 * header = 033 * new SimpleSMTPHeader("foobar@foo.com", "foo@bar.com" "Just testing"); 034 * header.addCC("bar@foo.com"); 035 * header.addHeaderField("Organization", "Foobar, Inc."); 036 * writer.write(header.toString()); 037 * writer.write("This is just a test"); 038 * writer.close(); 039 * if(!client.completePendingCommand()) // failure 040 * return false; 041 * </pre> 042 * <p> 043 * <p> 044 * @author Daniel F. Savarese 045 * @see SMTPClient 046 ***/ 047 048 public class SimpleSMTPHeader 049 { 050 private String __subject, __from, __to; 051 private StringBuffer __headerFields, __cc; 052 053 /*** 054 * Creates a new SimpleSMTPHeader instance initialized with the given 055 * from, to, and subject header field values. 056 * <p> 057 * @param from The value of the <code>From:</code> header field. This 058 * should be the sender's email address. 059 * @param to The value of the <code>To:</code> header field. This 060 * should be the recipient's email address. 061 * @param subject The value of the <code>Subject:</code> header field. 062 * This should be the subject of the message. 063 ***/ 064 public SimpleSMTPHeader(String from, String to, String subject) 065 { 066 __to = to; 067 __from = from; 068 __subject = subject; 069 __headerFields = new StringBuffer(); 070 __cc = null; 071 } 072 073 /*** 074 * Adds an arbitrary header field with the given value to the article 075 * header. These headers will be written before the From, To, Subject, and 076 * Cc fields when the SimpleSMTPHeader is convertered to a string. 077 * An example use would be: 078 * <pre> 079 * header.addHeaderField("Organization", "Foobar, Inc."); 080 * </pre> 081 * <p> 082 * @param headerField The header field to add, not including the colon. 083 * @param value The value of the added header field. 084 ***/ 085 public void addHeaderField(String headerField, String value) 086 { 087 __headerFields.append(headerField); 088 __headerFields.append(": "); 089 __headerFields.append(value); 090 __headerFields.append('\n'); 091 } 092 093 094 /*** 095 * Add an email address to the CC (carbon copy or courtesy copy) list. 096 * <p> 097 * @param address The email address to add to the CC list. 098 ***/ 099 public void addCC(String address) 100 { 101 if (__cc == null) 102 __cc = new StringBuffer(); 103 else 104 __cc.append(", "); 105 106 __cc.append(address); 107 } 108 109 110 /*** 111 * Converts the SimpleSMTPHeader to a properly formatted header in 112 * the form of a String, including the blank line used to separate 113 * the header from the article body. The header fields CC and Subject 114 * are only included when they are non-null. 115 * <p> 116 * @return The message header in the form of a String. 117 ***/ 118 public String toString() 119 { 120 StringBuffer header = new StringBuffer(); 121 122 if (__headerFields.length() > 0) 123 header.append(__headerFields.toString()); 124 125 header.append("From: "); 126 header.append(__from); 127 header.append("\nTo: "); 128 header.append(__to); 129 130 if (__cc != null) 131 { 132 header.append("\nCc: "); 133 header.append(__cc.toString()); 134 } 135 136 if (__subject != null) 137 { 138 header.append("\nSubject: "); 139 header.append(__subject); 140 } 141 142 header.append('\n'); 143 header.append('\n'); 144 145 return header.toString(); 146 } 147 } 148 149 150