Swati
Swati

Reputation: 2918

j2me/BlackBerry - How to send Email with Attachment from Application?

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

Upvotes: 2

Views: 4276

Answers (2)

Vivart
Vivart

Reputation: 15313

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

Upvotes: 2

Marc Paradise
Marc Paradise

Reputation: 1939

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

Upvotes: 0

Related Questions