This repository has been archived by the owner on Jun 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrote MailService to use Java Mail for future configurability #443
- Loading branch information
monitobeko
committed
Dec 19, 2016
1 parent
338e30e
commit c0ccaf7
Showing
1 changed file
with
18 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,30 @@ | ||
package io.hawkcd.utilities; | ||
|
||
import javax.mail.Message; | ||
import javax.mail.MessagingException; | ||
import javax.mail.Session; | ||
import javax.mail.Transport; | ||
import javax.mail.*; | ||
import javax.mail.internet.InternetAddress; | ||
import javax.mail.internet.MimeMessage; | ||
import java.util.Properties; | ||
|
||
public class MailService { | ||
public static void sendMail() { | ||
// Recipient's email ID needs to be mentioned. | ||
String to = "[email protected]"; | ||
|
||
// Sender's email ID needs to be mentioned | ||
String from = "[email protected]"; | ||
|
||
// Assuming you are sending email from localhost | ||
String host = "localhost"; | ||
public static void sendMail(String recipient, String subject, String content) { | ||
// Sender's email ID needs to be mentioned, TODO: Extract this into config file | ||
String from = "[email protected]"; | ||
|
||
// Get system properties | ||
Properties properties = System.getProperties(); | ||
|
||
// Setup mail server | ||
properties.setProperty("mail.smtp.host", "smtp.gmail.com"); | ||
// Setup mail server, TODO: Extract this into config file | ||
properties.setProperty("mail.smtp.host", "smtp.sendgrid.net"); | ||
properties.setProperty("mail.smtp.port", "25"); | ||
properties.setProperty("mail.smtp.auth", "true"); | ||
|
||
// Get the default Session object. | ||
Session session = Session.getDefaultInstance(properties); | ||
// Set up authentication, TODO: Extract this into config file, if no value is specified, try to get system environment variable for security | ||
Session session = Session.getDefaultInstance(properties, new Authenticator() { | ||
@Override | ||
protected PasswordAuthentication getPasswordAuthentication() { | ||
return new PasswordAuthentication("apikey", System.getenv("SENDGRID_API_KEY")); | ||
} | ||
}); | ||
|
||
try { | ||
// Create a default MimeMessage object. | ||
|
@@ -36,41 +34,18 @@ public static void sendMail() { | |
message.setFrom(new InternetAddress(from)); | ||
|
||
// Set To: header field of the header. | ||
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); | ||
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient)); | ||
|
||
// Set Subject: header field | ||
message.setSubject("This is the Subject Line!"); | ||
message.setSubject(subject); | ||
|
||
// Now set the actual message | ||
message.setText("This is actual message"); | ||
message.setText(content); | ||
|
||
// Send message | ||
// Transport transport = session.getTransport("smtp"); | ||
// transport.connect(null, login, pass); | ||
Transport.send(message); | ||
System.out.println("Sent message successfully...."); | ||
} catch (MessagingException mex) { | ||
mex.printStackTrace(); | ||
} | ||
|
||
// Email from = new Email("[email protected]"); | ||
// String subject = "Hello from Hawk"; | ||
// Email to = new Email("[email protected]"); | ||
// Content content = new Content("text/plain", "HawkCD rocks and can now send emails!"); | ||
// Mail mail = new Mail(from, subject, to, content); | ||
// | ||
// SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY")); | ||
// Request request = new Request(); | ||
// try { | ||
// request.method = Method.POST; | ||
// request.endpoint = "mail/send"; | ||
// request.body = mail.build(); | ||
// Response response = sg.api(request); | ||
// System.out.println(response.statusCode); | ||
// System.out.println(response.body); | ||
// System.out.println(response.headers); | ||
// } catch (IOException ex) { | ||
// throw ex; | ||
// } | ||
} | ||
} |