1. /*
  2. * Copyright 2002-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. */
  17. package org.apache.tools.ant.taskdefs.email;
  18. import java.io.BufferedInputStream;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.IOException;
  22. import java.io.PrintStream;
  23. import java.util.Enumeration;
  24. import org.apache.tools.ant.BuildException;
  25. import org.apache.tools.mail.MailMessage;
  26. /**
  27. * Class responsible for sending email through raw protocol methods.
  28. *
  29. * @since Ant 1.5
  30. */
  31. class PlainMailer extends Mailer {
  32. /**
  33. * Sends the email using the apache MailMessage class.
  34. *
  35. * @see org.apache.tools.mail.MailMessage
  36. */
  37. public void send() {
  38. try {
  39. MailMessage mailMessage = new MailMessage(host, port);
  40. mailMessage.from(from.toString());
  41. Enumeration e;
  42. e = replyToList.elements();
  43. while (e.hasMoreElements()) {
  44. mailMessage.replyto(e.nextElement().toString());
  45. }
  46. e = toList.elements();
  47. while (e.hasMoreElements()) {
  48. mailMessage.to(e.nextElement().toString());
  49. }
  50. e = ccList.elements();
  51. while (e.hasMoreElements()) {
  52. mailMessage.cc(e.nextElement().toString());
  53. }
  54. e = bccList.elements();
  55. while (e.hasMoreElements()) {
  56. mailMessage.bcc(e.nextElement().toString());
  57. }
  58. if (subject != null) {
  59. mailMessage.setSubject(subject);
  60. }
  61. mailMessage.setHeader("Date", getDate());
  62. if (message.getCharset() != null) {
  63. mailMessage.setHeader("Content-Type", message.getMimeType()
  64. + "; charset=\"" + message.getCharset() + "\"");
  65. } else {
  66. mailMessage.setHeader("Content-Type", message.getMimeType());
  67. }
  68. PrintStream out = mailMessage.getPrintStream();
  69. message.print(out);
  70. e = files.elements();
  71. while (e.hasMoreElements()) {
  72. File file = (File) e.nextElement();
  73. attach(file, out);
  74. }
  75. mailMessage.sendAndClose();
  76. } catch (IOException ioe) {
  77. throw new BuildException("IO error sending mail", ioe);
  78. }
  79. }
  80. /**
  81. * Attaches a file to this email
  82. *
  83. * @param file The file to attache
  84. * @param out The message stream to add to
  85. * @throws IOException if errors occur
  86. */
  87. protected void attach(File file, PrintStream out)
  88. throws IOException {
  89. if (!file.exists() || !file.canRead()) {
  90. throw new BuildException("File \"" + file.getName()
  91. + "\" does not exist or is not "
  92. + "readable.");
  93. }
  94. if (includeFileNames) {
  95. out.println();
  96. String filename = file.getName();
  97. int filenamelength = filename.length();
  98. out.println(filename);
  99. for (int star = 0; star < filenamelength; star++) {
  100. out.print('=');
  101. }
  102. out.println();
  103. }
  104. int length;
  105. final int maxBuf = 1024;
  106. byte[] buf = new byte[maxBuf];
  107. FileInputStream finstr = new FileInputStream(file);
  108. try {
  109. BufferedInputStream in = new BufferedInputStream(finstr, buf.length);
  110. while ((length = in.read(buf)) != -1) {
  111. out.write(buf, 0, length);
  112. }
  113. } finally {
  114. finstr.close();
  115. }
  116. }
  117. }