views:

827

answers:

2

hey i am building an application in which user can send an email to a person. The user enters the email id of the person to whom email is to be sent in a Edit field and then presses a send button the email must be delivered with an attachment.

how can i do it??????

i m really confused after googling. can someone tell me the exact way

also,cant i send email from simulator if my cod file is unsigned

thanks in advance

+2  A: 

Try this.

     Address[] address = new Address[1];
                    try {
                        address[0] = new Address(email,name);
                    } catch (AddressException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    byte[] data = readFile();
                    Multipart multipart = new Multipart();
                    SupportedAttachmentPart attach = new SupportedAttachmentPart(multipart,
                            "application/x-example", "test.txt", data);
                    multipart.addBodyPart(attach);
                    Message msg = new Message();
                    // add the recipient list to the message
                    try {
                        msg.addRecipients(Message.RecipientType.TO, address);
                         // set a subject for the message
                        msg.setSubject("Mail from mobile");
                        msg.setContent(multipart);
                    } catch (MessagingException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }


                    try {
                        Transport.send(msg);
                    } catch (MessagingException e) {
                        System.out.println(e.getMessage());
                    }
private static byte[] readFile() {
    String fName ="file:///store/home/user/test.txt";
    byte[] data = null;
    FileConnection fconn = null;
    DataInputStream is = null;
    try {
            fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
            is = fconn.openDataInputStream();             
            data = IOUtilities.streamToBytes(is);
    } catch (IOException e) {
            System.out.println(e.getMessage());
    } finally {
            try {
                    if (null != is)

                            is.close();
                    if (null != fconn)
                            fconn.close();
            } catch (IOException e) {
                    System.out.println(e.getMessage());
            }
    }
    return data;
}
Vivart
hey vivart i earlier also tried this method but does this method sends mail from simulator when my app is unsigned????all lines execute without any exception but still no mail is send.
SWATI
for sending emails from and to simulator you have to use ESS(Email Services Simulator).I have tested this code on real device and its working.
Vivart
hey thanks i also tested this code on device but attachment cant be send....what does filename indicates ---- i used the path of file(it gets stored in memory card) but still attachment cant be send!!!if i dont provide the location/path of file then it will fetch default location???
SWATI
i used this path"file:///SDCard/BlackBerry/documents/myfolder/myfile.txt"
SWATI
i exactly worked as this code instead of file path i gave the name of attachmentbut still no success!!!!!!!!!!!!!
SWATI
see my edited post its full code .
Vivart
i have tested just now and its working.
Vivart
thanks so much Vivartu really helped me, infact i was doing such a stupid mistake
SWATI
A: 

Here's a working example of creating a new email and bringing it up for review before sending it from my project BBSSH. The dialog/popup you would not need and could delete. In this example we're taking a bitmap as an argument, and converting it to a PNG which we attach to the email. Different content type would be attached similarly.

You should be able to do just about anything from the simulator if the code is unsigned; however I think that emails won't actually get sent since the simulator itself has no connection to an actual mail server.

/**
  * Sends feedback, optionally including the provided bitmap as an attachement.
  *
  * it is the caller's responsibility to ensure that this is invoked
  * in a properly synchronized manner.
  *
  * @param screenshot - if not null, this function prompts
  * the user to include the screenshot as an attachment. 
  */
 public static void sendFeedback(Bitmap screenshot) {
  ResourceBundle b = ResourceBundle.getBundle(BBSSHRResource.BUNDLE_ID,
    BBSSHRResource.BUNDLE_NAME);
  try {
   Multipart mp = new Multipart();
   Message msg = new Message();
   Address[] addresses = {new Address("[email protected]", "Recipient Name")};
   if (screenshot == null || Dialog.ask(Dialog.D_YES_NO,
     b.getString(BBSSHRResource.MSG_FEEDBACK_INCLUDE_SCREENSHOT), Dialog.YES) == Dialog.NO) {
   } else {
    PNGEncodedImage img = PNGEncodedImage.encode(screenshot);
    SupportedAttachmentPart pt = new SupportedAttachmentPart(mp, img.getMIMEType(),
      "bbssh-screen.png", img.getData());
    mp.addBodyPart(pt);
    msg.setContent(mp);
   }
   msg.addRecipients(RecipientType.TO, addresses);
   msg.setSubject("BBSSH Feedback");
   Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, new MessageArguments(msg));
  } catch (AddressException ex) {
   Logger.getLogger().log(10, "Unable to send feedback: " + ex.getMessage());
  } catch (MessagingException ex) {
   Logger.getLogger().log(10, "Unable to send feedback: " + ex.getMessage());
  }

 }

If you wanted to send the message instead of bringing it up for review instead of Invoke.invokeApplication you would use Transport.send(msg);

Marc Paradise