Click here to Skip to main content
15,889,876 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have using below code for attaching a file to e-mail in blackberry:
C#
if(field == attach)
{
	//create a multipart
	Multipart mp = new Multipart();
	
//data for the content of the file
			String fileData = "<html>just a simple test</html>";
			String messageData = "Mail Attachment Demo";
			
			//create the file
			SupportedAttachmentPart sap = new SupportedAttachmentPart(mp,"text/html","file.html","dsfgfdgg".getBytes());

			TextBodyPart tbp = new TextBodyPart(mp,messageData);

			//add the file to the multipart
			mp.addBodyPart(tbp);
			mp.addBodyPart(sap);

			//create a message in the sent items folder
			Folder folders[] = Session.getDefaultInstance().getStore().list(Folder.SENT);

			Message message = new Message(folders[0]);

			//add recipients to the message and send
			try {
			     Address toAdd = new Address("email_address@yahoo.com","Aman");
			     Address toAdds[] = new Address[1];
			     toAdds[0] = toAdd;
			     message.addRecipients(Message.RecipientType.TO,toAdds);
			     message.setContent(mp);

			     Transport.send(message);
			     Dialog.inform("Email sent : " +toAdd);
			} catch (Exception e) {
			     Dialog.inform(e.toString());
			}
		}

but i want to pick file from resource folder so plz help me regarding this
thanks in advance
Posted
Updated 27-Aug-12 9:02am
v4
Comments
Sandeep Mewara 27-Aug-12 6:57am    
Access/Extract image from resource file and then attach it?
AmanArora1987 28-Aug-12 7:37am    
I know that but i am unable to get the image from resource...

Below is the code to send Email with attachment, Image file is stored on SD card:

Java
if(field == Attach)
{
try 
{
fconn = (FileConnection) Connector.open("file:///store/home/user/documents/images.jpg" ,Connector.READ_WRITE); // image stored in device memory
				
if(fconn.exists())
{
inputStream=fconn.openInputStream();
byte[] data = IOUtilities.streamToBytes(inputStream);
inputStream.close();
fconn.close();                   
multipart = new Multipart();
SupportedAttachmentPart attach = new SupportedAttachmentPart(multipart,  "image/jpg", "images.jpg", data);
multipart.addBodyPart(attach);
Folder folders[] = Session.getDefaultInstance().getStore().list(Folder.SENT);
Message message = new Message(folders[0]);
try 
{
     Address toAdd = new Address("lib_aman@yahoo.com","Aman");
     Address toAdds[] = new Address[1];
     toAdds[0] = toAdd;						  message.addRecipients(Message.RecipientType.TO,toAdds);
message.setContent(multipart);
Transport.send(message);
Dialog.inform("Email sent : " +toAdd);
} 
catch (Exception e) 
{
	Dialog.inform(e.toString());
}
}
else
{
	Dialog.inform("File doesnot exist");
}
}
catch (Exception e) 
{
	Dialog.inform("Exception......................."+e);
}
}
 
Share this answer
 
C++
if(field == Attach)
		{
			try 
			{
				fconn = (FileConnection) Connector.open("res/images.jpg", Connector.READ_WRITE);
				if(fconn.exists())
				{
					inputStream=fconn.openInputStream();
					byte[] data = IOUtilities.streamToBytes(inputStream);
					inputStream.close();
					fconn.close();                  
					multipart = new Multipart();
					SupportedAttachmentPart attach = new SupportedAttachmentPart(multipart, ".txt/.jpg", "images.jpg", data);
					multipart.addBodyPart(attach);
					Folder folders[] = Session.getDefaultInstance().getStore().list(Folder.SENT);
					Message message = new Message(folders[0]);
					try 
					{
						Address toAdd = new Address("lib_aman@yahoo.com","Aman");
						Address toAdds[] = new Address[1];
						toAdds[0] = toAdd;
						message.addRecipients(Message.RecipientType.TO,toAdds);
						message.setContent(multipart);Transport.send(message);
						Dialog.inform("Email sent : " +toAdd);
					} 
					catch (Exception e) 
					{
						Dialog.inform(e.toString());
					}
				}
			}
			catch (IOException e) 
			{
				e.printStackTrace();
			}
		}

I am trying This code but it still not working, so kindly help me out for this.
 
Share this answer
 
v2
Comments
Sandeep Mewara 29-Aug-12 1:40am    
This is not an answer. Please use Improve Question link to edit/update your question at anytime.
AmanArora1987 29-Aug-12 3:41am    
i am using this code and my program is working, i get what i need...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900