Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

Sending mail using java - for Nushi

Hi!
I want a java program that sends emails.
Thank you!!!  
0
rzvika
Asked:
rzvika
  • 2
1 Solution
 
NushiCommented:
i mailed it to u in the last question.
do you need it again?

Nushi.
0
 
NushiCommented:
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
 
rzvikaAuthor Commented:
BTW, do you know where is the package javax.activation ?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now