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