Skip to content

Instantly share code, notes, and snippets.

@circlee
Created July 4, 2017 04:00
Show Gist options
  • Save circlee/e06ce175a5ef10939917c675d9e9a188 to your computer and use it in GitHub Desktop.
Save circlee/e06ce175a5ef10939917c675d9e9a188 to your computer and use it in GitHub Desktop.
ses service impl
import javax.inject.Inject;
import javax.mail.internet.MimeUtility;
import org.springframework.stereotype.Service;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient;
import com.amazonaws.services.simpleemail.model.Body;
import com.amazonaws.services.simpleemail.model.Content;
import com.amazonaws.services.simpleemail.model.Destination;
import com.amazonaws.services.simpleemail.model.Message;
import com.amazonaws.services.simpleemail.model.SendEmailRequest;
import com.glowpick.ec.core.s3Services.AWSSesService;
@Service
public class AWSSesServiceImpl implements AWSSesService{
@Inject
private AmazonSimpleEmailServiceClient amazonSesClient;
@Override
public void sendMail(String fromAddr, String toAddr, String subjectText, String bodyText){
// Construct an object to contain the recipient address.
Destination destination = new Destination().withToAddresses(new String[]{toAddr});
// Create the subject and body of the message.
Content subject = new Content().withData(subjectText);
Content textBody = new Content().withData(bodyText);
Body body = new Body().withHtml(textBody);
// Create a message with the specified subject and body.
Message message = new Message().withSubject(subject).withBody(body);
// Assemble the email.
SendEmailRequest request = new SendEmailRequest().withSource(fromAddr).withDestination(destination).withMessage(message);
amazonSesClient.sendEmail(request);
}
@Override
public void sendMail(String senderName, String fromAddr, String receiverName, String toAddr, String subjectText, String bodyText){
if(senderName != null && senderName.length() > 0) {
fromAddr = encodeName(senderName, fromAddr);
}
if(receiverName != null && receiverName.length() > 0) {
toAddr = encodeName(receiverName, toAddr);
}
sendMail(fromAddr, toAddr, subjectText, bodyText);
}
@Override
public void sendMail(String senderName, String fromAddr, String toAddr, String subjectText, String bodyText){
sendMail(senderName, fromAddr, null, toAddr, subjectText, bodyText);
}
private static String encodeName(String name, String address){
String returnStr = address;
try {
String encoded = MimeUtility.encodeText(name, "utf-8", "Q");
returnStr = encoded + "<"+address+">";
} catch (Exception e) {
e.printStackTrace();
}
return returnStr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment