Skip to content

Instantly share code, notes, and snippets.

@carlo-rtr
Created November 8, 2013 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlo-rtr/7371834 to your computer and use it in GitHub Desktop.
Save carlo-rtr/7371834 to your computer and use it in GitHub Desktop.
Sending Email
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 = "yo@foo.com";
String emailFrom = "yo@foo.com";
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