Search in sources :

Example 1 with MailReportSendException

use of com.axway.ats.log.report.exceptions.MailReportSendException in project ats-framework by Axway.

the class MailReportSender method send.

/**
     * Email the report
     */
public void send() {
    log.info("Sending log mail report");
    // get the info needed for sending a mail
    ReportConfigurator reportConfigurator = ReportConfigurator.getInstance();
    String smtpServerName = reportConfigurator.getSmtpServerName();
    String smtpServerPort = reportConfigurator.getSmtpServerPort();
    String[] addressesTo = reportConfigurator.getAddressesTo();
    String[] addressesCc = reportConfigurator.getAddressesCc();
    String[] addressesBcc = reportConfigurator.getAddressesBcc();
    String addressFrom = reportConfigurator.getAddressFrom();
    // Attaching to default Session
    Properties mailProperties = new Properties();
    mailProperties.put("mail.smtp.host", smtpServerName);
    mailProperties.put("mail.smtp.port", smtpServerPort);
    Session session = Session.getDefaultInstance(mailProperties);
    Message msg = new MimeMessage(session);
    String errMsg = "Error creating mail object";
    try {
        // mail addresses
        msg.setFrom(new InternetAddress(addressFrom));
        msg.setRecipients(Message.RecipientType.TO, transformAdresses(addressesTo));
        msg.setRecipients(Message.RecipientType.CC, transformAdresses(addressesCc));
        msg.setRecipients(Message.RecipientType.BCC, transformAdresses(addressesBcc));
        // mail subject
        msg.setSubject(subject);
        // mail content
        msg.setContent(body, "text/html");
        // other header information
        msg.setSentDate(new Date());
    } catch (AddressException e) {
        throw new MailReportSendException(errMsg, e);
    } catch (MessagingException e) {
        throw new MailReportSendException(errMsg, e);
    }
    // send the message
    errMsg = "Error sending mail";
    try {
        Transport.send(msg);
    } catch (MessagingException e) {
        throw new MailReportSendException(errMsg, e);
    }
    log.info("Log mail report sent ok");
}
Also used : ReportConfigurator(com.axway.ats.log.report.model.ReportConfigurator) InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MailReportSendException(com.axway.ats.log.report.exceptions.MailReportSendException) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) Properties(java.util.Properties) Date(java.util.Date) Session(javax.mail.Session)

Aggregations

MailReportSendException (com.axway.ats.log.report.exceptions.MailReportSendException)1 ReportConfigurator (com.axway.ats.log.report.model.ReportConfigurator)1 Date (java.util.Date)1 Properties (java.util.Properties)1 Message (javax.mail.Message)1 MessagingException (javax.mail.MessagingException)1 Session (javax.mail.Session)1 AddressException (javax.mail.internet.AddressException)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeMessage (javax.mail.internet.MimeMessage)1