Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Scout/Tutorial/3.7/IMAP Step-by-Step"

< Scout‎ | Tutorial‎ | 3.7
(New page: == HowTo make a Mail-Client/Server-Application with Eclipse Scout ==)
 
(HowTo make a Mail-Client/Server-Application with Eclipse Scout)
Line 1: Line 1:
== HowTo make a Mail-Client/Server-Application with Eclipse Scout ==
+
== HowTo make a Mail-Client/Server-Application with Eclipse Scout ==
 +
 
 +
*Ctrl-N for a new Project.
 +
*In the wizard choose Scout->Scout Project
 +
*Choose a Project Name, Postfix and Alias.
 +
**Project Name: com.example.mail
 +
**Project Postfix: core
 +
**Project Alias: (automaticly set to mailCore)
 +
*We just want to make a swt application, so we uncheck com.example.mail.ui.swing.core.
 +
*Click to "Finish" and wait a moment for the generation of the projects.<br>Now you have 4 projects with some source to start with.
 +
*Change the perspective to "Scout".
 +
*In the "Scout Projects" tree navigate to the yellow box "com.example.mail.client.core" -> Desktop -> Outlines and right-click "New Outline...".
 +
*In the Name field enter "Mail" and hit Return. The Translation box will open. At least enter in the default field "Mail".<br>Type Name and Outline can be left to default. Hit Return or click "Finish".
 +
*Open the oultine folder and right on the "Child Pages" folder "New Page...".
 +
*Choose "AbstractPageWithNodes" and hit return.
 +
*In the Name field enter "Mail" and double hit Return. The default values are ok for now.
 +
*Right-Click on the Variables folder under "MailNodePage" -> "New Property Bean".
 +
*Name: "folderId" / Bean Type: String -> return
 +
*Repeat the last two steps again with
 +
**"folderName" / String
 +
**"messageCount" / int
 +
**"unreadMessageCount" / int
 +
*Click on the MailNodePage and in the "Scout Oject Properties" view click on the last link "Get Bookmark Identifier". Accept the next Messagebox with yes.
 +
*In the editor pane the cursor will jump to the newly created method and there you can overwrite the null-return with following:<br>getFolderId()
 +
*In the "Scout Oject Properties"-view click on the link "Exec Init Page". Accept the next Messagebox with yes.
 +
*In the editor pane the cursor will jump to the newly created method and there you can enter following code:<br>setTableVisible(false);
 +
*Repeat the last two steps again with following methods:
 +
::Exec Decorate Cell
 +
<source lang="java">
 +
int totalCount=getMessageCount();
 +
if(totalCount>=0){
 +
  cell.setTooltipText("Folder contains "+totalCount+" messages");
 +
}
 +
else{
 +
  cell.setTooltipText(null);
 +
}
 +
</source>
 +
::Exec Page Activated
 +
<source lang="java">
 +
if(getFolderId()!=null){
 +
  if(getDetailForm()==null){
 +
    MailForm f=new MailForm();
 +
    f.setFolderId(getFolderId());
 +
    setDetailForm(f);
 +
    f.startView();
 +
  }
 +
}
 +
</source>
 +
::Exec Create Child Pages
 +
<source lang="java">
 +
for(Object[] row: SERVICES.getService(IMailProcessService.class).getChildFolders(getFolderId())){
 +
  MailNodePage page=new MailNodePage();
 +
  page.setFolderId((String)row[0]);
 +
  page.setFolderName((String)row[1]);
 +
  page.setMessageCount((Integer)row[2]);
 +
  page.setUnreadMessageCount((Integer)row[4]);
 +
  pageList.add(page);
 +
}
 +
</source>
 +
::Exec Set Page Status
 +
<source lang="java">
 +
getOutline().setStatus(new ProcessingStatus(""+getMessageCount()+" message(s)",IStatus.INFO));
 +
</source>
 +
*With Ctrl-o you can resolve some imports. The missing Classes you can ignore for now. You'll create them in a moment.
 +
*Right-Click on the folder "Forms" -> "New Form..."
 +
*Name: Mail<br>Uncheck "Create form ID" the other fields you can leave the default values --> Next
 +
