MailsenderImpl - gsnaidujava/pp GitHub Wiki
package com.firstdata.email.impl;
import java.util.Properties;
import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart;
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service;
import com.firstdata.email.model.EmailMessage;
@Service("mailSender") public class MailSenderImpl { Logger LOG = LoggerFactory.getLogger(MailSenderImpl.class);
@Value("${mail.smtp.user.name}")
private String username;
@Value("${mail.smtp.from.email}")
private String fromEmail;
@Value("${mail.smtp.password}")
private String password;
@Value("${mail.smtp.host}")
private String host;
@Value("${mail.smtp.port}")
private String port;
@Value("${mail.smtp.auth}")
private String auth;
@Value("${mail.smtp.starttls.enable}")
private String enable;
public void send(EmailMessage emailMessage)
{
LOG.debug("MailSenderImpl started in send method.");
LOG.debug("mail.smtp.auth : " + auth);
LOG.debug("mail.smtp.starttls.enable : " + enable);
LOG.debug("mail.smtp.host : " + host);
LOG.debug("mail.smtp.port : " + port);
LOG.debug("mail.smtp.user.name : " + username);
LOG.debug("mail.smtp.from.email : " + fromEmail);
Properties props = new Properties();
props.put("mail.smtp.auth", auth);
props.put("mail.smtp.starttls.enable", enable);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
// TODO : uncomment the below after getting the SMTP details.
Session session = Session.getInstance(props, new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}
});
if (session != null)
{
try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmail));
message
.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(emailMessage.getToEmailAddress()));
message.setSubject(emailMessage.getSubject());
message.setText(emailMessage.getText());
Transport.send(message);
LOG.debug("Message Sent");
} catch (MessagingException e)
{
LOG.error("Message could not be sent : " + e.getMessage());
}
}
LOG.debug("MailSenderImpl finished in send method.");
}
public void send(EmailMessage emailMessage, String fileName, String fileAttachment)
{
LOG.debug("MailSenderImpl started in send method.");
Properties props = new Properties();
props.put("mail.smtp.auth", auth);
props.put("mail.smtp.starttls.enable", enable);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
// TODO : uncomment the below after getting the SMTP details.
Session session = Session.getInstance(props, new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}
});
if (session != null)
{
try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmail));
message
.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(emailMessage.getToEmailAddress()));
message.setSubject(emailMessage.getSubject());
MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText(emailMessage.getText()); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(fileAttachment); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(fileName); multipart.addBodyPart(messageBodyPart); // Put parts in message message.setContent(multipart);
Transport.send(message);
LOG.debug("Message Sent");
} catch (MessagingException e)
{
LOG.error("Message could not be sent : " + e.getMessage());
}
}
LOG.debug("MailSenderImpl finished in send method.");
}
}