Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1452 | jmachado | 1 | package pt.estgp.estgweb.services.email.impl; |
2 | |||
3 | import org.apache.log4j.Logger; |
||
4 | import pt.estgp.estgweb.Globals; |
||
5 | |||
6 | import javax.mail.MessagingException; |
||
7 | import javax.mail.PasswordAuthentication; |
||
8 | import javax.mail.Session; |
||
9 | import javax.mail.Transport; |
||
10 | import java.util.Properties; |
||
11 | |||
12 | /** |
||
13 | * Created by jorgemachado on 13/06/16. |
||
14 | */ |
||
15 | public class EmailTransport |
||
16 | { |
||
17 | |||
18 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(EmailTransport.class); |
||
19 | |||
20 | Session session = null; |
||
21 | Transport transport = null; |
||
22 | |||
23 | 1.5.0/docs/api/java/lang/String.html">String sendFromAddress; |
||
24 | 1.5.0/docs/api/java/lang/String.html">String password; |
||
25 | 1.5.0/docs/api/java/lang/String.html">String identifier; |
||
26 | |||
27 | public EmailTransport(1.5.0/docs/api/java/lang/String.html">String identifier, 1.5.0/docs/api/java/lang/String.html">String sendFromAddress,1.5.0/docs/api/java/lang/String.html">String password) |
||
28 | { |
||
29 | this.identifier = identifier; |
||
30 | this.sendFromAddress = sendFromAddress; |
||
31 | this.password = password; |
||
32 | } |
||
33 | |||
34 | public synchronized Transport getEmailTransport() |
||
35 | { |
||
36 | if(session == null || transport == null || !transport.isConnected()) |
||
37 | { |
||
38 | logger.info("Transport(" + identifier + ") CONNECTION TO TRANSPORT DOES NOT EXIST OR FAIL WILL REOPEN : " + sendFromAddress + " password: " + password); |
||
39 | } |
||
40 | else |
||
41 | { |
||
42 | 1.5.0/docs/api/java/lang/System.html">System.out.println("Transport(" + identifier + ") OK --> RETURNING Transport"); |
||
43 | return transport; |
||
44 | } |
||
45 | |||
46 | |||
47 | |||
48 | |||
49 | // Set up properties for mail session |
||
50 | //Properties props = System.getProperties(); |
||
51 | 1.5.0/docs/api/java/util/Properties.html">Properties props = new 1.5.0/docs/api/java/util/Properties.html">Properties(); |
||
52 | props.put("mail.smtp.host", Globals.EMAIL_SERVER_HOST); |
||
53 | props.setProperty("mail.transport.protocol", "smtp");//todo new |
||
54 | javax.mail.1.5.0/docs/api/java/net/Authenticator.html">Authenticator authenticator = null; |
||
55 | if(Globals.SYSTEM_EMAIL_USE_SECURITY && Globals.EMAIL_SERVER_SECURITY != null && Globals.EMAIL_SERVER_SECURITY.compareTo("TLS")==0) |
||
56 | { |
||
57 | logger.info("Transport(" + identifier + ") Using Email Security TLS"); |
||
58 | |||
59 | props.put("mail.smtp.auth", "true"); |
||
60 | props.put("mail.smtp.starttls.enable", "true"); |
||
61 | props.put("mail.smtp.port", ""+ Globals.EMAIL_SERVER_SECURITY_TLS); |
||
62 | authenticator = new javax.mail.1.5.0/docs/api/java/net/Authenticator.html">Authenticator() { |
||
63 | |||
64 | protected 1.5.0/docs/api/java/net/PasswordAuthentication.html">PasswordAuthentication getPasswordAuthentication() { |
||
65 | 1.5.0/docs/api/java/lang/System.html">System.out.println("Email from:" + sendFromAddress + " pass:" + password); |
||
66 | return new 1.5.0/docs/api/java/net/PasswordAuthentication.html">PasswordAuthentication(sendFromAddress, password); |
||
67 | } |
||
68 | }; |
||
69 | } |
||
70 | else if(Globals.SYSTEM_EMAIL_USE_SECURITY && Globals.EMAIL_SERVER_SECURITY != null && Globals.EMAIL_SERVER_SECURITY.compareTo("SSL")==0) |
||
71 | { |
||
72 | logger.info("Transport(" + identifier + ") Using Email Security SSL"); |
||
73 | |||
74 | props.put("mail.smtp.socketFactory.port", Globals.EMAIL_SERVER_SECURITY_SSL); |
||
75 | props.put("mail.smtp.socketFactory.class", |
||
76 | "javax.net.ssl.SSLSocketFactory"); |
||
77 | props.put("mail.smtp.auth", "true"); |
||
78 | props.put("mail.smtp.port", Globals.EMAIL_SERVER_SECURITY_SSL); |
||
79 | authenticator = new javax.mail.1.5.0/docs/api/java/net/Authenticator.html">Authenticator() { |
||
80 | protected 1.5.0/docs/api/java/net/PasswordAuthentication.html">PasswordAuthentication getPasswordAuthentication() { |
||
81 | 1.5.0/docs/api/java/lang/System.html">System.out.println("Email from:" + sendFromAddress + " pass:" + password); |
||
82 | |||
83 | return new 1.5.0/docs/api/java/net/PasswordAuthentication.html">PasswordAuthentication(sendFromAddress,password); |
||
84 | } |
||
85 | }; |
||
86 | } |
||
87 | else |
||
88 | { |
||
89 | |||
90 | } |
||
91 | |||
92 | |||
93 | |||
94 | 1.5.0/docs/api/java/lang/System.html">System.out.println("Transport(" + identifier + ") Autenticator != null :" + (authenticator != null)); |
||
95 | // Get session |
||
96 | /*Session session = authenticator != null ? |
||
97 | Session.getDefaultInstance(props, authenticator) |
||
98 | : Session.getDefaultInstance(props) ; |
||
99 | */ |
||
100 | |||
101 | session = authenticator != null ? |
||
102 | Session.getInstance(props, authenticator) |
||
103 | : Session.getInstance(props) ; |
||
104 | |||
105 | try { |
||
106 | transport = session.getTransport("smtp"); |
||
107 | 1.5.0/docs/api/java/lang/System.html">System.out.println("Transport(" + identifier + ") before connect isConnected:" + transport.isConnected()); |
||
108 | transport.connect(); |
||
109 | 1.5.0/docs/api/java/lang/System.html">System.out.println("Transport(" + identifier + ") after connect isConnected:" + transport.isConnected()); |
||
110 | } catch (MessagingException e) { |
||
111 | logger.error(e,e); |
||
112 | return null; |
||
113 | } |
||
114 | |||
115 | session.setDebug(true); |
||
116 | session.setDebugOut(1.5.0/docs/api/java/lang/System.html">System.out); |
||
117 | return transport; |
||
118 | } |
||
119 | |||
120 | public 1.5.0/docs/api/java/lang/String.html">String getIdentifier() { |
||
121 | return identifier; |
||
122 | } |
||
123 | |||
124 | public Session getSession() { |
||
125 | return session; |
||
126 | } |
||
127 | |||
128 | public Transport getTransport() { |
||
129 | return transport; |
||
130 | } |
||
131 | |||
132 | public 1.5.0/docs/api/java/lang/String.html">String getSendFromAddress() { |
||
133 | return sendFromAddress; |
||
134 | } |
||
135 | } |