issue # smtp 설정 중
This commit is contained in:
@@ -0,0 +1,20 @@
|
|||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Component
|
||||||
|
public class MailConfig {
|
||||||
|
|
||||||
|
@Value("${spring.mail.username}")
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
@Value("${spring.mail.clientId}")
|
||||||
|
private String clientId;
|
||||||
|
|
||||||
|
@Value("${spring.mail.clientSecret}")
|
||||||
|
private String clientSecret;
|
||||||
|
|
||||||
|
@Value("${spring.mail.refershToken}")
|
||||||
|
private String refreshToken;
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package com.baekyangdan.scheduler.service.mail;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.google.auth.oauth2.AccessToken;
|
||||||
|
import com.google.auth.oauth2.UserCredentials;
|
||||||
|
|
||||||
|
import jakarta.mail.Message;
|
||||||
|
import jakarta.mail.Session;
|
||||||
|
import jakarta.mail.Transport;
|
||||||
|
import jakarta.mail.internet.InternetAddress;
|
||||||
|
import jakarta.mail.internet.MimeMessage;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class MailService {
|
||||||
|
|
||||||
|
private static final String CLIENT_ID="688417162908-iqvnj4ceb8t1dkbjr70dtcafo27m8kqe.apps.googleusercontent.com";
|
||||||
|
private static final String CLIENT_SECRET="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";
|
||||||
|
private static final String REFRESH_TOKEN="1//04_pSivNoGpPUCgYIARAAGAQSNwF-L9IrO0Kx6jSzq_eQNjdl65f0O2iqKSNpFeZ3gtIGMhOk0oiZsnKrPfWs8jvuEic1NhUoZ0g";
|
||||||
|
private static final String USER_MAIL="baekyangdan@gmail.com";
|
||||||
|
|
||||||
|
public void sendTestMail() throws Exception {
|
||||||
|
UserCredentials credentials =
|
||||||
|
UserCredentials.newBuilder()
|
||||||
|
.setClientId(CLIENT_ID)
|
||||||
|
.setClientSecret(CLIENT_SECRET)
|
||||||
|
.setRefreshToken(REFRESH_TOKEN)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
AccessToken accessToken = credentials.refreshAccessToken();
|
||||||
|
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.put("mail.smtp.auth", "true");
|
||||||
|
props.put("mail.smtp.starttls.enable", "true");
|
||||||
|
props.put("mail.smtp.host", "smtp.gmail.com");
|
||||||
|
props.put("mail.smtp.port", "587");
|
||||||
|
props.put("mail.smtp.auth.mechanisms", "XOAUTH2");
|
||||||
|
|
||||||
|
Session session = Session.getInstance(props);
|
||||||
|
Transport transport = session.getTransport("smtp");
|
||||||
|
|
||||||
|
transport.connect("smtp.gmail.com", USER_MAIL, accessToken.getTokenValue());
|
||||||
|
|
||||||
|
Message message = new MimeMessage(session);
|
||||||
|
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("lapinuit966@gmail.com"));
|
||||||
|
message.setSubject("test Mail");
|
||||||
|
message.setText("test mail");
|
||||||
|
message.setFrom(new InternetAddress(USER_MAIL));
|
||||||
|
|
||||||
|
transport.sendMessage(message, message.getAllRecipients());
|
||||||
|
transport.close();
|
||||||
|
|
||||||
|
System.out.println("메일 전송 완료");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,10 @@ spring.mail.port=587
|
|||||||
spring.mail.username=baekyangdan@gmail.com
|
spring.mail.username=baekyangdan@gmail.com
|
||||||
spring.mail.protocol=smtp
|
spring.mail.protocol=smtp
|
||||||
|
|
||||||
|
spring.mail.clientId=688417162908-iqvnj4ceb8t1dkbjr70dtcafo27m8kqe.apps.googleusercontent.com
|
||||||
|
spring.mail.clientSecret=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
|
||||||
|
spring.mail.refreshToken=1//04_pSivNoGpPUCgYIARAAGAQSNwF-L9IrO0Kx6jSzq_eQNjdl65f0O2iqKSNpFeZ3gtIGMhOk0oiZsnKrPfWs8jvuEic1NhUoZ0g
|
||||||
|
|
||||||
spring.mail.properties.mail.smtp.auth=true
|
spring.mail.properties.mail.smtp.auth=true
|
||||||
spring.mail.properties.mail.smtp.starttls.enable=true
|
spring.mail.properties.mail.smtp.starttls.enable=true
|
||||||
spring.mail.properties.mail.smtp.starttls.required=true
|
spring.mail.properties.mail.smtp.starttls.required=true
|
||||||
|
|||||||
Reference in New Issue
Block a user