Uploaded image for project: 'The Virtual Brain'
  1. The Virtual Brain
  2. TVB-693 User-Settings
  3. TVB-857

Replace the gmail credentials we currently use to send emails from TVB with the credentials we use for the registration site.

    XMLWordPrintable

Details

    • Sub-task
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 1.0.3
    • TVB Framework
    • 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[]

      {new InternetAddress(settingsService.getTvbAdminEmail())}

      );

      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()

      { return new PasswordAuthentication(userLogin, userPassword); }

      });
      }
      }

      Gliffy Diagrams

        Attachments

          Activity

            People

              bogdan.neacsa Bogdan Neacsa
              bogdan.neacsa Bogdan Neacsa
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - 3 hours
                  3h
                  Remaining:
                  Remaining Estimate - 0 minutes
                  0m
                  Logged:
                  Time Spent - 2 hours Time Not Required
                  2h