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.org;
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.OrganizationException;
37 public class JavaxMailer implements Mailer {
38 private Session session;
40 public JavaxMailer() {
42 // Get the default Session object.
43 session = Session.getDefaultInstance(System.getProperties());
48 public int sendEmail(AuthzTrans trans, boolean testMode, String mailFrom, List<String> to, List<String> cc, String subject, String body,
49 Boolean urgent) throws OrganizationException {
55 // Create a default MimeMessage object.
56 MimeMessage message = new MimeMessage(session);
58 // Set From: header field of the header.
59 message.setFrom(new InternetAddress(mailFrom));
62 // Set To: header field of the header. This is a required field
63 // and calling module should make sure that it is not null or
65 message.addRecipients(Message.RecipientType.TO,getAddresses(to));
67 // Set CC: header field of the header.
68 if ((cc != null) && (cc.size() > 0)) {
69 message.addRecipients(Message.RecipientType.CC,getAddresses(cc));
72 // Set Subject: header field
73 message.setSubject(subject);
76 message.addHeader("X-Priority", "1");
79 // Now set the actual message
80 message.setText(body);
83 // override recipients
84 message.addRecipients(Message.RecipientType.TO,
85 InternetAddress.parse(mailFrom));
87 // Set Subject: header field
88 message.setSubject("[TESTMODE] " + subject);
91 message.addHeader("X-Priority", "1");
94 ArrayList<String> newBody = new ArrayList<>();
96 Address temp[] = getAddresses(to);
97 String headerString = "TO:\t" + InternetAddress.toString(temp) + "\n";
99 temp = getAddresses(cc);
100 headerString += "CC:\t" + InternetAddress.toString(temp) + "\n";
102 newBody.add(headerString);
104 newBody.add("Text: \n");
107 String outString = "";
108 for (String s : newBody) {
109 outString += s + "\n";
112 message.setText(outString);
115 Transport.send(message);
118 } catch (MessagingException mex) {
119 System.out.println("Error messaging: "+ mex.getMessage());
120 System.out.println("Error messaging: "+ mex.toString());
121 throw new OrganizationException("Exception send email message "
129 * Convert the delimiter String into Internet addresses with the default
134 private Address[] getAddresses(List<String> strAddress) throws OrganizationException {
135 return this.getAddresses(strAddress,";");
138 * Convert the delimiter String into Internet addresses with the
139 * delimiter of provided
140 * @param strAddresses
144 private Address[] getAddresses(List<String> strAddresses, String delimiter) throws OrganizationException {
145 Address[] addressArray = new Address[strAddresses.size()];
147 for (String addr : strAddresses)
150 addressArray[count] = new InternetAddress(addr);
153 throw new OrganizationException("Failed to parse the email address "+ addr +": "+e.getMessage());