3

I've developed a custom inbound email handler in order to be used with email-to-case, but, when some criteria are met, I need to "bounce" a custom email to the sender indicating some steps to follow and I need to have the inbound message attached to this "bounce" email.

I'm facing some issues with it, My apex code is something similar to this:

private void sendWarningEmail(){

  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

  String[] toAddresses = new String[] {this.inboundEmail.fromAddress}; 
  mail.setToAddresses(toAddresses);
  mail.setBccSender(false);
  mail.setUseSignature(false);
  mail.setSenderDisplayName('Sender Name');

  EmailTemplate et = [select Id, Subject, HtmlValue, Body from EmailTemplate where DeveloperName=:'Template'];
  mail.setTemplateId(et.id);

   //dummy contact to be the target object
  Contact c = new Contact();
  c.Email = this.inboundEmail.fromAddress;
  mail.setTargetObjectId(c.Id);


  //attach the original mail message
  Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
  efa.setFileName(this.inboundEmail.subject);

  efa.setBody(Blob.valueof(this.inboundEmail.htmlBody));
  efa.setContentType('multipart/mixed');

  mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

  // Send the email you have created.
  Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

}

I'm not sure about what the problem can be, but even when the attachement is created and added to the "bounced" email, it is "corrupted" or not in the correct format, because it cannot be opened.

What am I missing?

I hope someone can shed some light on this.

  • Please clarify what you mean by "cannot be opened" (you originally wrote "can be opened", but I am 99% sure you meant "cannot" so I edited it). How does the file size of the attachment compare with the size of the original message? When you open the attachment explicitly with a text editor, what exactly do you see? – jkraybill Sep 30 '14 at 6:15
0

You don't show your inbound handler code, but unless the inbound body was saved as a 'multipart/mixed' ContentTyoe, I'd have expected it to be a Mime type of either 'HTML' or 'plaintext'.

That's what I'd be looking at, especially if you can't append their inbound message to the end of your outbound message body. I emphasize that since I don't see where you're sending any other attachments with this email.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.