Skip to content

Instantly share code, notes, and snippets.

@freeeve
Created October 4, 2011 01:57
Show Gist options
  • Save freeeve/1260739 to your computer and use it in GitHub Desktop.
Save freeeve/1260739 to your computer and use it in GitHub Desktop.
JavaMail simple text sample
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
// Send a simple, single part, text/plain e-mail
public class TestEmail {
public static void main(String[] args) {
String to = "sendToMailAddress";
String from = "sendFromMailAddress";
String host = "infoa.com";
Properties props = new Properties();
props.put("mail.smtp.host", host);
Session session = Session.getInstance(props);
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("test email subject");
// Set message body
msg.setText("test email body.\n" +
"test email body line 2.");
Transport.send(msg);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment