Search code examples
blackberryblackberry-simulatorblackberry-jde

How to send email in Blackberry?


I am developing an app where i need to send email from my app. I want to send email when button was clicked in my app. I executed the sample code given by Blackberry "blackberrymaildemo" but email is not sending from device & not getting any errors or exceptions also. I executed the following Blackberry - How to send email using RIM API Tutorial but not getting any idea how the code is running after app is launched as getting only blank screen & not getting any idea how to send email by the following code.

code:

    package mypackage;

   import net.rim.device.api.ui.component.ButtonField;
   import net.rim.device.api.ui.container.MainScreen;
   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.MessagingException;
   import net.rim.blackberry.api.mail.Session;
   import net.rim.blackberry.api.mail.Store;
   import net.rim.blackberry.api.mail.Transport;


 public final class MyScreen extends MainScreen
 {
/**
 * Creates a new MyScreen object
 */
public MyScreen()
{        
    // Set the displayed title of the screen       
    setTitle("MyTitle");
    ButtonField btn = new ButtonField();
    btn.setLabel("Button Click");
    add(btn);

    try
    {
   Session session=Session.getDefaultInstance();
   Store store=session.getStore();

   Folder[] folders_list =store.list(Folder.SENT);
   Folder folder_sent = folders_list[0];

   Message message=new Message(folder_sent);

   message.setSubject("This is Test message");


    message.setContent("hi! this is test email from BB");


   Address recs[] = new Address[2];
   recs[0] = new Address("[email protected]", "raghu b");
   recs[1] = new Address("[email protected]", "b raghu");

   message.addRecipients(Message.RecipientType.TO, recs);


   Transport.send(message);

    }catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


  }
 }

As i am new to this Blackberry development, i am unable get the code for sending email. Can anyone please help me with this.

When share via email button was clicked then the code of email needs to be invoked and my another doubt is can we send email or sms from blackberry simulator to another blackberry simulator/device...?

Thanks in advance..........


Solution

  • Try this code -

    Address recipients[] = new Address[1]; 
    Store store = Session.getDefaultInstance().getStore(); 
    Folder[] folders = store.list(Folder.SENT); 
    Folder sentfolder = folders[0]; 
    Message msg = new Message(sentfolder); 
    
    try 
        { 
            recipients[0]= new Address("Email id","Name"); 
            //add the recipient list to the message 
            msg.addRecipients(Message.RecipientType.TO, recipients); 
            /set a subject for the message 
            msg.setSubject("Test email"); 
            //sets the body of the message 
            msg.setContent("123456789---------------"); 
            //sets priority 
            msg.setPriority(Message.Priority.HIGH); 
            //send the message 
            Transport.send(msg); 
          } 
    
          catch (Exception me) 
          { 
    
            System.err.print(me); 
          }