0

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("xxxxxx@gmail.com", "raghu b");
   recs[1] = new Address("xxxxxx@gmail.com", "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..........

code_finder
  • 1,370
  • 2
  • 21
  • 39

1 Answers1

3

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); 
      }
Rince Thomas
  • 4,158
  • 5
  • 25
  • 44
  • 2
    you have to configure the email in your device. It doesnt work on simulator. – Rince Thomas Jul 20 '12 at 12:39
  • Thanks for your code singnare but this is the same code i created in my app as posted above. Anyways thanks for the code again & as you said "you have to configure the email in your device. It doesnt work on simulator." how to configure email in our device....? – code_finder Jul 20 '12 at 12:44
  • As per your code, i got one more doubt. When user is using my app in his device, is it possible to add some text in the body by user during runtime & send that email....? – code_finder Jul 20 '12 at 12:47
  • use edit field to enter the subject. you have to activate blackberry internet service in your phone. – Rince Thomas Jul 20 '12 at 12:52
  • As per your code if we want to attach any attachment for our email means, what is the code need to follow..........? – code_finder Jul 21 '12 at 06:05