+ Start a Discussion
St57 MSt57 M 

Amazon SES

Hi All need a small help in
how to send email along with attachment  using Amazon SES from apex class?

I am able to send email using 'Action=SendEmail' (like http://salesforce.stackexchange.com/questions/18428/connection-between-awsamazon-web-service-ses-and-sales-force-using-apex-code-t)but unable to send email along with attachment.
Mert YALTIMert YALTI
//Define public string set
                public Set<String> emailListId=new Set<String>();

               //create a method mail creation method
		public myEmail(Id messageId,String BaseUrl,String toAddresses,String fromAddress,String subject, String body,List<String> AttachName, list<String> AttachContentType,List<Blob>AttachBody,String HTMLValue)
    	        {

	        this.toAddresses = toAddresses;
	        this.fromAddress = fromAddress;
	        this.subject = subject;
	        this.body = body;
	        this.AttachName = AttachName;
	        this.AttachContentType = AttachContentType;
	        this.AttachBody = AttachBody;
	        this.BaseUrl = BaseUrl;
	        this.HTMLValue = HTMLValue +'<img src=" '+this.BaseUrl+'/?MLId='+EncodingUtil.urlEncode(EncryptedKeys.Encrypt(messageId),'UTF-8') +'" height="1" width="1">';
	       
	        
	        this.emailListId.add(messageId);
	        this.att=([Select Name,id,Parentid,Body,BodyLength,ContentType From Attachment WHERE Parentid IN :this.emailListId]);
    	    
   		} 
//use the code given below for sending you email with you amazon ses configurations
		List<Attachment> attachments = YOUR QUERY
        HttpRequest httpReq = new HttpRequest();
        httpReq.setMethod(YOUR METHOD);
        httpReq.setEndpoint( YOUR END POINT); 
        //SET HTTP REQUEST's HEADERS IF NECESSARY    
        httpReq.setHeader(HEADER1 );       
        httpReq.setHeader(HEADER2);
        httpReq.setHeader(HEADER3); 
        
        
        List<String> attachContentType = new List<String>();
        List<String> attachName = new List<String>();
        List<Blob> attachBody = new List<Blob>();
        
        for(Attachment attachment : attachments){
            attachName.add(attachment.Name);
            attachContentType.add(attachment.ContentType);
            attachBody.add(attachment.Body);
        }
        
        myEmail newEmail = new myEmail(messageId,BaseUrl,recipient,sender,subject,body,attachName,attachContentType,attachBody,htmlBody); 
        httpReq.setBody(newEmail.encodedEmail);        
        Http http = new Http();
        HttpResponse response;
        response = http.send(httpReq);


I hope this is the answer you are looking for.

Regards;

Mert

Thejasvi AThejasvi A
hello Mert YALTI,

I am trying to send the email from SF to AWS along with attchment. I am also receiving the email, but without attchment. I understand "recipient,sender,subject,body,attachName,attachContentType,attachBody" , could you please help me with the 'baseURl' and HTML value that you are referring to here.