1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.net.smtp;
  17. /***
  18. * This class is used to construct a bare minimum
  19. * acceptable header for an email message. To construct more
  20. * complicated headers you should refer to RFC 822. When the
  21. * Java Mail API is finalized, you will be
  22. * able to use it to compose fully compliant Internet text messages.
  23. * <p>
  24. * The main purpose of the class is to faciliatate the mail sending
  25. * process, by relieving the programmer from having to explicitly format
  26. * a simple message header. For example:
  27. * <pre>
  28. * writer = client.sendMessageData();
  29. * if(writer == null) // failure
  30. * return false;
  31. * header =
  32. * new SimpleSMTPHeader("foobar@foo.com", "foo@bar.com" "Just testing");
  33. * header.addCC("bar@foo.com");
  34. * header.addHeaderField("Organization", "Foobar, Inc.");
  35. * writer.write(header.toString());
  36. * writer.write("This is just a test");
  37. * writer.close();
  38. * if(!client.completePendingCommand()) // failure
  39. * return false;
  40. * </pre>
  41. * <p>
  42. * <p>
  43. * @author Daniel F. Savarese
  44. * @see SMTPClient
  45. ***/
  46. public class SimpleSMTPHeader
  47. {
  48. private String __subject, __from, __to;
  49. private StringBuffer __headerFields, __cc;
  50. /***
  51. * Creates a new SimpleSMTPHeader instance initialized with the given
  52. * from, to, and subject header field values.
  53. * <p>
  54. * @param from The value of the <code>From:</code> header field. This
  55. * should be the sender's email address.
  56. * @param to The value of the <code>To:</code> header field. This
  57. * should be the recipient's email address.
  58. * @param subject The value of the <code>Subject:</code> header field.
  59. * This should be the subject of the message.
  60. ***/
  61. public SimpleSMTPHeader(String from, String to, String subject)
  62. {
  63. __to = to;
  64. __from = from;
  65. __subject = subject;
  66. __headerFields = new StringBuffer();
  67. __cc = null;
  68. }
  69. /***
  70. * Adds an arbitrary header field with the given value to the article
  71. * header. These headers will be written before the From, To, Subject, and
  72. * Cc fields when the SimpleSMTPHeader is convertered to a string.
  73. * An example use would be:
  74. * <pre>
  75. * header.addHeaderField("Organization", "Foobar, Inc.");
  76. * </pre>
  77. * <p>
  78. * @param headerField The header field to add, not including the colon.
  79. * @param value The value of the added header field.
  80. ***/
  81. public void addHeaderField(String headerField, String value)
  82. {
  83. __headerFields.append(headerField);
  84. __headerFields.append(": ");
  85. __headerFields.append(value);
  86. __headerFields.append('\n');
  87. }
  88. /***
  89. * Add an email address to the CC (carbon copy or courtesy copy) list.
  90. * <p>
  91. * @param address The email address to add to the CC list.
  92. ***/
  93. public void addCC(String address)
  94. {
  95. if (__cc == null)
  96. __cc = new StringBuffer();
  97. else
  98. __cc.append(", ");
  99. __cc.append(address);
  100. }
  101. /***
  102. * Converts the SimpleSMTPHeader to a properly formatted header in
  103. * the form of a String, including the blank line used to separate
  104. * the header from the article body. The header fields CC and Subject
  105. * are only included when they are non-null.
  106. * <p>
  107. * @return The message header in the form of a String.
  108. ***/
  109. public String toString()
  110. {
  111. StringBuffer header = new StringBuffer();
  112. if (__headerFields.length() > 0)
  113. header.append(__headerFields.toString());
  114. header.append("From: ");
  115. header.append(__from);
  116. header.append("\nTo: ");
  117. header.append(__to);
  118. if (__cc != null)
  119. {
  120. header.append("\nCc: ");
  121. header.append(__cc.toString());
  122. }
  123. if (__subject != null)
  124. {
  125. header.append("\nSubject: ");
  126. header.append(__subject);
  127. }
  128. header.append('\n');
  129. header.append('\n');
  130. return header.toString();
  131. }
  132. }