Created
November 8, 2013 14:33
-
-
Save carlo-rtr/7371834 to your computer and use it in GitHub Desktop.
Sending Email
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
package com.rtr.services.example; | |
import java.util.Date; | |
import java.util.Properties; | |
import javax.mail.Message; | |
import javax.mail.MessagingException; | |
import javax.mail.Multipart; | |
import javax.mail.Session; | |
import javax.mail.Transport; | |
import javax.mail.internet.AddressException; | |
import javax.mail.internet.InternetAddress; | |
import javax.mail.internet.MimeBodyPart; | |
import javax.mail.internet.MimeMessage; | |
import javax.mail.internet.MimeMultipart; | |
import com.google.common.base.Charsets; | |
public class SampleEmail { | |
public static void main(String[] args) throws AddressException, MessagingException{ | |
Properties props = new Properties(); | |
props.put("mail.smtp.host", "127.0.0.1"); //Assumes you have an stmp server running locally | |
props.put("mail.smtp.port", Integer.toString(25)); | |
props.put("mail.debug", "true"); | |
Session session = Session.getInstance(props); | |
String emailTo = "[email protected]"; | |
String emailFrom = "[email protected]"; | |
MimeMessage mimeMsg = new MimeMessage(session); | |
mimeMsg.setFrom(new InternetAddress(emailFrom)); | |
mimeMsg.setSubject("This is a test", Charsets.UTF_8.toString()); | |
//You can obviously put more than 1 person on the mailt to | |
InternetAddress[] recipients = InternetAddress.parse(emailTo, true); | |
mimeMsg.setRecipients(Message.RecipientType.TO, recipients); | |
MimeBodyPart part = new MimeBodyPart(); | |
part.setText("<html><body><h3>Hello world!</h3</body></html>", | |
Charsets.UTF_8.toString(), | |
"html"); | |
Multipart mp = new MimeMultipart(); | |
mp.addBodyPart(part); | |
mimeMsg.setSentDate(new Date()); | |
mimeMsg.setContent(mp); | |
Transport.send(mimeMsg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment