0

我正在开发一个应用程序,我需要从我的应用程序发送电子邮件。我想在我的应用程序中单击按钮时发送电子邮件。我执行了黑莓“blackberrymaildemo”给出的示例代码,但电子邮件没有从设备发送,也没有收到任何错误或异常。我执行了以下 Blackberry - 如何使用 RIM API 教程发送电子邮件,但不知道应用程序启动后代码是如何运行的,因为只有空白屏幕并且不知道如何通过以下代码发送电子邮件。

代码:

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


  }
 }

由于我是这个黑莓开发的新手,我无法获得发送电子邮件的代码。谁能帮我解决这个问题。

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

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

4

1 回答 1

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); 
      }
于 2012-07-20T12:37:06.767 回答