1+ package com .objectcomputing .pulsesurvey .email .manager ;
2+
3+ import com .google .api .client .googleapis .javanet .GoogleNetHttpTransport ;
4+ import com .google .api .client .http .HttpTransport ;
5+ import com .google .api .client .json .JsonFactory ;
6+ import com .google .api .client .json .jackson2 .JacksonFactory ;
7+ import com .google .api .client .util .Base64 ;
8+ import com .google .api .services .gmail .Gmail ;
9+ import com .google .api .services .gmail .model .Message ;
10+ import io .micronaut .context .annotation .Property ;
11+ import org .slf4j .Logger ;
12+ import org .slf4j .LoggerFactory ;
13+
14+ import javax .inject .Inject ;
15+ import javax .inject .Singleton ;
16+ import javax .mail .MessagingException ;
17+ import javax .mail .Session ;
18+ import javax .mail .internet .InternetAddress ;
19+ import javax .mail .internet .MimeMessage ;
20+ import java .io .ByteArrayOutputStream ;
21+ import java .io .IOException ;
22+ import java .net .Authenticator ;
23+ import java .net .PasswordAuthentication ;
24+ import java .security .GeneralSecurityException ;
25+ import java .util .Map ;
26+ import java .util .Properties ;
27+
28+ @ Singleton
29+ public class GmailSender {
30+
31+ private final HttpTransport httpTransport ;
32+ private final JsonFactory jsonFactory ;
33+ private final GoogleAuthenticator authenticator ;
34+ private final String applicationName ;
35+ private static final Logger LOG = LoggerFactory .getLogger (GmailSender .class );
36+
37+ @ Inject
38+ GmailSender (
39+ @ Property (name = "oci-google-drive.application.name" ) String applicationName ,
40+ GoogleAuthenticator authenticator ) throws GeneralSecurityException , IOException {
41+ this .applicationName = applicationName ;
42+ this .authenticator = authenticator ;
43+ this .httpTransport = GoogleNetHttpTransport .newTrustedTransport ();
44+ this .jsonFactory = JacksonFactory .getDefaultInstance ();
45+ }
46+
47+ Gmail getService () throws IOException {
48+ return new Gmail .Builder (httpTransport , jsonFactory , authenticator .setupCredentials ())
49+ .setApplicationName (applicationName )
50+ .build ();
51+ }
52+
53+ public void sendEmail (String subject , String emailAddress , String emailMessage ) {
54+ try {
55+ Gmail emailService = getService ();
56+ LOG .info ("Email Address: " + emailAddress );
57+ LOG .info ("Email body: " + emailMessage );
58+
59+ Properties props = new Properties ();
60+ Session session = Session .getDefaultInstance (props , null );
61+ MimeMessage emailContent = new MimeMessage (session );
62+ emailContent .setFrom ("kimberlinm@objectcomputing.com" );
63+ emailContent .addRecipient (javax .mail .Message .RecipientType .TO ,
64+ new InternetAddress ("williamsh@objectcomputing.com" ));
65+ emailContent .setSubject (subject );
66+ emailContent .setText (emailMessage );
67+ /*.setContent(html code) will have to be constructed from template */
68+ Message message = createMessageWithEmail (emailContent );
69+ message = emailService .users ().messages ().send ("kimberlinm@objectcomputing.com" , message ).execute ();
70+
71+ LOG .info ("Message id: " + message .getId ());
72+ LOG .info (message .toPrettyString ());
73+ LOG .info ("Email Sent: " + subject + " to " + emailAddress + "\n " );
74+ } catch (IOException e ) {
75+ LOG .error ("IOException: " + e .getMessage ());
76+ e .printStackTrace ();
77+ } catch (MessagingException e ) {
78+ LOG .error ("MessagingException: " + e .getMessage ());
79+ e .printStackTrace ();
80+ }
81+
82+ }
83+
84+ /**
85+ * Create a message from an email. Converts Mime to gmail format
86+ *
87+ * @param emailContent Email to be set to raw of message
88+ * @return a message containing a base64url encoded email
89+ * @throws IOException
90+ * @throws MessagingException
91+ */
92+ public static Message createMessageWithEmail (MimeMessage emailContent )
93+ throws MessagingException , IOException {
94+ ByteArrayOutputStream buffer = new ByteArrayOutputStream ();
95+ emailContent .writeTo (buffer );
96+ byte [] bytes = buffer .toByteArray ();
97+ String encodedEmail = Base64 .encodeBase64URLSafeString (bytes );
98+ Message message = new Message ();
99+ message .setRaw (encodedEmail );
100+ return message ;
101+ }
102+ }
0 commit comments