Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Problems sending email javamail

Thread view: 
sickness - 16 May 2006 09:19 GMT
Hello
I'm using Eclipse and  Jboss (all last vers.)
I want to send an email using this common classes created by another
person

--MailMessenger.java--
[code]import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class MailMessenger
{
 private String stmpServer;
 private String popServer;
 private String popUser;
 private String popPassword;

 /**
  * Il costruttore crea un oggetto di tipo MailMessenger a partire dal
server smtp, dal server pop3,
  *  dallo user e dalla password
  * @param stmpServer java.lang.String
  * @param popServer java.lang.String
  * @param popUser  java.lang.String
  * @param popPassword  java.lang.String
  */
 public MailMessenger(String stmpServer, String popServer, String
popUser, String popPassword)
 {
   this.stmpServer = stmpServer;
   this.popServer = popServer;
   this.popUser = popUser;
   this.popPassword = popPassword;
 }

 /**
  * Il metodo effettua l' invio di un messaggio che può contenere
degli attachment
  * @param mm MailMessage
  */
 public void Send(MailMessage mm)
 {
   try
   {
     MailAttachment attachments[];
     Properties props = System.getProperties();

     // -- Attaching to default Session, or we could start a new one
--

     props.put("mail.smtp.host", stmpServer);
     Session session = Session.getDefaultInstance(props, null);

     // -- Create a new message --
     Message msg = new MimeMessage(session);

     // -- Set the FROM and TO fields --
     msg.setFrom(new InternetAddress(mm.GetFrom()));
     msg.setRecipients(Message.RecipientType.TO,
                       InternetAddress.parse(mm.GetTo(), false));

     //Matteo 22/09/2005: inserisco eventuali destinatari CC o BCC
     Address[] addrCC = mm.GetCC();
     if(addrCC != null && addrCC.length > 0)
       msg.setRecipients(Message.RecipientType.CC,addrCC);

     Address[] addrBCC = mm.GetBCC();
     if(addrBCC != null && addrBCC.length > 0)
       msg.setRecipients(Message.RecipientType.BCC,addrBCC);

     addrCC=null; addrBCC=null;
     ///////////////////////////////////////////////////////////////

     msg.setSubject(mm.GetSubject());

     // Create the message part
     BodyPart messageBodyPart = new MimeBodyPart();

     // Fill the message
     messageBodyPart.setText(mm.GetBody());

     Multipart multipart = new MimeMultipart();
     multipart.addBodyPart(messageBodyPart);

     attachments = mm.GetAttachments();
     if (attachments!=null) {
       // Part two is attachments
       for (int i=0;i<attachments.length;i++) {
         messageBodyPart = new MimeBodyPart();
         DataSource source = new
FileDataSource(attachments[i].GetFile());
         messageBodyPart.setDataHandler(new DataHandler(source));
         messageBodyPart.setFileName(attachments[i].GetFile());
         multipart.addBodyPart(messageBodyPart);
       }
     }

     // Put parts in message
     msg.setContent(multipart);

     // -- Set some other header information --
     msg.setSentDate(new Date());

     // -- Send the message --
     Transport.send(msg);

//     System.out.println("Message sent OK.");
   }
   catch (Exception ex)
   {
     ex.printStackTrace();
   }
 }[/code]

--- MailMessage.java ---
[code]import javax.mail.Address;
import javax.mail.internet.*;

public class MailMessage
{
 private String to;
 private String from;
 private String subject;
 private String body;
 private MailAttachment[] attachments;

 //destinatari di altro tipo
 private Address[] aIAddrCC;
 private Address[] aIAddrBCC;

/**
  * Il costruttore restituisce un oggetto MailMessage a partire dalla
specifica
  * del mittente, del destinatario,dell'oggetto,del corpo e degli
attachment
  * @param to String
  * @param from String
  * @param subject String
  * @param body String
  * @param attachments MailAttachment
  */
  public MailMessage(String to,
                     String from,
                     String subject,
                     String body,
                     MailAttachment attachments[])
 {
   this.to = to;
   this.from = from;
   this.subject = subject;
   this.body = body;
   this.attachments = attachments;
 }

/**
* Il costruttore restituisce un oggetto MailMessage con mittente,
destinatario,oggetto e corpo
* vuoti e array degli attachment nullo
*/
  public MailMessage()
  {
   to = "";
   from = "";
   subject = "";
   body = "";
   attachments = null;
  }

  /**
  * Il metodo imposta il mittente del messaggio di mail
  * @param to java.lang.String
  */
  public void SetTo(String to)
  {
        this.to = to;
  }

  /**
  *
  * @param cc -  stringa contenente gli indirizzi dei destinatari CC
separati da ",";
  */
  public void SetCC(String cc)
  {
    try
    {
     this.aIAddrCC = InternetAddress.parse(cc,false);

    }catch(AddressException ae)
    {
      System.out.println("Metodo SetCC(String cc)");
      ae.printStackTrace();
      this.aIAddrCC=null;
    }

  }

  /**
  *
  * @param bcc -  stringa contenente gli indirizzi dei destinatari BCC
separati da ",";
  */
  public void SetBCC(String bcc)
  {
    try
    {
     this.aIAddrBCC = InternetAddress.parse(bcc,false);

    }catch(AddressException ae)
    {
      System.out.println("Metodo SetBCC(String bcc)");
      ae.printStackTrace();
      this.aIAddrBCC=null;
    }

  }

  /**
  * Il metodo imposta il destinatario del messaggio di mail
  * @param from java.lang.String
  */
  public void SetFrom(String from)
  {
        this.from = from;
  }

  /**
  * Il metodo imposta l' oggetto del messaggio di mail
  * @param subject java.lang.String
  */
  public void SetSubject(String subject)
  {
        this.subject = subject;
  }

  /**
  * Il metodo imposta il corpo del messaggio di mail
  * @param body java.lang.String
  */
  public void SetBody(String body)
  {
        this.body = body;
  }

  /**
  * Il metodo imposta il destinatario del messaggio di mail
  * @param attachments MailAttachment[]
  */
  public void SetAttachments(MailAttachment[] attachments)
  {
        this.attachments = attachments;
  }

  /**
  * Il metodo imposta gli attachment di un messaggio intesi come array
di
  * stringhe che identificano i path dei file da appendere al
messaggio
  * @param attachments java.lang.String[]
  */
  public void SetAttachments(String[] attachments)
  {
        if (attachments==null)
         return;

        this.attachments = new MailAttachment[attachments.length];
        for (int i=0;i<attachments.length;i++)
        {
          this.attachments[i] = new MailAttachment(attachments[i]);
        }

  }

  /**
  * Il metodo restituisce il destinatario del messaggio di mail
  * @return java.lang.String
  */
  public String GetTo()
  {
        return to;
  }

  /**
  *
  * @return javax.mail.Address - array di oggetti di tipo
InternetAddress contenete gli indirizzi dei destinatari CC;
  */
  public Address[] GetCC()
  {
    return this.aIAddrCC;
  }

  /**
  *
  * @return javax.mail.Address - array di oggetti di tipo
InternetAddress contenete gli indirizzi dei destinatari BCC;
  */
  public Address[] GetBCC()
  {
    return this.aIAddrBCC;
  }

  /**
  * Il metodo restituisce il mittente del messaggio di mail
  * @return java.lang.String
  */
  public String GetFrom()
  {
        return from;
  }

  /**
  * Il metodo restituisce l' oggetto del messaggio di mail
  * @return java.lang.String
  */
  public String GetSubject()
  {
        return subject;
  }

  /**
  * Il metodo restituisce il corpo del messaggio di mail
  * @return java.lang.String
  */
  public String GetBody()
  {
        return body;
  }
   /**
  * Il metodo restituisce gli attachment del messaggio di mail
  * @return MailAttachment[]
  */
  public MailAttachment[] GetAttachments()
  {
        return attachments;
  }

 /**
  * Il medotodo stampa su stdout un messaggio di mail
  */
  public void Print()
  {
    System.out.println("MAIL MESSAGE");
    System.out.println("\tFROM: " + from );
    System.out.println("\tTO: " + to);
    System.out.println("\tCC: " + this.GetCCString());
    System.out.println("\tBCC: " + this.GetBCCString());
    System.out.println("\tSUBJECT: " + subject);
    System.out.println("\tBODY: " + body);
    if (attachments!=null)
    {
      for(int i=0;i<attachments.length;i++)
      {
        attachments[i].Print();
      }

    }
  }

  private String GetCCString()
  {
    String sAddresses = "";

    if(aIAddrCC != null)
    {
      for(int j=-1; ++j<this.aIAddrCC.length;)
      {
          sAddresses +=
((InternetAddress)(this.aIAddrCC[j])).getAddress() + ",";
      }
      sAddresses = sAddresses.substring(0,sAddresses.length() - 1);
    }

    return sAddresses;
  }

  private String GetBCCString()
  {
    String sAddresses = "";
    if(aIAddrBCC != null)
    {
      for(int j=-1; ++j<this.aIAddrBCC.length;)
      {
          sAddresses +=
((InternetAddress)(this.aIAddrBCC[j])).getAddress() + ",";
      }

      sAddresses = sAddresses.substring(0,sAddresses.length() - 1);
    }

    return sAddresses;
  }[/code]

I try now to send one email using these

[code]MailMessenger mr = new MailMessenger(ProjectConstants.SMTPServer,
ProjectConstants.POPServer, ProjectConstants.POPUser,
ProjectConstants.POPPassword);
       MailMessage mm = new MailMessage("bella.ciccio@tim.it",
"sintesi@mixer.com", "password recovery", "ecco la password", null);

       mr.Send(mm);[/code]

The constants defined in ProjectConstants are correctly setted to my
POP SMTP server
I've imported mail.jar and activation.jar
Jboss gives me this error !

[code]javax.servlet.ServletException: Unresolved compilation problems:
    The import javax.mail cannot be resolved
    The import javax.mail cannot be resolved
    The import javax.activation cannot be resolved
    The import javax.activation cannot be resolved
    The import javax.activation cannot be resolved
    Session cannot be resolved to a type
    Session cannot be resolved
    Message cannot be resolved to a type
    MimeMessage cannot be resolved to a type
    InternetAddress cannot be resolved to a type
    Message cannot be resolved
    InternetAddress cannot be resolved
    Address cannot be resolved to a type
    The method GetCC() is undefined for the type MailMessage
    Message cannot be resolved
    Address cannot be resolved to a type
    The method GetBCC() is undefined for the type MailMessage
    Message cannot be resolved
    BodyPart cannot be resolved to a type
    MimeBodyPart cannot be resolved to a type
    Multipart cannot be resolved to a type
    MimeMultipart cannot be resolved to a type
    MimeBodyPart cannot be resolved to a type
    DataSource cannot be resolved to a type
    FileDataSource cannot be resolved to a type
    DataHandler cannot be resolved to a type
    Transport cannot be resolved
    Session cannot be resolved to a type
    Session cannot be resolved to a type
    Message cannot be resolved to a type
    MimeMessage cannot be resolved to a type
    InternetAddress cannot be resolved to a type
    Message cannot be resolved
    InternetAddress cannot be resolved
    Address cannot be resolved to a type
    The method GetCC() is undefined for the type MailMessage
    Message cannot be resolved
    Address cannot be resolved to a type
    The method GetBCC() is undefined for the type MailMessage
    Message cannot be resolved
    BodyPart cannot be resolved to a type
    MimeBodyPart cannot be resolved to a type
    Multipart cannot be resolved to a type
    MimeMultipart cannot be resolved to a type
    MimeBodyPart cannot be resolved to a type
    DataSource cannot be resolved to a type
    FileDataSource cannot be resolved to a type
    DataHandler cannot be resolved to a type
    Transport cannot be resolved
    Store cannot be resolved to a type
    Folder cannot be resolved to a type
    Message cannot be resolved to a type
    Session cannot be resolved to a type
    Session cannot be resolved
    Folder cannot be resolved
    Message cannot be resolved to a type
    InternetAddress cannot be resolved to a type
    InternetAddress cannot be resolved to a type
    Part cannot be resolved to a type
    Multipart cannot be resolved to a type
    Multipart cannot be resolved to a type
    Multipart cannot be resolved to a type
    Part cannot be resolved to a type
    Multipart cannot be resolved to a type

    org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:294)
    org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
    com.sintesinet.common.persistence.PersistenceFilter.doFilter(PersistenceFilter.java:63)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

root cause

java.lang.Error: Unresolved compilation problems:
    The import javax.mail cannot be resolved
    The import javax.mail cannot be resolved
    The import javax.activation cannot be resolved
    The import javax.activation cannot be resolved
    The import javax.activation cannot be resolved
    Session cannot be resolved to a type
    Session cannot be resolved
    Message cannot be resolved to a type
    MimeMessage cannot be resolved to a type
    InternetAddress cannot be resolved to a type
    Message cannot be resolved
    InternetAddress cannot be resolved
    Address cannot be resolved to a type
    The method GetCC() is undefined for the type MailMessage
    Message cannot be resolved
    Address cannot be resolved to a type
    The method GetBCC() is undefined for the type MailMessage
    Message cannot be resolved
    BodyPart cannot be resolved to a type
    MimeBodyPart cannot be resolved to a type
    Multipart cannot be resolved to a type
    MimeMultipart cannot be resolved to a type
    MimeBodyPart cannot be resolved to a type
    DataSource cannot be resolved to a type
    FileDataSource cannot be resolved to a type
    DataHandler cannot be resolved to a type
    Transport cannot be resolved
    Session cannot be resolved to a type
    Session cannot be resolved to a type
    Message cannot be resolved to a type
    MimeMessage cannot be resolved to a type
    InternetAddress cannot be resolved to a type
    Message cannot be resolved
    InternetAddress cannot be resolved
    Address cannot be resolved to a type
    The method GetCC() is undefined for the type MailMessage
    Message cannot be resolved
    Address cannot be resolved to a type
    The method GetBCC() is undefined for the type MailMessage
    Message cannot be resolved
    BodyPart cannot be resolved to a type
    MimeBodyPart cannot be resolved to a type
    Multipart cannot be resolved to a type
    MimeMultipart cannot be resolved to a type
    MimeBodyPart cannot be resolved to a type
    DataSource cannot be resolved to a type
    FileDataSource cannot be resolved to a type
    DataHandler cannot be resolved to a type
    Transport cannot be resolved
    Store cannot be resolved to a type
    Folder cannot be resolved to a type
    Message cannot be resolved to a type
    Session cannot be resolved to a type
    Session cannot be resolved
    Folder cannot be resolved
    Message cannot be resolved to a type
    InternetAddress cannot be resolved to a type
    InternetAddress cannot be resolved to a type
    Part cannot be resolved to a type
    Multipart cannot be resolved to a type
    Multipart cannot be resolved to a type
    Multipart cannot be resolved to a type
    Part cannot be resolved to a type
    Multipart cannot be resolved to a type

    com.sintesinet.MailMessenger.MailMessenger.<init>(MailMessenger.java:4)
    com.sintesinet.jobnet.cv.action.PswRcvAction.pswRcv_step3(PswRcvAction.java:43)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
    org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
    com.sintesinet.common.persistence.PersistenceFilter.doFilter(PersistenceFilter.java:63)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
[/code]

I try to do only this
MailMessenger(ProjectConstants.SMTPServer, ProjectConstants.POPServer,
ProjectConstants.POPUser, ProjectConstants.POPPassword);
and jboss gives me the error too..

activation.jar and mail.jar are in the jboss4.0.3SP1 libraries, they're
added on  my webapp
I try also to add them in lib of my webapp but nothing

I don't understand what's the problem?
Someone have suggestions??

thanks a lot guys
bye
Andy Flowers - 16 May 2006 18:52 GMT
> Hello
> I'm using Eclipse and  Jboss (all last vers.)
[quoted text clipped - 14 lines]
> thanks a lot guys
> bye

Try removing the mail.jar & activation.jar you have added.

JBoss comes as a full J2EE product and includes mail.jar & activation.jar in
it's system libraries.

The cannot resolve error means it doesn't know which library to use I believe.
sickness - 17 May 2006 12:02 GMT
Hi Andy
thank a lot for your response
Yes Jboss already have mail.jar and activation.jar so I've just removed
the corrispondent jar in my webapp Lib folder.!.. but still it doesn't
work
I really don't know what's the problem
thanks

Andy Flowers ha scritto:

> > Hello
> > I'm using Eclipse and  Jboss (all last vers.)
[quoted text clipped - 21 lines]
>
> The cannot resolve error means it doesn't know which library to use I believe.
sickness - 17 May 2006 12:36 GMT
I've solved!

The problem is that the common classes for mail that I use were
compiled with other mail.jar and activation.jar instead these of
jboss4.0

So I've imported directly in my webapp the package with sources and
I've compiled one more time these with Jboss's jars.

now it works.
Thanks for all


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.