Kalaimaan

Doing simple things to make great life

Send mail with Attachment using Exchange web service java

Posted by kalaimaan on February 9, 2009

Send mail with attachmen, here i am submiting the part of the code.  Collected the previous post and merge the attachment code given below

/**
*
* @return
*/
public void sendEmailItem(String[] to, String[] cc, String bcc[], String sub, String bodyText, List<String> attach)
{
String status = null;
this.to=  to;
this.cc = cc;
this.bcc = bcc;
this.sub = sub;
this.bodyText = bodyText;

MessageType message = getMessageType();

sendMessageToDraft(message);

List<String> ls = attach;
String list[] = null;
if (ls != null && ls.size() > 0)
list = attachFilesIntoDraftMessage(itemId, changeKey, ls);

itemId = list[0];
changeKey = list[1];
status = sendDraftMessage(itemId, changeKey);

System.out.println(“Status : ” + status);
}

/**
*
* @param id
* @param key
* @return
*/
private String[] attachFilesIntoDraftMessage(String id, String key, List<String> attachToDraft)
{
String[] st = new String[2];

NonEmptyArrayOfAttachmentsType arrayOfAttachmentsType = null;
try
{
arrayOfAttachmentsType = getEmailAttachmentContent(attachToDraft);
}
catch (Exception e1)
{
st[0] = id;
st[1] = key;
return st;
}

// Create the CreateAttachment request.
CreateAttachmentType reqCreateAttach = CreateAttachmentType.Factory.newInstance();

// Identify the item that will have the attachments.
ItemIdType item = ItemIdType.Factory.newInstance();

item.setId(id);
item.setChangeKey(key);

// Add the parent item to the request.
reqCreateAttach.setParentItemId(item);
reqCreateAttach.setAttachments(arrayOfAttachmentsType);

CreateAttachmentDocument document = CreateAttachmentDocument.Factory.newInstance();
document.setCreateAttachment(reqCreateAttach);

CreateAttachmentResponseDocument responseDocument = null;
try
{
responseDocument = exService.CreateAttachment(document, null, null, null, null);
}
catch (Exception e)
{
return st;
}

ArrayOfResponseMessagesType arrayOfResponseMessagesType = responseDocument.getCreateAttachmentResponse()
.getResponseMessages();

AttachmentInfoResponseMessageType[] attachmentInfoResponseMessageTypes = arrayOfResponseMessagesType
.getCreateAttachmentResponseMessageArray();

for (AttachmentInfoResponseMessageType type : attachmentInfoResponseMessageTypes)
{
if (type.getResponseClass() == ResponseClassType.SUCCESS)
{
st[0] = type.getAttachments().getFileAttachmentArray()[0].getAttachmentId().getRootItemId();
st[1] = type.getAttachments().getFileAttachmentArray()[0].getAttachmentId().getRootItemChangeKey();
break;
}
else
{
st[0] = id;
st[1] = key;
}
}

return st;
}

/**
*
* @param ls
* @return
* @throws Exception
*/
private NonEmptyArrayOfAttachmentsType getEmailAttachmentContent(List<String> ls) throws Exception
{
// Add attachments to the request.
NonEmptyArrayOfAttachmentsType arrayOfAttachmentsType = NonEmptyArrayOfAttachmentsType.Factory.newInstance();

int total = 0;
FileAttachmentType[] attachmentTypes = new FileAttachmentType[ls.size()];
for (int count = 0; count < ls.size(); count++)
{
attachmentTypes[count] = FileAttachmentType.Factory.newInstance();
File f = new File(ls.get(count).toString());
attachmentTypes[count].setName(f.getName());

// Get the size of the file
attachmentTypes[count].setContent(getBytesFromFile(f));

total += f.length();
}
arrayOfAttachmentsType.setFileAttachmentArray(attachmentTypes);

return arrayOfAttachmentsType;
}

/**
* Returns the contents of the file in a byte array.
*
* @param file
* @return
* @throws IOException
*/
private byte[] getBytesFromFile(File file) throws IOException
{
InputStream is = new FileInputStream(file);

// Get the size of the file
long length = file.length();

// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.

if (length > Integer.MAX_VALUE)
{
// File is too large
}

// Create the byte array to hold the data
byte[] bytes = new byte[(int) length];

// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length – offset)) >= 0)
{
offset += numRead;
}

// Ensure all the bytes have been read in
if (offset < bytes.length)
{
throw new IOException(“Could not completely read file ” + file.getName());
}

// Close the input stream and return bytes
is.close();

return bytes;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: