+ Start a Discussion
sunny522sunny522 

How to send email along with attachments using Amazon SES?

Hi ,
I am able to send email using sendemail method.In order to send email with attachments I think we need to use SendRawEmail method.,I used sendrawEmail method .I am getting response as 200 but I didn't receive any emails/attachments.Can anyone provide a sample code for this?
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);


This codes are working for me. I hope this will help you.

Regards;