KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > javax > mail > Part

javax.mail
Interface Part

All Known Subinterfaces:
MimePart
All Known Implementing Classes:
BodyPart, Message, MimeBodyPart, MimeMessage
See Also:
Top Examples, Source Code

public void addHeader(String header_name,
                      String header_value)
               throws MessagingException
See Also:
IllegalStateException, IllegalWriteException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[693]Process mail message
By Anonymous on 2004/03/08 19:14:11  Rate
public boolean processMailMessage ( Message message )  {  
     Calendar today = Calendar.getInstance (  ) ; 
     try {  
       // Get the header information 
       String subject = message.getSubject (  ) ; 
       String dateString = "unknown date"; 
       String to 
              =  (  ( InternetAddress ) message.getAllRecipients (  )  [ 0 ]  ) .getPersonal (  ) ; 
       String toEmail 
              =  (  ( InternetAddress ) message.getAllRecipients (  )  [ 0 ]  ) .getAddress (  ) ; 
       String from =  (  ( InternetAddress ) message.getFrom (  )  [ 0 ]  ) .getPersonal (  ) ; 
       String email =  (  ( InternetAddress ) message.getFrom (  )  [ 0 ]  ) .getAddress (  ) ; 
  
  
       if  ( to==null )    to = toEmail; 
       if  ( from==null )  from = email; 
  
  
       java.util.Date date=message.getSentDate (  ) ; 
  
  
       // download only today's emails 
       Calendar mDate = Calendar.getInstance (  ) ; 
       if ( date!=null )  {  
         dateString = date.toString (  ) ; 
         mDate.setTime ( date ) ; 
         if ( mDate.get ( Calendar.DAY_OF_MONTH )  <  
            today.get ( Calendar.DAY_OF_MONTH ) -1 ) return false; 
        }  
  
  
       // Get the message 
       Part messagePart=message; 
       Object content=messagePart.getContent (  ) ; 
       String contentType = null; 
       String msg = null; 
  
  
       // Look for content-type: text/plain 
       if  ( content instanceof Multipart )  {  
         for ( int i=0;i <  (  ( Multipart ) content ) .getCount (  ) ;i++ )  {  
           messagePart= (  ( Multipart ) content ) .getBodyPart ( i ) ; 
           contentType = messagePart.getContentType (  ) ; 
           if  ( contentType.startsWith ( "text/plain" )  ) break; 
          }  
        }  
       msg = readMsg ( messagePart ) ; 
       int emailId = saveEmail ( dateString,from,subject,msg ) ; 
  
  
       // now get other content-types and attachments 
       if  ( content instanceof Multipart )  {  
         for ( int i=1;i <  (  ( Multipart ) content ) .getCount (  ) ;i++ )  {  
           messagePart= (  ( Multipart ) content ) .getBodyPart ( i ) ; 
           contentType=messagePart.getContentType (  ) ; 
           if ( ! ( contentType.startsWith ( "text/plain" )  )  )  {  
               //this is not binary msg, do someting about it 
               .... 
            }  
          }  
        }  
      }  
     catch  ( Exception e )  {  
       System.err.println ( e ) ; 
      }  
     return true; 
    }  
  
  
   private String readMsg ( Part messagePart )  {  
     String message = ""; 
     try {  
       String contentType=messagePart.getContentType (  ) ; 
       if  ( contentType.startsWith ( "text/plain" )  )  {  
         InputStream is = messagePart.getInputStream (  ) ; 
         BufferedReader reader = 
                         new BufferedReader ( new InputStreamReader ( is )  ) ; 
         String line = reader.readLine (  ) ; 
         while ( line!=null )  {  
           message = message + line; 
           line = reader.readLine (  ) ; 
          }  
        }  
      } catch ( Exception e )  {  
       System.err.println ( e ) ; 
      }  
     return message; 
    }  
  
  
  
 //addHeader


public static final String ATTACHMENT
See Also:
setDisposition(java.lang.String), getDisposition()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Enumeration getAllHeaders()
                          throws MessagingException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Object getContent()
                  throws IOException,
                         MessagingException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String getContentType()
                      throws MessagingException
See Also:
DataHandler
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public DataHandler getDataHandler()
                           throws MessagingException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String getDescription()
                      throws MessagingException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String getDisposition()
                      throws MessagingException
See Also:
getFileName(), INLINE, ATTACHMENT
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String getFileName()
                   throws MessagingException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String[] getHeader(String header_name)
                   throws MessagingException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public InputStream getInputStream()
                           throws IOException,
                                  MessagingException
See Also:
getDataHandler()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int getLineCount()
                 throws MessagingException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Enumeration getMatchingHeaders(String[] header_names)
                               throws MessagingException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Enumeration getNonMatchingHeaders(String[] header_names)
                                  throws MessagingException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int getSize()
            throws MessagingException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final String INLINE
See Also:
setDisposition(java.lang.String), getDisposition()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean isMimeType(String mimeType)
                   throws MessagingException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void removeHeader(String header_name)
                  throws MessagingException
See Also:
IllegalStateException, IllegalWriteException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setContent(Object obj,
                       String type)
                throws MessagingException
See Also:
IllegalStateException, IllegalWriteException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setContent(Multipart mp)
                throws MessagingException
See Also:
IllegalStateException, IllegalWriteException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setDataHandler(DataHandler dh)
                    throws MessagingException
See Also:
IllegalStateException, IllegalWriteException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setDescription(String description)
                    throws MessagingException
See Also:
IllegalStateException, IllegalWriteException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setDisposition(String disposition)
                    throws MessagingException
See Also:
setFileName(java.lang.String), INLINE, ATTACHMENT, IllegalStateException, IllegalWriteException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setFileName(String filename)
                 throws MessagingException
See Also:
IllegalStateException, IllegalWriteException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setHeader(String header_name,
                      String header_value)
               throws MessagingException
See Also:
IllegalStateException, IllegalWriteException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setText(String text)
             throws MessagingException
See Also:
IllegalStateException, IllegalWriteException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void writeTo(OutputStream os)
             throws IOException,
                    MessagingException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags