Sending Email using WaveMaker Connector
Email Connector provides simplified APIs to integrate with any Email service provider. It provides methods for sending a plain text message, parameterized/templatized messages & also enables sending messages with attachments.
This document explains the following 3 different ways to send the email message :
- Send an Email with a text message.
- Send an Email with a templatized message.
- Send an Email with attachments.
Steps involved in sending an email:
Importing the Email-connector to Project
- Download the latest email-connector zip here
- Import the downloaded email-connector zip into your app using the Import Resource option to the Connector folder.
Configure Email Connector Properties in Profiles
- By default externalized connector properties are added in the project profiles.
- Connector externalized properties are prefixed with
connector.${connectorName}
.
connector.email.default.email.server.host=
connector.email.default.email.server.password=
connector.email.default.email.server.port=
connector.email.default.email.server.username=
connector.email.default.email.transport.protocol=smtp
connector.email.default.email.server.sslenabled=true
You should specify the values for connector properties in profiles.
These externalized properties are used in the connector, If required, you can also read these properties in java service as below:
Import Statement:
import org.springframework.beans.factory.annotation.Value;
@Value("${email.server.username}")
private String fromEmailAddress;
Creating Java Service
- Create a Java Service, named EmailService
- Add the import statement for the email connector api class.
import org.springframework.mail.SimpleMailMessage;
import com.wavemaker.connector.email.EmailConnector;
- Autowire the email connector API class.
@Autowired
private EmailConnector emailConnector;
- Using the
emailConnector
Here we are setting default values for the properties like fromEmailAddress. required by the EmailService. To set them to Environment level values see the next section.
Send an Email with Text
@ExposeToClient
public class EmailService {
private static final Logger logger = LoggerFactory.getLogger(EmailService.class);
@Autowired
private EmailConnector emailConnector;
@Value("${email.server.username}")
private String fromEmailAddress;
public void sendMailWithSimpleMessage(String toEmailAddress, String emailSubject, String emailMessage) {
logger.info("Sending the email to {} with subject {} and message {}", toEmailAddress, emailSubject, emailMessage);
String[] recipientList = toEmailAddress.split(",");
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(fromEmailAddress);
simpleMailMessage.setTo(recipientList);
simpleMailMessage.setSubject(emailSubject);
simpleMailMessage.setText(emailMessage);
emailConnector.sendSimpleMailMessage(simpleMailMessage);
}
}
Send an Email with a templatized message
Required import statements
import java.util.HashMap;
import java.util.Map;
import com.wavemaker.connector.exception.EmailTemplateNotFoundException;
Sending an email with template
public void sendEmailWithTemplate(String toEmailAddress, String emailSubject) {
logger.info("Sending the email to {} with subject {} ", toEmailAddress, emailSubject);
String[] recipientList = toEmailAddress.split(",");
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject(emailSubject);
message.setTo(recipientList);
message.setFrom(fromEmailAddress);
Map<String, String> props = new HashMap<>();
props.put("user", "Mike");
try {
emailConnector.sendSimpleMailMessageWithTemplate(message, "templates/invitationtemplate", props);
} catch (EmailTemplateNotFoundException e) {
throw new RuntimeException("Email template not found", e);
}
}
If you want to use the method, you should have a template with name invitationtemplate.txt in the project resources src/main/resources/templates
folder.
Send an Email with Attachments
Required import statements
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
Send Email with Attachment
public void sendMailWithMessagePreparator(String toEmailAddress, String emailSubject, String emailMessage) {
logger.info("Sending email to {}, with subject : {}, message : {} and mimetype content", toEmailAddress, emailSubject, emailMessage);
emailConnector.sendMimeMail(new MimeMessagePreparator() {
@Override
public void prepare(final MimeMessage mimeMessage) throws Exception {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
mimeMessageHelper.addTo(toEmailAddress);
mimeMessageHelper.setFrom(fromEmailAddress);
mimeMessageHelper.setSubject(emailSubject);
mimeMessageHelper.setText(emailMessage);
mimeMessageHelper.addAttachment("myfile", new ClassPathResource("GitLabIcon.png"));
}
});
}
Send Email Method with Mime Type
public void sendMimeMail(String toEmailAddress, String emailSubject) {
emailConnector.sendMimeMail(new MimeMessagePreparator() {
logger.info("Sending email to {}, with subject {} and mimetype content", toEmailAddress, emailSubject);
@Override
public void prepare(final MimeMessage mimeMessage) throws Exception {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
mimeMessageHelper.addTo(toEmailAddress);
mimeMessageHelper.setFrom(fromEmailAddress);
mimeMessageHelper.setSubject(emailSubject);
MimeBodyPart mimeBodyPart = new MimeBodyPart();
MimeMultipart mimeMultipart = new MimeMultipart();
String htmlContent = "<html><h1>Hi</h1><p>Nice to meet you!</p></html>";
mimeBodyPart.setContent(htmlContent, "text/html");
mimeMultipart.addBodyPart(mimeBodyPart);
mimeMessageHelper.getMimeMessage().setContent(mimeMultipart);
}
});
}
Send Email with Attachment and Mime Message
public void sendInlineMail(String toEmailAddress, String emailSubject) {
emailConnector.sendMimeMail(new MimeMessagePreparator() {
@Override
public void prepare(final MimeMessage mimeMessage) throws Exception {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
mimeMessageHelper.addTo(toEmailAddress);
mimeMessageHelper.setFrom(fromEmailAddress);
mimeMessageHelper.setSubject(emailSubject);
MimeMultipart mimeMultipart = new MimeMultipart();
MimeBodyPart htmlpart = new MimeBodyPart();
String htmlMessage = "<html>Hi there, ";
htmlMessage += "See this cool pic: <img src=\"cid:AbcXyz123\" />";
htmlMessage += "</html>";
htmlpart.setContent(htmlMessage, "text/html");
MimeBodyPart imagePart = new MimeBodyPart();
imagePart.setHeader("Content-ID", "<AbcXyz123>");
imagePart.setDisposition(MimeBodyPart.INLINE);
imagePart.attachFile(new ClassPathResource("GitLabIcon.png").getFile());
mimeMultipart.addBodyPart(htmlpart);
mimeMultipart.addBodyPart(imagePart);
mimeMessageHelper.getMimeMessage().setContent(mimeMultipart);
}
});
}
Integrating with UI
Create a Java Service Variable for the Java service created in the previous steps.
You can now use this service variable in your application as per your business logic.
Customizing Email Properties
If you want to add or customize any other existing email properties in addition to the properties discussed above, you can do it through Java Services.
- Add the below Import statement to the Java Service.
import javax.annotation.PostConstruct;
- Add the below code snippet to your Java Service. Here, we are setting the
mail.smtp.starttls.enable
property of emailconnector to false.
@PostConstruct
public void settingEmailProperties() {
Properties properties = new Properties();
properties.setProperty("mail.smtp.starttls.enable", "false");
emailConnector.setEmailProperties(properties);
}
See Also
How to send emails using Java Service
How to implement forgot password feature using Java Service
How to schedule a Java Service
How to accomplish Pre-Post Processing for a DB Service APIs