Developement/Java

SendMailUsage v1.0 (SMTP를 이용한 파일첨부,텍스트,HTML 메일발송)

highheat 2006. 5. 1. 11:06
" +
                          "Here is line 2.";
          msg.setText(mytxt);

            // Alternate form
          msg.setContent(mytxt, "text/plain");

    }

    public static void setTextContent(Message msg, String contentStr) throws MessagingException {

            // Set message content
          msg.setText(contentStr);
          msg.setContent(contentStr,"text/plain");

   //try {
     // Alternate form
        //msg.setContent(new String(contentStr.getBytes("euc-kr"),"8859_1"), "text/plain");
 //}
 //catch (UnsupportedEncodingException uee) {
 // uee.printStackTrace();
 //}
  }

 

    // A simple multipart/mixed e-mail. Both body parts are text/plain.
  public static void setMultipartContent(Message msg) throws MessagingException {
      // Create and fill first part
      MimeBodyPart p1 = new MimeBodyPart();
      p1.setText("This is part one of a test multipart e-mail.");

        // Create and fill second part
      MimeBodyPart p2 = new MimeBodyPart();
      // Here is how to set a charset on textual content
      p2.setText("This is the second part", "euc-kr");

        // Create the Multipart.  Add BodyParts to it.
      Multipart mp = new MimeMultipart();
      mp.addBodyPart(p1);
      mp.addBodyPart(p2);

        // Set Multipart as the message's content
      msg.setContent(mp);
  }

 

    // Set a file as an attachment.  Uses JAF FileDataSource.
  public static void setFileAsAttachment(Message msg, String filename)
           throws MessagingException {

        // Create and fill first part
      MimeBodyPart p1 = new MimeBodyPart();
      p1.setText("This is part one of a test multipart e-mail." +
                  "The second part is file as an attachment");

        // Create second part
      MimeBodyPart p2 = new MimeBodyPart();

        // Put a file in the second part
      FileDataSource fds = new FileDataSource(filename);
      p2.setDataHandler(new DataHandler(fds));
      p2.setFileName(fds.getName());

        // Create the Multipart.  Add BodyParts to it.
      Multipart mp = new MimeMultipart();
      mp.addBodyPart(p1);
      mp.addBodyPart(p2);

        // Set Multipart as the message's content
      msg.setContent(mp);
  }

 

    // Set a single part html content.
  // Sending data of any type is similar.
  public static void setHTMLContent(Message msg, String contentStr) throws MessagingException {
      msg.setDataHandler(new DataHandler(new HTMLDataSource(contentStr)));
  }


/*
   * Inner class to act as a JAF datasource to send HTML e-mail content
   */
  static class HTMLDataSource implements DataSource {
      private String html;

        public HTMLDataSource(String htmlString) {
          html = htmlString;
      }

        // Return html string in an InputStream.
      // A new stream must be returned each time.
      public InputStream getInputStream() throws IOException {
          if (html == null) throw new IOException("Null HTML");
          return new ByteArrayInputStream(html.getBytes());
      }

        public OutputStream getOutputStream() throws IOException {
          throw new IOException("This DataHandler cannot write HTML");
      }

        public String getContentType() {
          return "text/html";
      }

        public String getName() {
          return "JAF text/html dataSource to send e-mail only";
      }
  } // End of inner class

} //End of class