Now days in almost every application user has to send email and share the contents such as score of any game etc..
Using the below code in any blackberry application one can easily do that.
Address[] address = new Address[1];
try
{
address[0] = new Address(email,name);
}
catch (AddressException e1)
{
e1.printStackTrace();
}
byte[] data = readFile();
Multipart multipart = new Multipart();
SupportedAttachmentPart attach ;
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)
{
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;
}
Have a look at this sample image:
No comments:
Post a Comment