如何在Blackberry中发送电子邮件?

时间:2012-07-20 12:32:07

标签: blackberry blackberry-simulator blackberry-jde

我正在开发一个应用程序,我需要从我的应用程序发送电子邮件。我想在我的应用程序中点击按钮时发送电子邮件。我执行了Blackberry “blackberrymaildemo”给出的示例代码,但电子邮件不是从设备&也没有得到任何错误或例外。我执行了以下 Blackberry - How to send email using RIM API Tutorial,但在启动应用程序后只知道代码是如何运行的,因为只获得了空白屏幕&不知道如何通过以下代码发送电子邮件。

代码:

    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();
    }


  }
 }

由于我是Blackberry开发的新手,我无法获得发送电子邮件的代码。任何人都可以帮我这个。

当点击通过电子邮件分享按钮时,需要调用电子邮件的代码,我的另一个疑问是我们可以从黑莓模拟器发送电子邮件或短信到另一个黑莓模拟器/设备......?

提前致谢..........

1 个答案:

答案 0 :(得分:3)

试试这段代码 -

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); 
      }