자바 메 일 발송 상세 설명 실현
javax.mail
<dependency >
<groupId >com.sun.mail</groupId >
<artifactId >javax.mail</artifactId >
<version >1.6.0</version >
</dependency >
그리고 메 일 을 보 내 는 데 필요 한 실체 류 를 구성 합 니 다.
package com.email;
import java.io.Serializable;
/**
* @Author zjt
* @Date 2019 03 07 10:37
*/
public class EmailEntity implements Serializable {
private static final long serialVersionUID = 1L;
//
private String host;
//
private Integer port;
//
private String userName;
//
private String password;
//
private String fromAddress;
//
private String toAddress;
//
private String subject;
//
private String context;
//
private String contextType;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFromAddress() {
return fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String getToAddress() {
return toAddress;
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
public String getContextType() {
return contextType;
}
public void setContextType(String contextType) {
this.contextType = contextType;
}
}
그 다음으로 메 일 발송 방법 을 작성 합 니 다.
package com.email;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.*;
/**
* @Author zjt
* @Date 2019 03 07 10:38
*/
public class EmailSend {
public static boolean EmailSendTest(EmailEntity emailEntity){
try {
//
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.host", emailEntity.getHost());
properties.put("mail.smtp.port", 25);
properties.put("mail.smtp.starrttls.enable", "true");
//
VerifyEmail verifyEmail = new VerifyEmail(emailEntity.getUserName(), emailEntity.getPassword());
Session mailSession = Session.getInstance(properties, verifyEmail);
mailSession.setDebug(true);
//
Message message = new MimeMessage(mailSession);
InternetAddress from = new InternetAddress(emailEntity.getFromAddress());
InternetAddress to = new InternetAddress(emailEntity.getToAddress());
//
message.setFrom(from);
//
message.setRecipient(MimeMessage.RecipientType.TO, to);
message.setSubject(emailEntity.getSubject());
//
message.setSentDate(new Date());
//
message.setContent(emailEntity.getContext() , emailEntity.getContextType());
message.saveChanges();
//
Transport transport = mailSession.getTransport("smtp");
transport.connect(emailEntity.getHost(), emailEntity.getUserName(), emailEntity.getPassword());
System.out.println(" :" + transport);
transport.sendMessage(message, message.getAllRecipients());
System.out.println("success");
return true;
} catch (MessagingException e) {
e.printStackTrace();
System.out.println("fial...");
return false;
}
}
}
메 일 발송 방법 을 호출 할 때 메 일 로그 인 이름과 비밀번호 가 올 바른 지 검증 하 는 방법 에 사용 합 니 다.
package com.email;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
*
* @Author zjt
* @Date 2019 03 07 10:32
*/
public class VerifyEmail extends Authenticator {
//
private String userName;
//
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//
public VerifyEmail(){
super();
}
public VerifyEmail(String userName, String password) {
super();
this.userName = userName;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(userName, password);
}
}
테스트 클래스 를 작성 하여 메 일 발송 방법 이 성 공 했 는 지 테스트 합 니 다.
package com.email;
import org.junit.jupiter.api.Test;
/**
* @Author zjt
* @Date 2019 03 07 10:26
*/
public class TestEmail {
@Test
public void test(){
EmailEntity email = new EmailEntity();
email.setUserName("*******@163.com");
email.setPassword("******");
email.setHost("smtp.163.com");
email.setPort(25);
email.setFromAddress("******@163.com");
email.setToAddress("******@163.com");
email.setSubject(" !!!!");
email.setContext(" ");
email.setContextType("text/html;charset=utf-8");
boolean flag = EmailSend.EmailSendTest(email);
System.err.println(" =="+flag);
}
}
여기 서 테스트 한 163 메 일 로 보 냅 니 다.주의해 야 할 것 은 로그 인 비밀번호 가 아니 라 설정 중인 클 라 이언 트 인증 비밀번호 입 니 다.테스트 파일 을 실행 하면 메 일 에 로그 인하 여 보 낸 결 과 를 볼 수 있 습 니 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.