*Now you could drag'n'drop the permissions and services if you had more the one module. In this example you have just one module (core) so you can just uncheck classes you don't need.<br>Uncheck the handlers (you'll create an other in a moment) and the permissions (you don't need permissions in this example) -> Finish
 +
*Right-Click on Variables -> "New Property Bean..."
 +
*Name: "folderId" / Bean Type: String -> Finish
 +
*Right-Click on the MainBox under the MailForm -> "New Form Field..."
 +
*In this dialog you get a list of common used Fields. For this example you start with an "uncommon" field. So click on the checkbox at the bottom "show all fields" and enter or search "AbstractSplitBox". -> Next
 +
*Name: empty<br>Type Name: SplitBoxField -> Finish
 +
*Click on the newly created SplitBoxField.
 +
*In the "Scout Oject Properties"-view uncheck "Split Horizontal" and change the "Splitter Position" to 0.3.
 +
*Right-Click on the SplitBoxField -> "New Form Field..."
 +
*Choose the "Table Field" -> Next
 +
*Name: empty<br>Type Name: MailTableField -> Finish
 +
*Right-Click on the SplitBoxField -> "New Form Field..."
 +
*Click on the checkbox "show all fields" and enter or search "AbstractMailField". -> Next
 +
*Name: empty<br>Type Name: MailField -> Finish
 +
*Open the MailTableField folder and the Table folder with a click on the "+".
 +
*Right-Click on the Columns folder -> "New Column..."
 +
*Choose "String Column" -> Next
 +
*Name: empty / Type Name: FolderIdColumn -> Finish
 +
*Repeat 36./37. with following:
 +
**"Long Column" / Name: empty / Type Name: UidColumn
 +
**"String Column" / Name: "From" / Type Name: FromColumn
 +
**"String Column" / Name: "Subject" / Type Name: SubjectColumn
 +
**"Date Column" / Name: "Received Date" / Type Name: ReceivedDateColumn
 +
**"Integer Column" / Name: "Size" / Type Name: SizeColumn
 +
*FolderIdColumn: In the "Scout Oject Properties"-view uncheck "Displayable" and check "Primary Key".
 +
*Same as 40. for the UidColumn.
 +
*FromColumn, SubjectColumn: Change "Width" to 200.
 +
*ReceivedDateColumn: Change "Width" to 100. In "Format" enter "EE dd.MM.yyyy HH:mm". Check "Has Time".
 +
*SizeColumn: Change "Width" to 50.
 +
*MailField: Change "Grid H" to 10.
 +
*Change back to the Table: Click to "Exec Rows Selected" and enter following (you'll get some errors, some can be resolved with Ctrl-O, the you'll be able to resolve lateron):
 +
<source lang="java">
 +
if(rows.length==1){
 +
  byte[] data=SERVICES.getService(IMailProcessService.class).getMessage(getFolderId()(),getUidColumn().getValue(rows[0]));
 +
  if(data!=null){
 +
    try{
 +
      getMailField().setValue(new MimeMessage(null,new ByteArrayInputStream(data)));
 +
    }
 +
    catch(javax.mail.MessagingException e){
 +
      throw new ProcessingException(e.getMessage(),e);
 +
    }
 +
  }
 +
  else{
 +
    getMailField().setValue(null);
 +
  }
 +
}
 +
else{
 +
  getMailField().setValue(null);
 +
}
 +
</source>
 +
*Right-Click on the "Handlers" folder under the MailForm -> "New Handler..."
 +
*Choose "Form Handler" -> Next. Type Name: ViewHandler -> Finish
 +
*Open the Handlers folder and click on the ViewHandler. Click to "Exec Load" and enter following:
 +
<source lang="java">
 +
Object[][] data=SERVICES.getService(IMailProcessService.class).getMessageHeaders(getFolderId()());
 +
getMailTableField().getTable().replaceRowsByMatrix(data);
 +
</source>
 +
*Go to: com.example.mail.service.core -> "Process Services" -> MailProcessService.
 +
*Click on the IMailProcessService and in the editor pane delete all method descriptions and enter following:
 +
<source lang="java">
 +
/**
 +
* @return fullName (ID), name, count, newCount, unreadCount
 +
*/
 +
public Object[][] getChildFolders(String parentName) throws ProcessingException;
 +
 
 +
/**
 +
* @return folderId (ID), uid (ID), from, subject, date, size
 +
*/
 +
public Object[][] getMessageHeaders(String folderId) throws ProcessingException;
 +
 
 +
/**
 +
* @return MimeMessage
 +
*/
 +
public byte[] getMessage(String folderd,long uid) throws ProcessingException;
 +
</source>
 +
*Click on the MailProcessService and in the editor pane delete all method descriptions and enter following:
 +
<source lang="java">
 +
private ImapAdapter createImapAdapter(){
 +
  ImapPrincipal principal=null;
 +
  for(ImapPrincipal p : Subject.getSubject(AccessController.getContext()).getPrincipals(ImapPrincipal.class)){
 +
    principal=p;
 +
  }
 +
  if(principal != null){
 +
    ImapAdapter adapter=new ImapAdapter(principal.getHost(), 143, principal.getUsername(), principal.getPassword());
 +
    adapter.setUseSSL(true);
 +
    return adapter;
 +
  }
 +
  return null;
 +
}
 +
 
 +
/**
 +
* @return fullName (ID), name, count, newCount, unreadCount
 +
*/
 +
public Object[][] getChildFolders(String parentName) throws ProcessingException{
 +
  ImapAdapter adapter=createImapAdapter();
 +
  try{
 +
    ArrayList<Object[]> rows=new ArrayList<Object[]>();
 +
    adapter.connect();
 +
    javax.mail.Folder parent=parentName!=null ? adapter.getStore().getFolder(parentName) : adapter.getStore().getDefaultFolder();
 +
    if(parent!=null && parent.exists()){
 +
      for(javax.mail.Folder f : parent.list()){
 +
        if(f.exists()){
 +
          if(f instanceof javax.mail.UIDFolder){
 +
            if((f.getType() & javax.mail.Folder.HOLDS_MESSAGES)!=0){
 +
              Object[] row=new Object[]{f.getFullName(),f.getName(),f.getMessageCount(),f.getNewMessageCount(),f.getUnreadMessageCount()};
 +
              rows.add(row);
 +
            }
 +
            else if((f.getType() & javax.mail.Folder.HOLDS_FOLDERS)!=0){
 +
              Object[] row=new Object[]{f.getFullName(),f.getName(),0,0,0};
 +
              rows.add(row);
 +
            }
 +
          }
 +
        }
 +
      }
 +
    }
 +
    return rows.toArray(new Object[0][0]);
 +
  }
 +
  catch(ProcessingException p){
 +
    throw p;
 +
  }
 +
  catch(Throwable t){
 +
    throw new ProcessingException(t.getMessage(),t);
 +
  }
 +
  finally{
 +
    adapter.closeConnection();
 +
  }
 +
}
 +
 
 +
/**
 +
* @return folderName (ID), uid (ID), from, subject, date, size
 +
*/
 +
public Object[][] getMessageHeaders(String folderName) throws ProcessingException{
 +
  ImapAdapter adapter=createImapAdapter();
 +
  try{
 +
    ArrayList<Object[]> rows=new ArrayList<Object[]>();
 +
    adapter.connect();
 +
    javax.mail.Folder f=adapter.getStore().getFolder(folderName);
 +
    if(f!=null && f.exists()){
 +
      if(f instanceof javax.mail.UIDFolder){
 +
        f.open(javax.mail.Folder.READ_ONLY);
 +
        try{
 +
          javax.mail.Message[] msgs=f.getMessages();
 +
          javax.mail.FetchProfile fp=new javax.mail.FetchProfile();
 +
          fp.add(javax.mail.FetchProfile.Item.ENVELOPE);
 +
          f.fetch(msgs,fp);
 +
          for(javax.mail.Message m: msgs){
 +
            if(!m.isExpunged()){
 +
              Object[] row=new Object[]{folderName, ((javax.mail.UIDFolder)f).getUID(m),firstAddress(m.getFrom()),m.getSubject(),m.getSentDate(),m.getSize()};
 +
              rows.add(row);
 +
            }
 +
          }
 +
        }
 +
        finally{
 +
          f.close(false);
 +
        }
 +
      }
 +
    }
 +
    return rows.toArray(new Object[0][0]);
 +
  }
 +
  catch(ProcessingException p){
 +
    throw p;
 +
  }
 +
  catch(Throwable t){
 +
    throw new ProcessingException(t.getMessage(),t);
 +
  }
 +
  finally{
 +
    adapter.closeConnection();
 +
  }
 +
}
 +
 
 +
public byte[] getMessage(String folderId,long uid) throws ProcessingException{
 +
  ImapAdapter adapter=createImapAdapter();
 +
  try{
 +
    adapter.connect();
 +
    javax.mail.Folder f=adapter.getStore().getFolder(folderId);
 +
    if(f!=null && f.exists()){
 +
      if(f instanceof javax.mail.UIDFolder){
 +
        f.open(javax.mail.Folder.READ_ONLY);
 +
        try{
 +
          javax.mail.Message m=((javax.mail.UIDFolder)f).getMessageByUID(uid);
 +
          if(m!=null && !m.isExpunged() && m instanceof javax.mail.internet.MimeMessage){
 +
            ByteArrayOutputStream out=new ByteArrayOutputStream();
 +
            m.writeTo(out);
 +
            return out.toByteArray();
 +
          }
 +
        }
 +
        finally{
 +
          f.close(false);
 +
        }
 +
      }
 +
    }
 +
    return null;
 +
  }
 +
  catch(ProcessingException p){
 +
    throw p;
 +
  }
 +
  catch(Throwable t){
 +
    throw new ProcessingException(t.getMessage(),t);
 +
  }
 +
  finally{
 +
    adapter.closeConnection();
 +
  }
 +
}
 +
 
 +
private String firstAddress(javax.mail.Address[] a){
 +
  if(a!=null && a.length>0 && a[0] instanceof InternetAddress){
 +
    InternetAddress ia=(InternetAddress)a[0];
 +
    String s=ia.getPersonal();
 +
    if(StringUtility.isNullOrEmpty(s)) s=ia.getAddress();
 +
    return s;
 +
  }
 +
  return null;
 +
}
 +
</source>

Revision as of 06:09, 16 March 2010

HowTo make a Mail-Client/Server-Application with Eclipse Scout

  • Ctrl-N for a new Project.
  • In the wizard choose Scout->Scout Project
  • Choose a Project Name, Postfix and Alias.
    • Project Name: com.example.mail
    • Project Postfix: core
    • Project Alias: (automaticly set to mailCore)
  • We just want to make a swt application, so we uncheck com.example.mail.ui.swing.core.
  • Click to "Finish" and wait a moment for the generation of the projects.
    Now you have 4 projects with some source to start with.
  • Change the perspective to "Scout".
  • In the "Scout Projects" tree navigate to the yellow box "com.example.mail.client.core" -> Desktop -> Outlines and right-click "New Outline...".
  • In the Name field enter "Mail" and hit Return. The Translation box will open. At least enter in the default field "Mail".
    Type Name and Outline can be left to default. Hit Return or click "Finish".
  • Open the oultine folder and right on the "Child Pages" folder "New Page...".
  • Choose "AbstractPageWithNodes" and hit return.
  • In the Name field enter "Mail" and double hit Return. The default values are ok for now.
  • Right-Click on the Variables folder under "MailNodePage" -> "New Property Bean".
  • Name: "folderId" / Bean Type: String -> return
  • Repeat the last two steps again with
    • "folderName" / String
    • "messageCount" / int
    • "unreadMessageCount" / int
  • Click on the MailNodePage and in the "Scout Oject Properties" view click on the last link "Get Bookmark Identifier". Accept the next Messagebox with yes.
  • In the editor pane the cursor will jump to the newly created method and there you can overwrite the null-return with following:
    getFolderId()
  • In the "Scout Oject Properties"-view click on the link "Exec Init Page". Accept the next Messagebox with yes.
  • In the editor pane the cursor will jump to the newly created method and there you can enter following code:
    setTableVisible(false);
  • Repeat the last two steps again with following methods:
Exec Decorate Cell
int totalCount=getMessageCount();
if(totalCount>=0){
  cell.setTooltipText("Folder contains "+totalCount+" messages");
}
else{
  cell.setTooltipText(null);
}
Exec Page Activated
if(getFolderId()!=null){
  if(getDetailForm()==null){
    MailForm f=new MailForm();
    f.setFolderId(getFolderId());
    setDetailForm(f);
    f.startView();
  }
}
Exec Create Child Pages
for(Object[] row: SERVICES.getService(IMailProcessService.class).getChildFolders(getFolderId())){
  MailNodePage page=new MailNodePage();
  page.setFolderId((String)row[0]);
  page.setFolderName((String)row[1]);
  page.setMessageCount((Integer)row[2]);
  page.setUnreadMessageCount((Integer)row[4]);
  pageList.add(page);
}
Exec Set Page Status
getOutline().setStatus(new ProcessingStatus(""+getMessageCount()+" message(s)",IStatus.INFO));
  • With Ctrl-o you can resolve some imports. The missing Classes you can ignore for now. You'll create them in a moment.
  • Right-Click on the folder "Forms" -> "New Form..."
  • Name: Mail
    Uncheck "Create form ID" the other fields you can leave the default values --> Next
  • Now you could drag'n'drop the permissions and services if you had more the one module. In this example you have just one module (core) so you can just uncheck classes you don't need.
    Uncheck the handlers (you'll create an other in a moment) and the permissions (you don't need permissions in this example) -> Finish
  • Right-Click on Variables -> "New Property Bean..."
  • Name: "folderId" / Bean Type: String -> Finish
  • Right-Click on the MainBox under the MailForm -> "New Form Field..."
  • In this dialog you get a list of common used Fields. For this example you start with an "uncommon" field. So click on the checkbox at the bottom "show all fields" and enter or search "AbstractSplitBox". -> Next
  • Name: empty
    Type Name: SplitBoxField -> Finish
  • Click on the newly created SplitBoxField.
  • In the "Scout Oject Properties"-view uncheck "Split Horizontal" and change the "Splitter Position" to 0.3.
  • Right-Click on the SplitBoxField -> "New Form Field..."
  • Choose the "Table Field" -> Next
  • Name: empty
    Type Name: MailTableField -> Finish
  • Right-Click on the SplitBoxField -> "New Form Field..."
  • Click on the checkbox "show all fields" and enter or search "AbstractMailField". -> Next
  • Name: empty
    Type Name: MailField -> Finish
  • Open the MailTableField folder and the Table folder with a click on the "+".
  • Right-Click on the Columns folder -> "New Column..."
  • Choose "String Column" -> Next
  • Name: empty / Type Name: FolderIdColumn -> Finish
  • Repeat 36./37. with following:
    • "Long Column" / Name: empty / Type Name: UidColumn
    • "String Column" / Name: "From" / Type Name: FromColumn
    • "String Column" / Name: "Subject" / Type Name: SubjectColumn
    • "Date Column" / Name: "Received Date" / Type Name: ReceivedDateColumn
    • "Integer Column" / Name: "Size" / Type Name: SizeColumn
  • FolderIdColumn: In the "Scout Oject Properties"-view uncheck "Displayable" and check "Primary Key".
  • Same as 40. for the UidColumn.
  • FromColumn, SubjectColumn: Change "Width" to 200.
  • ReceivedDateColumn: Change "Width" to 100. In "Format" enter "EE dd.MM.yyyy HH:mm". Check "Has Time".
  • SizeColumn: Change "Width" to 50.
  • MailField: Change "Grid H" to 10.
  • Change back to the Table: Click to "Exec Rows Selected" and enter following (you'll get some errors, some can be resolved with Ctrl-O, the you'll be able to resolve lateron):
if(rows.length==1){
  byte[] data=SERVICES.getService(IMailProcessService.class).getMessage(getFolderId()(),getUidColumn().getValue(rows[0]));
  if(data!=null){
    try{
      getMailField().setValue(new MimeMessage(null,new ByteArrayInputStream(data)));
    }
    catch(javax.mail.MessagingException e){
      throw new ProcessingException(e.getMessage(),e);
    }
  }
  else{
    getMailField().setValue(null);
  }
}
else{
  getMailField().setValue(null);
}
  • Right-Click on the "Handlers" folder under the MailForm -> "New Handler..."
  • Choose "Form Handler" -> Next. Type Name: ViewHandler -> Finish
  • Open the Handlers folder and click on the ViewHandler. Click to "Exec Load" and enter following:
Object[][] data=SERVICES.getService(IMailProcessService.class).getMessageHeaders(getFolderId()());
getMailTableField().getTable().replaceRowsByMatrix(data);
  • Go to: com.example.mail.service.core -> "Process Services" -> MailProcessService.
  • Click on the IMailProcessService and in the editor pane delete all method descriptions and enter following:
/**
 * @return fullName (ID), name, count, newCount, unreadCount
 */
public Object[][] getChildFolders(String parentName) throws ProcessingException;
 
/**
 * @return folderId (ID), uid (ID), from, subject, date, size
 */
public Object[][] getMessageHeaders(String folderId) throws ProcessingException;
 
/**
 * @return MimeMessage
 */
public byte[] getMessage(String folderd,long uid) throws ProcessingException;
  • Click on the MailProcessService and in the editor pane delete all method descriptions and enter following:
private ImapAdapter createImapAdapter(){
  ImapPrincipal principal=null;
  for(ImapPrincipal p : Subject.getSubject(AccessController.getContext()).getPrincipals(ImapPrincipal.class)){
    principal=p;
  }
  if(principal != null){
    ImapAdapter adapter=new ImapAdapter(principal.getHost(), 143, principal.getUsername(), principal.getPassword());
    adapter.setUseSSL(true);
    return adapter;
  }
  return null;
}
 
/**
 * @return fullName (ID), name, count, newCount, unreadCount
 */
public Object[][] getChildFolders(String parentName) throws ProcessingException{
  ImapAdapter adapter=createImapAdapter();
  try{
    ArrayList<Object[]> rows=new ArrayList<Object[]>();
    adapter.connect();
    javax.mail.Folder parent=parentName!=null ? adapter.getStore().getFolder(parentName) : adapter.getStore().getDefaultFolder();
    if(parent!=null && parent.exists()){
      for(javax.mail.Folder f : parent.list()){
        if(f.exists()){
          if(f instanceof javax.mail.UIDFolder){
            if((f.getType() & javax.mail.Folder.HOLDS_MESSAGES)!=0){
              Object[] row=new Object[]{f.getFullName(),f.getName(),f.getMessageCount(),f.getNewMessageCount(),f.getUnreadMessageCount()};
              rows.add(row);
            }
            else if((f.getType() & javax.mail.Folder.HOLDS_FOLDERS)!=0){
              Object[] row=new Object[]{f.getFullName(),f.getName(),0,0,0};
              rows.add(row);
            }
          }
        }
      }
    }
    return rows.toArray(new Object[0][0]);
  }
  catch(ProcessingException p){
    throw p;
  }
  catch(Throwable t){
    throw new ProcessingException(t.getMessage(),t);
  }
  finally{
    adapter.closeConnection();
  }
}
 
/**
 * @return folderName (ID), uid (ID), from, subject, date, size
 */
public Object[][] getMessageHeaders(String folderName) throws ProcessingException{
  ImapAdapter adapter=createImapAdapter();
  try{
    ArrayList<Object[]> rows=new ArrayList<Object[]>();
    adapter.connect();
    javax.mail.Folder f=adapter.getStore().getFolder(folderName);
    if(f!=null && f.exists()){
      if(f instanceof javax.mail.UIDFolder){
        f.open(javax.mail.Folder.READ_ONLY);
        try{
          javax.mail.Message[] msgs=f.getMessages();
          javax.mail.FetchProfile fp=new javax.mail.FetchProfile();
          fp.add(javax.mail.FetchProfile.Item.ENVELOPE);
          f.fetch(msgs,fp);
          for(javax.mail.Message m: msgs){
            if(!m.isExpunged()){
              Object[] row=new Object[]{folderName, ((javax.mail.UIDFolder)f).getUID(m),firstAddress(m.getFrom()),m.getSubject(),m.getSentDate(),m.getSize()};
              rows.add(row);
            }
          }
        }
        finally{
          f.close(false);
        }
      }
    }
    return rows.toArray(new Object[0][0]);
  }
  catch(ProcessingException p){
    throw p;
  }
  catch(Throwable t){
    throw new ProcessingException(t.getMessage(),t);
  }
  finally{
    adapter.closeConnection();
  }
}
 
public byte[] getMessage(String folderId,long uid) throws ProcessingException{
  ImapAdapter adapter=createImapAdapter();
  try{
    adapter.connect();
    javax.mail.Folder f=adapter.getStore().getFolder(folderId);
    if(f!=null && f.exists()){
      if(f instanceof javax.mail.UIDFolder){
        f.open(javax.mail.Folder.READ_ONLY);
        try{
          javax.mail.Message m=((javax.mail.UIDFolder)f).getMessageByUID(uid);
          if(m!=null && !m.isExpunged() && m instanceof javax.mail.internet.MimeMessage){
            ByteArrayOutputStream out=new ByteArrayOutputStream();
            m.writeTo(out);
            return out.toByteArray();
          }
        }
        finally{
          f.close(false);
        }
      }
    }
    return null;
  }
  catch(ProcessingException p){
    throw p;
  }
  catch(Throwable t){
    throw new ProcessingException(t.getMessage(),t);
  }
  finally{
    adapter.closeConnection();
  }
}
 
private String firstAddress(javax.mail.Address[] a){
  if(a!=null && a.length>0 && a[0] instanceof InternetAddress){
    InternetAddress ia=(InternetAddress)a[0];
    String s=ia.getPersonal();
    if(StringUtility.isNullOrEmpty(s)) s=ia.getAddress();
    return s;
  }
  return null;
}

Back to the top