Details
-
Sub-task
-
Status: Closed
-
Major
-
Resolution: Fixed
-
None
-
None
Description
<bean id="mailService" class="ro.codemart.tvb.mail.GMailService">
<property name="smtpServer" value="mail.thevirtualbrain.org" />
<property name="smtpPort" value="587" />
<property name="userLogin" value="tvb_appserver" />
<property name="userPassword" value="du5rEpratHAc" />
<property name="fromAddress" value="tvb_appserver@thevirtualbrain.org" />
<property name="settingsService" ref="settingsService" />
</bean>
package ro.codemart.tvb.mail;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.MimeMessageHelper;
import ro.codemart.tvb.settings.SettingsService;
import java.io.File;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
- Service for sending email notifications.
* - @author ionel.ortelecan
*/
public class GMailService {
// Constant for email content type
private static final String EMAIL_CONTENT_TYPE = "text/html";
private static final String IMAGE_HEADER_IDENTIFIER = "tvbHeaderIdentifier";
private static final String IMAGE_HEADER_NAME = "emailHeader.jpg";
private static final String HOST = "mail.smtp.host";
private static final String PORT = "mail.smtp.port";
private static final String AUTH = "mail.smtp.auth";
private static final String START_TLS = "mail.smtp.starttls.enable";
private SettingsService settingsService;
private String smtpServer;
private String smtpPort;
private String fromAddress;
private String userLogin;
private String userPassword;
public void setSmtpServer(String smtpServer)
{ this.smtpServer = smtpServer; }public void setSmtpPort(String smtpPort)
{ this.smtpPort = smtpPort; }public void setFromAddress(String fromAddress)
{ this.fromAddress = fromAddress; }public void setSettingsService(SettingsService settingsService)
{ this.settingsService = settingsService; }public void setUserLogin(String userLogin)
{ this.userLogin = userLogin; }public void setUserPassword(String userPassword)
{ this.userPassword = userPassword; }/**
- Sends an email with the specified parameters.
* - @param toAddress The reciever email address
- @param subject The email subject
- @param body The email body
- @param filePaths the paths to the files that you want to attache to this email
- @throws MessagingException When invalid smtp server or address.
*/
public void sendMail(String toAddress, String subject, String body, List<String> filePaths) throws MessagingException {
Properties properties = getEmailProperties();
Session session = getEmailSession(properties);
MimeMessage message = new MimeMessage(session);
InternetAddress addressTo = new InternetAddress(toAddress);
message.addRecipient(javax.mail.Message.RecipientType.TO, addressTo);
message.setSubject(subject);
message.setFrom(new InternetAddress(fromAddress));
message.setReplyTo(new Address[]
);
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setText(body, true);
URL imgUrl = GMailService.class.getClassLoader().getResource(IMAGE_HEADER_NAME);
FileSystemResource res = new FileSystemResource(new File(imgUrl.getFile()));
helper.addInline(IMAGE_HEADER_IDENTIFIER, res);
for (String path : filePaths)
{ FileSystemResource file = new FileSystemResource(new File(path)); helper.addAttachment(file.getFilename(), file); } message.setSentDate(new Date());
Transport.send(message);
}
private Properties getEmailProperties()
{ Properties props = new Properties(); props.put(HOST, smtpServer); props.put(PORT, smtpPort); props.put(AUTH, "true"); props.put(START_TLS, "true"); return props; } private Session getEmailSession(Properties properties) {
return Session.getDefaultInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication()
});
}
}