I'm trying to send email with attachment using SMTP protocol. Also I added activation.jar
(v. 1.1.1) as Maven dependency and javax.mail dependency (v. 1.4.7). And I have got configs in xml that works for messages without attachments.
Here is my code
public boolean sendEmail(Form form) {
try {
Properties props = getProperties();
Session session = Session.getInstance(props);
MimeMessage mimeMessage = new MimeMessage(session);
MimeMultipart multiPart = new MimeMultipart("mixed");
MimeBodyPart contentPart = new MimeBodyPart();
contentPart.setText("Email Body", "UTF-8");
multiPart.addBodyPart(contentPart);
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.setContent(form.getCvFile(), "application/pdf");
attachmentPart.setDisposition(Part.ATTACHMENT);
attachmentPart.setFileName(form.getFile().getName());
multiPart.addBodyPart(attachmentPart);
mimeMessage.setContent(multiPart);
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
log.error(e.getMessage(), e);
}
return false;
}
It gives me NullPointer
java.lang.NullPointerException
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:557)
at com.my.sender.EmailSender.sendEmail(EmailSender.java:59)
at com.my.controller.SpringController.update(SpringController.java:133)
at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:71)
at com.liferay.portlet.CheckboxParametersPortletFilter.doFilter(CheckboxParametersPortletFilter.java:57)
at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:68)
at com.liferay.portlet.CheckboxParametersPortletFilter.doFilter(CheckboxParametersPortletFilter.java:57)
at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:68)
at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:48)
at com.liferay.portal.kernel.servlet.PortletServlet.service(PortletServlet.java:105)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at com.liferay.portal.osgi.web.wab.extender.internal.adapter.ServletExceptionAdapter.service(ServletExceptionAdapter.java:68)
at org.eclipse.equinox.http.servlet.internal.registration.EndpointRegistration.service(EndpointRegistration.java:153)
at org.eclipse.equinox.http.servlet.internal.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:50)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:119)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:115)
at com.liferay.portal.osgi.web.wab.extender.internal.adapter.FilterExceptionAdapter.doFilter(FilterExceptionAdapter.java:46)
at org.eclipse.equinox.http.servlet.internal.registration.FilterRegistration.doFilter(FilterRegistration.java:121)
at org.eclipse.equinox.http.servlet.internal.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:45)
at org.eclipse.equinox.http.servlet.internal.servlet.ResponseStateHandler.processRequest(ResponseStateHandler.java:70)