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

前端 未结 2 604
执笔经年
执笔经年 2020-12-21 15:02

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 the

2条回答
  •  悲&欢浪女
    2020-12-21 15:39

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

提交回复
热议问题