I am trying to send an attachemnet in an email. I am able to do so the problem that i am having is that I am not able to get the original name and content type of the attachment.
Is there any way of extracting the same.
Public class Cls_SendEmail
{
public String subject {get; set;}
public String body {get; set;}
public String to {get; set;}
public String cc {get; set;}
private Order__c order;
private EmailTemplate emailTemplatebody;
public List<string> cc_addresses{get;set;}
private EmailTemplate__c emailTemplateObj;
public Attachment attachment {
get {
if (attachment == null)
attachment = new Attachment();
return attachment;
}
set;
}
// Constructor to populate instance of your object
public MiC_Cls_SendEmail(ApexPages.StandardController controller) {
this.order = (Order__c )controller.getRecord();
order = [SELECT name, ContactEmail__c ,case__r.contact.name, case__r.contact.email, Qc_contact__r.email, Case__r.Case_Subject__C, Order_Type__c , Exchanged_Through__c FROM Order__c
WHERE id = :ApexPages.CurrentPage().getParameters().get('id')];
/*
to = order.Mic_case__r.contact.email;
emailTemplatebody = [Select id, subject, body from EmailTemplate where
DeveloperName=:'ET_EmailInvoice'];
*/
try
{
emailTemplateObj = [Select Name, QC_Contact__r.email from EmailTemplate__c where Exchanged_Through__c =: order.Exchanged_Through__c
and Order_Type__c =: order.Order_Type__c];
System.debug('emailTemplateObj = ' + emailTemplateObj.Name );
to = emailTemplateObj.QC_Contact__r.email;
emailTemplatebody = [Select id, subject, body from EmailTemplate where DeveloperName=:emailTemplateObj.Name ];
System.debug('emailTemplatebody = ' + emailTemplatebody + 'emailTemplateObj.Name' +emailTemplateObj.Name);
subject = emailTemplatebody.subject;
order.EmailBodyFromAgent__c = body;
cc_addresses = new List<string>();
body = emailTemplatebody.body;
System.debug('Subject = ' +subject + 'body = ' + body);
order.case__r.contact.name);
}
catch(CalloutException e)
{
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'No Email Template is Available' );
ApexPages.addMessage(msg);
}
}
public pageReference send(){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toaddress = new String[]{};
toaddress.add(to);
email.setToAddresses(toaddress); //Use SOQL to retrieve addresses in the address
email.setBccSender(true);
if(cc.length() >0 && cc != null){
List<string> split_cc = new List<string>();
split_cc = cc.split(';');
for(string s: split_cc){
cc_addresses .add(s);
}
}
email.setCcAddresses(cc_addresses );
email.setTargetObjectId(order.case__r.contact.Id);
email.setWhatId(order.Id);
emailTemplatebody.body = body;
order.EmailBodyFromAgent__c = body;
// update emailTemplatebody;
email.setTemplateId(emailTemplatebody.id);
email.setSaveAsActivity(true);
// Create the email attachment
if( attachment != null)
{
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
// efa.setFileName(attachment.name);
efa.setFileName('file.docx');
efa.setBody(attachment.body);
efa.setContentType(attachment.ContentType);
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
}
Messaging.SendEmailResult [] res = Messaging.SendEmail(new Messaging.SingleEmailMessage[] {email});
for ( Messaging.sendEmailResult result : res ) {
if ( !res[0].isSuccess () ) {
System.debug ( result );
}
else{
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Email Sent Successfully' );
ApexPages.addMessage(msg);
}
}
return null;
}
}