<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.5.0-b01</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>
import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailSend {
public static void main(String[] args) {
new MailSend().send();
}
public void send() {
System.out.println("Sending email......");
try {
// (1) Setting..
Properties property = new Properties();
property.put("mail.smtp.host", "smtp.gmail.com");
property.put("mail.smtp.auth", "true");
property.put("mail.smtp.starttls.enable", "true");
property.put("mail.smtp.host", "smtp.gmail.com");
property.put("mail.smtp.port", "587");
property.put("mail.smtp.debug", "true");
// (2) gmail 계정과 패스워드 입력
Session session = Session.getInstance(property, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("#[my gmail account]", "#[my gmail password]") ;
}
});
MimeMessage mimeMessage = new MimeMessage(session);
// (3) 보네는 사람 ( 이메일 Address, 보네는이 이름 )
InternetAddress fromAddress = new InternetAddress("#[mymail@gmail.com]", "#[맹꽁이가]");
mimeMessage.setFrom(fromAddress);
// (4) 받는 사람 ( 이메일 Address, 받는사람 이름 )
InternetAddress toAddress = new InternetAddress("#[yourmail@naver.com]", "#[멍멍이한테]");
mimeMessage.setRecipient(Message.RecipientType.TO, toAddress);
// (5) 이메일의 타이틀 입력
mimeMessage.setSubject("#[메일제목]", "UTF-8");
// (6) 이메일의 본문내용 입력
mimeMessage.setText("#[메일 내용]", "UTF-8");
// (7) 메일 전송
Transport.send(mimeMessage);
System.out.println("Success");
} catch (Exception e) {
e.printStackTrace();
}
}
}