Solved

Sending mail using java - for Nushi

Posted on 2001-07-15
3
221 Views
Last Modified: 2006-11-17
Hi!
I want a java program that sends emails.
Thank you!!!  
0
Comment
Question by:rzvika
  • 2
3 Comments
 
LVL 10

Expert Comment

by:Nushi
ID: 6283148
i mailed it to u in the last question.
do you need it again?

Nushi.
0
 
LVL 10

Accepted Solution

by:
Nushi earned 100 total points
ID: 6283149
here is the answer which i gave u before.
and thank you for the points.

ask me here if something isnt clear enought for u.
Nushi.

application:
http://www.javacommerce.com/jforge/JMessenger/index.html

source:
http://www.javacommerce.com/jforge/JMessenger/JMsgSource.htm

or take a look at this class that i wrote for u:

this will do the job for you.
class + documnetation for each variable and method

/**
* This class serve as a util class for mail options
* Use this class to define mail properties.
*
* use the function to set the fileds you want and then call sendMessage
* function.
*
*
*
* @see javax.mail.*;
* @see jacax.mail.internet.*;
*
* @author Nushi
*/

import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class NushiMail {
 
  //////////////////////////////////////
  //          MAIL SERVER     PROPERTIES          //
  //////////////////////////////////////
 
  //fields for message
  private Multipart          g_multiPart=new MimeMultipart();     //"Container" for diffrent message
parts
 
  //fields for addresses
  private String g_host                    ="localhost";     //mail server
  private String g_message               =null;     //the body message
  private String g_subject               =null;     //the body message
  private InternetAddress[] g_from     =null;     //from adress
  private InternetAddress[] g_to          =null;     //array for To adresses
  private InternetAddress[] g_cc         =null;     //array for CC adresses
  private InternetAddress[] g_bcc          =null;     //array for BCC adresses
  //delimeter between adresses
  private static String ADDRESS_DELIMETERS = " ,;";
 
  /** mail host server field identifier*/
  public static final int FIELD_HOST          =1000;
  /** from address field identifier */
  public static final int FIELD_FROM          =1001;
  /** TO address field identifier */
  public static final int FIELD_TO          =1002;
  /** CC address field identifier */
  public static final int FIELD_CC          =1003;
  /** BCC address field identifier */
  public static final int FIELD_BCC          =1004;
  /** MEssage field identifier */
  public static final int FIELD_MESSAGE     =1005;
  /** Subject field identifier */
  public static final int FIELD_SUBJECT     =1006;
  /** DELIMETER field identifier */
  public static final int FIELD_DELIMETER     =1007;

 
  //////////////////////////////////////////////////////////
  //                      ADDRESSES SECTION                         //
  //////////////////////////////////////////////////////////
 
  /**
   * Set field
   
   * @param p_fields          field in message
   * @param p_text          string which conatins values
   */
  public void setField(int p_fields , String p_text){
     
      try{
          switch(p_fields){
             
              case FIELD_HOST:
                  g_host = p_text;    
                  break;
                 
              case FIELD_MESSAGE:
                  g_message = p_text;    
                  break;
                 
              case FIELD_FROM:
                  g_from = convertToAddress(p_text);    
                  break;
                 
              case FIELD_SUBJECT:
                  g_subject = p_text;
                  break;
                 
              case FIELD_TO:
                  g_to = convertToAddress(p_text);
                  break;
                 
              case FIELD_CC:
                  g_cc = convertToAddress(p_text);
                  break;
                 
              case FIELD_BCC:
                  g_bcc = convertToAddress(p_text);
                  break;
                 
              case FIELD_DELIMETER:
                  ADDRESS_DELIMETERS = p_text;
                  break;
              }
          }catch(Exception e){
          System.out.println("Exception while trying to set field "+p_fields);
          }
      }
 
  /**
   * Get field by its identifier
   *
   * @param p_identifier      identifier of field which we need
   * @return                    field which we asked for
   */
  public InternetAddress[] getAddresses(int p_identifier){
     
      switch(p_identifier){
         
          case FIELD_TO:
              return g_to;    
             
          case FIELD_CC:
              return g_cc;    
             
          case FIELD_BCC:
              return g_bcc;    
          }
      return null;
      }
 
  /**
   * Get field
   * @return          mail server host
   */
  public String getHost(){
      return g_host;    
      }
 
  /**
   * Get message
   * @return          the body message
   */
  public String getMessage(){
      return g_message;    
      }
 
  /**
   * Get Subject of message
   * @return          the body message
   */
  public String getSubject(){
      return g_subject;    
      }
 
 
  /**
   * Sets a new Delimeter between addresses
   * Default is ","
   *
   * @param p_del          new delimeter
   */
  public void setDelimeter(String p_del){
      ADDRESS_DELIMETERS = p_del;
      }
 
  /**
   * Convert string to InternetAddress[].
   * the function gets a string of addresses seperated by delimeter
   * and parse the addresses from it
   *
   * @param  p_string          string with new data
   * @return array
   */
  public InternetAddress[] convertToAddress(String p_string){
      StringTokenizer          l_strToken=null;
      InternetAddress[]     l_reply=null;
     
      l_strToken     =new StringTokenizer(p_string , ADDRESS_DELIMETERS , false);
      l_reply          =new InternetAddress[l_strToken.countTokens()];
     
      int ind=0;
      try{
          while(l_strToken.hasMoreElements())    
              l_reply[ind++] = new InternetAddress(l_strToken.nextToken());
         
          return l_reply;
          }catch(Exception e){
          System.out.println("Exception while trying to generate adresses "+e);
          }
      return null;
      }
 
 
  //////////////////////////////////////////////////////////
  //                      ATTACHMENTS SECTION                         //
  //////////////////////////////////////////////////////////
 
  /**
   * add file attachment to the message
   *
   * @param p_fileName     file name to attach to message
   */
  public void addAttachment(String p_fileName){
     
      //create attachment
      MimeBodyPart     l_newPart=new MimeBodyPart();
      DataSource          l_source =new FileDataSource(p_fileName);
     
      try{
          l_newPart.setDataHandler(new DataHandler(l_source));
          l_newPart.setFileName(p_fileName);
          g_multiPart.addBodyPart(l_newPart);
         
          }catch(Exception e){
          System.out.println("Exception in: addAttachment");
          }
      }
 
 
  //////////////////////////////////////////////////////////
  /**
   * This function will send an e-mail message
   */
  public void sendMessage(){
      //do some check before mailing:
      if (g_host == null){
          System.out.println("Please set a mail server first");
          return;
          }
     
      if (g_to == null){
          System.out.println("Field 'TO' was not filled");
          return;
          }
     
      Properties     l_props = System.getProperties();
      Session          l_session;
      Message          l_message;
     
      try{
          //set Session & msg
          l_props.put("mail.smtp.host" , getHost() );
          l_session=Session.getDefaultInstance( l_props , null);              
          l_message = new MimeMessage(l_session);

          g_from=convertToAddress(FROM_FIELD);
          //add addresses fields to the message
          if (g_from != null)
              l_message.addFrom(g_from);          
         
          if (g_to != null)
              l_message.addRecipients(Message.RecipientType.TO  , g_to);
         
          if (g_cc != null)
              l_message.addRecipients(Message.RecipientType.CC  , g_cc);

          if (g_bcc != null)
              l_message.addRecipients(Message.RecipientType.BCC , g_bcc);
         
          if (g_message != null)
              l_message.setText(g_message);
         
          if (g_subject != null)
              l_message.setSubject(g_subject);
         
          //l_message.setContent( g_multiPart);
          l_message.setContent(g_message , "text/plain");
          Transport.send(l_message);
         
          }catch(Exception e){
          System.out.println("Exception in: sendMessage "+e);
          }
      }
  }

for example :

NushiMail l_mail=new NushiMail();


...
//set fields and sent message send the mail
l_mail.setField(l_mail.FIELD_TO , l_to);
l_mail.setField(l_mail.FIELD_CC , l_cc);
l_mail.setField(l_mail.FIELD_SUBJECT , l_subject);
l_mail.setField(l_mail.FIELD_MESSAGE , l_message);
l_mail.sendMessage();


Nushi.
0
 

Author Comment

by:rzvika
ID: 6283159
BTW, do you know where is the package javax.activation ?
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now