Skip to content

Instantly share code, notes, and snippets.

Created October 1, 2011 14:31
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save maji-KY/1256110 to your computer and use it in GitHub Desktop.
JavaMail Example
import static javax.mail.Message.RecipientType.TO;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendJavaMail {
public static void main(String[] args) throws AddressException, MessagingException, IOException {
Properties props = new Properties();
props.setProperty("", "");
props.setProperty("mail.smtp.port", "587");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.connectiontimeout", "5000");
props.setProperty("mail.smtp.timeout", "5000");
props.setProperty("mail.user", "majiky");
props.setProperty("", "");
props.setProperty("mail.debug", "true");
class PasswordAuthenticatior extends Authenticator{
private String username;
private String password;
PasswordAuthenticatior(String username, String password) {
this.username = username;
this.password = password;
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
Session session = Session.getDefaultInstance(
new PasswordAuthenticatior("majiky","**passwd**")
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(""));
msg.setSender(new InternetAddress(""));
msg.setRecipient(TO, new InternetAddress(""));
msg.setSubject("メールを送った件について", "utf-8");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment