Tutorials Blackberry Sending Email with Attachment in Blackberry

Sending Email with Attachment in Blackberry

This tutorial explains how to send email with attachment in blackberry. Before sending email, make sure that your blackberry device is configured with mail application.

import net.rim.blackberry.api.mail.Address;
import net.rim.blackberry.api.mail.Folder;
import net.rim.blackberry.api.mail.Message;
import net.rim.blackberry.api.mail.Multipart;
import net.rim.blackberry.api.mail.Session;
import net.rim.blackberry.api.mail.SupportedAttachmentPart;
import net.rim.blackberry.api.mail.TextBodyPart;
import net.rim.blackberry.api.mail.Transport;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class MailDetailsPopup extends PopupScreen implements FieldChangeListener {
private VerticalFieldManager verticalFieldManager = null; 
private ButtonField send = null;
private ButtonField cancel = null;
private EditField toAddress = null;
private EditField quoteName = null;
//private EditField subject = null;
private EditField msg = null;

 public MailDetailsPopup() {
  super(new VerticalFieldManager(), Field.FOCUSABLE);

  verticalFieldManager = new VerticalFieldManager (Manager.VERTICAL_SCROLL);

  toAddress = new EditField();
  verticalFieldManager.add(new LabelField("To:"));
  verticalFieldManager.add(new SeparatorField());
  verticalFieldManager.add(toAddress);

  quoteName = new EditField(null,"",50,0);
  verticalFieldManager.add(new LabelField("Quote Name:"));
  verticalFieldManager.add(new SeparatorField());
  verticalFieldManager.add(quoteName);

  msg = new EditField();
  verticalFieldManager.add(new LabelField("Message:"));
  verticalFieldManager.add(new SeparatorField());
  verticalFieldManager.add(msg);

  HorizontalFieldManager btnMgr = new HorizontalFieldManager(Manager.FIELD_HCENTER);  
  cancel = new ButtonField("Cancel");
  send = new ButtonField("Send");
  cancel.setChangeListener(this);
  send.setChangeListener(this);

  btnMgr.add(cancel);
  btnMgr.add(send);
  verticalFieldManager.add(btnMgr);  
  add(verticalFieldManager);

 }
 public void fieldChanged(Field field, int arg1) {
  if(field == cancel){
   UiApplication.getUiApplication().popScreen(
         UiApplication.getUiApplication().getActiveScreen());
  }
  else if(field == send){
   new Thread(new Runnable() {
       public void run() {
        sendMail();
       }

    private void sendMail() {
     Multipart mp = new Multipart();
              //data for the content of the file
              String fileData = getHTML();              
              String email = toAddress.getText();              
              String messageData = msg.getText();           
              String subject = quoteName.getText();              
                 //create the file
               SupportedAttachmentPart sap = new SupportedAttachmentPart(mp,"text/html",                              subject+".html", fileData.getBytes());

               TextBodyPart tbp = new TextBodyPart(mp, messageData);

               //add the file to the multi part
               mp.addBodyPart(tbp);
               mp.addBodyPart(sap);

               //create a message in the sent items folder
               Folder folders[] = Session.getDefaultInstance().getStore().list(Folder.SENT);

               Message message = new Message(folders[0]);
               message.setSubject(subject);
               //add recipients to the message and send
               try {
                    Address toAdd = new Address(email,email);
                    Address toAdds[] = new Address[1];
                    toAdds[0] = toAdd;
                    message.addRecipients(Message.RecipientType.TO,toAdds);
                    message.setContent(mp);

                    Transport.send(message);
               } catch (Exception e) {
                    Dialog.inform(e.toString());
               }
    }

    private String getHTML() {
     String str = "sample It is sexy. ";
     return str;
    }
     }).start();

   UiApplication.getUiApplication().popScreen(             UiApplication.getUiApplication().getActiveScreen());
  }
 }
}
Total
0
Shares
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Anonymous
Anonymous
10 years ago

I got the piece of code which I have been searching. it is really a nice blog. thanks for all of u guys for putting efforts for helping for guys like us. beautifully presented.

Anonymous
Anonymous
10 years ago

hey neel how far is it possible to create an html email in blackberry. newer blackberrys support retrieving html mail. il in blackberry. newer blackberrys support retrieving html mail.

javatechig
javatechig
10 years ago
Reply to  Anonymous

Newer version of blackberry have the capability of retrieving HTML mails, since blackberry 6.0. But RIM still doesn’t provide API support for application developers to do the same. Hope to get the same in the next RIM SDK release.

Previous Post
j2me_tutorials_javatechig_featured_image

How does double Buffering works in J2ME ?

Next Post
Blackberry Tutorials

Importing existing blackberry keys to eclipse

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All