javamail에서 qq메일박스로 메일을 보내는 설정 방법
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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;
public class Test_Email_N {
public static void main(String args[]){
try {
send_email();
}catch (Exception e) {
e.printStackTrace();
}
}
public static void send_email() throws IOException, AddressException, MessagingException{
String to = "[email protected]";
String subject = "subject";
String content = "content";
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.qq.com");
properties.put("mail.smtp.port", "25");
properties.put("mail.smtp.auth", "true");
Authenticator authenticator = new Email_Authenticator("[email protected]", "password");
javax.mail.Session sendMailSession = javax.mail.Session.getDefaultInstance(properties, authenticator);
MimeMessage mailMessage = new MimeMessage(sendMailSession);
mailMessage.setFrom(new InternetAddress("[email protected]"));
// Message.RecipientType.TO TO
mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
mailMessage.setSubject(subject, "UTF-8");
mailMessage.setSentDate(new Date());
// MiniMultipart , MimeBodyPart
Multipart mainPart = new MimeMultipart();
// HTML MimeBodyPart
BodyPart html = new MimeBodyPart();
html.setContent(content.trim(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
mailMessage.setContent(mainPart);
Transport.send(mailMessage);
}
}
그 중 의존하는jar 패키지는javax입니다.mail, 여기는 마븐이 관리하는 곳입니다. 마븐으로 직접 jar 패키지를 다운로드해도 됩니다.https://java.net/projects/javamail/pages/Homejar 패키지를 직접 다운로드합니다.
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.5.0-b01</version>
</dependency>
Email_Authenticator.java, 여기는 Authenticator 클래스를 계승하여name,password를 봉인하는 데 사용됩니다.
package com.infomorrow.webtest.JuxinliTest.restdetect;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class Email_Authenticator extends Authenticator {
String userName = null;
String password = null;
public Email_Authenticator() {
}
public Email_Authenticator(String username, String password) {
this.userName = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
}
설정이 이렇게 많으니 메일박스 비밀번호를 자신의 것으로 바꾸면 됩니다. 그렇지 않으면 오류가 발생할 수 있습니다.프로그램이 여기까지 오면 실행할 수 있습니다!다음은 프로필 파일을 설정하여 계정 비밀번호를 관리하는 것입니다.
이메일을 새로 만듭니다.propertis 파일.
email.propertis:
mail.smtp.host=smtp.qq.com
mail.smtp.port=25
[email protected]
password=password
Test_Email.java 코드는 다음과 같이 변경됩니다.
package com.infomorrow.webtest.JuxinliTest.restdetect;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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;
public class Test_Email {
public static void main(String args[]){
try {
send_email();
}catch (Exception e) {
e.printStackTrace();
}
}
public static void send_email() throws IOException, AddressException, MessagingException{
String to = "[email protected]";
String subject = "subject";//
String content = "content";//
Properties properties = new Properties();
InputStream resourceAsStream = null;
try {
resourceAsStream = Object.class.getResourceAsStream("/email.properties");
properties.load(resourceAsStream);
} finally{
if (resourceAsStream!=null) {
resourceAsStream.close();
}
}
System.err.println("properties:"+properties);
properties.put("mail.smtp.host", properties.get("mail.smtp.host"));
properties.put("mail.smtp.port", properties.get("mail.smtp.port"));
properties.put("mail.smtp.auth", "true");
Authenticator authenticator = new Email_Authenticator(properties.get("username").toString(), properties.get("password").toString());
javax.mail.Session sendMailSession = javax.mail.Session.getDefaultInstance(properties, authenticator);
MimeMessage mailMessage = new MimeMessage(sendMailSession);
mailMessage.setFrom(new InternetAddress(properties.get("username").toString()));
// Message.RecipientType.TO TO
mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
mailMessage.setSubject(subject, "UTF-8");
mailMessage.setSentDate(new Date());
// MiniMultipart , MimeBodyPart
Multipart mainPart = new MimeMultipart();
// HTML MimeBodyPart
BodyPart html = new MimeBodyPart();
html.setContent(content.trim(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
mailMessage.setContent(mainPart);
Transport.send(mailMessage);
}
}
오케이, 여기까지.