如何以编程方式将图像文件或文本文件附加到Blackberry中的电子邮件? [英] How to programatically attach file either image or text file to the email in blackberry?

查看:64
本文介绍了如何以编程方式将图像文件或文本文件附加到Blackberry中的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用以下代码在Blackberry中将文件附加到电子邮件:

I have using below code for attaching a file to e-mail in blackberry:

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


但是我想从资源文件夹中选择文件,所以请PLZ帮助我解决这个问题
在此先感谢


but i want to pick file from resource folder so plz help me regarding this
thanks in advance

推荐答案

下面是发送带有附件的电子邮件的代码,图像文件存储在SD卡上:

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

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


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.


这篇关于如何以编程方式将图像文件或文本文件附加到Blackberry中的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