Created
January 9, 2012 15:39
-
-
Save ebot/1583465 to your computer and use it in GitHub Desktop.
Java Email Function
This file contains hidden or 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
| # Email Settings | |
| SMTP Server: tbd | |
| Email Account: [email protected] | |
| Email Recipients: [email protected];[email protected] |
This file contains hidden or 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
| import javax.mail.Message; | |
| import javax.mail.Multipart; | |
| import javax.mail.Session; | |
| import javax.mail.Transport; | |
| import javax.mail.internet.InternetAddress; | |
| import javax.mail.internet.MimeBodyPart; | |
| import javax.mail.internet.MimeMessage; | |
| import javax.mail.internet.MimeMultipart; | |
| /** | |
| * Sends emails with settings, and to recipients specified in the program's | |
| * conf file. | |
| * | |
| * @param subject | |
| * String representing the subject of the email. | |
| * @param message | |
| * String representing the message of the email. | |
| * @param important | |
| * Boolean indicating if this message should be sent with | |
| * importance. | |
| * @throws Exception | |
| */ | |
| public void sendEmail(String subject, String message, boolean important) | |
| throws Exception { | |
| System.out.println("Creating Email Session"); | |
| if (ProgramPreferences.isDebuging()) { | |
| System.out.println(" Server = " | |
| + ProgramPreferences.getSmtpServer()); | |
| System.out.println(" Account = " | |
| + ProgramPreferences.getEmailAccount()); | |
| System.out.println(" Recipients = " | |
| + ProgramPreferences.getEmailRecipients()); | |
| System.out.println(" Important = " + important); | |
| System.out.println(" Subject = " + subject); | |
| System.out.println(" Message = " + message); | |
| } | |
| // create some properties and get the default Session | |
| Properties props = System.getProperties(); | |
| props.put("mail.smtp.host", ProgramPreferences.getSmtpServer()); | |
| // Create a new mail session | |
| Session session = Session.getInstance(props, null); | |
| if (ProgramPreferences.isDebuging()) { | |
| session.setDebug(true); | |
| } | |
| // create a message | |
| MimeMessage msg = new MimeMessage(session); | |
| msg.setFrom(new InternetAddress(ProgramPreferences.getEmailAccount())); | |
| // Get the recepients | |
| String[] sendTo = ProgramPreferences.getEmailRecipients().split("\\;"); | |
| InternetAddress[] mailTo = new InternetAddress[sendTo.length]; | |
| for (int i = 0; i < sendTo.length; i++) { | |
| mailTo[i] = new InternetAddress(sendTo[i]); | |
| } | |
| msg.setRecipients(Message.RecipientType.TO, mailTo); | |
| // Set the subject | |
| msg.setSubject(subject); | |
| // Set the importance to high | |
| if (important) { | |
| msg.addHeader("Importance", "High"); | |
| } | |
| // create and fill the first message part | |
| MimeBodyPart mbp1 = new MimeBodyPart(); | |
| mbp1.setContent(message, "text/html"); | |
| // create the Multipart and add its parts to it | |
| Multipart mp = new MimeMultipart(); | |
| mp.addBodyPart(mbp1); | |
| // add the Multipart to the message | |
| msg.setContent(mp); | |
| // set the Date: header | |
| msg.setSentDate(new Date()); | |
| // send the message | |
| Transport.send(msg); | |
| System.out.println("Email Session ended normally"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment