2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
21 package org.onap.aaf.auth.javax;
23 import java.util.ArrayList;
24 import java.util.List;
26 import javax.mail.Address;
27 import javax.mail.Message;
28 import javax.mail.MessagingException;
29 import javax.mail.Session;
30 import javax.mail.Transport;
31 import javax.mail.internet.InternetAddress;
32 import javax.mail.internet.MimeMessage;
34 import org.onap.aaf.auth.env.AuthzTrans;
35 import org.onap.aaf.auth.org.Mailer;
36 import org.onap.aaf.auth.org.OrganizationException;
38 public class JavaxMailer implements Mailer {
39 private Session session;
41 public JavaxMailer() {
43 // Get the default Session object.
44 session = Session.getDefaultInstance(System.getProperties());
49 public int sendEmail(AuthzTrans trans, boolean testMode, String mailFrom, List<String> to, List<String> cc, String subject, String body,
50 Boolean urgent) throws OrganizationException {
56 // Create a default MimeMessage object.
57 MimeMessage message = new MimeMessage(session);
59 // Set From: header field of the header.
60 message.setFrom(new InternetAddress(mailFrom));
63 // Set To: header field of the header. This is a required field
64 // and calling module should make sure that it is not null or
66 message.addRecipients(Message.RecipientType.TO,getAddresses(to));
68 // Set CC: header field of the header.
69 if ((cc != null) && (cc.size() > 0)) {
70 message.addRecipients(Message.RecipientType.CC,getAddresses(cc));
73 // Set Subject: header field
74 message.setSubject(subject);
77 message.addHeader("X-Priority", "1");
80 // Now set the actual message
81 message.setText(body);
84 // override recipients
85 message.addRecipients(Message.RecipientType.TO,
86 InternetAddress.parse(mailFrom));
88 // Set Subject: header field
89 message.setSubject("[TESTMODE] " + subject);
92 message.addHeader("X-Priority", "1");
95 ArrayList<String> newBody = new ArrayList<>();
97 Address temp[] = getAddresses(to);
98 String headerString = "TO:\t" + InternetAddress.toString(temp) + "\n";
100 temp = getAddresses(cc);
101 headerString += "CC:\t" + InternetAddress.toString(temp) + "\n";
103 newBody.add(headerString);
105 newBody.add("Text: \n");
108 String outString = "";
109 for (String s : newBody) {
110 outString += s + "\n";
113 message.setText(outString);
116 Transport.send(message);
119 } catch (MessagingException mex) {
120 System.out.println("Error messaging: "+ mex.getMessage());
121 System.out.println("Error messaging: "+ mex.toString());
122 throw new OrganizationException("Exception send email message "
130 * Convert the delimiter String into Internet addresses with the default
135 private Address[] getAddresses(List<String> strAddress) throws OrganizationException {
136 return this.getAddresses(strAddress,";");
139 * Convert the delimiter String into Internet addresses with the
140 * delimiter of provided
141 * @param strAddresses
145 private Address[] getAddresses(List<String> strAddresses, String delimiter) throws OrganizationException {
146 Address[] addressArray = new Address[strAddresses.size()];
148 for (String addr : strAddresses)
151 addressArray[count] = new InternetAddress(addr);
154 throw new OrganizationException("Failed to parse the email address "+ addr +": "+e.getMessage());