Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication failed #684

Open
sunwon12 opened this issue Feb 16, 2024 · 1 comment
Open

Authentication failed #684

sunwon12 opened this issue Feb 16, 2024 · 1 comment

Comments

@sunwon12
Copy link

i write code with correct. But Authentication failed. why?? pleaze teach me

org.springframework.mail.MailAuthenticationException: Authentication failed
	at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:440) ~[spring-context-support-6.0.11.jar:6.0.11]
	at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:361) ~[spring-context-support-6.0.11.jar:6.0.11]
	at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:378) ~[spring-context-support-6.0.11.jar:6.0.11]
	at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:366) ~[spring-context-support-6.0.11.jar:6.0.11]
	at sunwon12.sendingEmail.MailSender.sendEmail(MailSender.java:30) ~[classes/:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-6.0.11.jar:6.0.11]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:196) ~[spring-aop-6.0.11.jar:6.0.11]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-6.0.11.jar:6.0.11]
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:756) ~[spring-aop-6.0.11.jar:6.0.11]
	at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115) ~[spring-aop-6.0.11.jar:6.0.11]
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[na:na]
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[na:na]
	at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Caused by: jakarta.mail.AuthenticationFailedException: 535 5.7.8  Authentication credentials invalid

Gradle

testImplementation("com.icegreen:greenmail-junit5:2.0.0")

application-test.yml

email:
  host: localhost
  port: 3025
  id: test
  password: test

MailConfig

@Configuration
public class MailConfig {

    @Value("${email.id}")
    private String fromId;

    @Value("${email.password}")
    private String password;

    @Value("${email.port}")
    private int port;

    @Value("${email.host}")
    private String host;

    @Bean
    public JavaMailSender getJavaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(host);
        mailSender.setPort(port);
        mailSender.setUsername(fromId);
        mailSender.setPassword(password);
        mailSender.setDefaultEncoding("UTF-8");

        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "true");
       // props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.debug", "true");
        return mailSender;
    }}

MailSender

@Service
@RequiredArgsConstructor
public class MailSender {

    private final JavaMailSender mailSender;

    @Value("${email.id}")
    private String fromId;

    @Async("mailExecutor")
    public void sendEmail(String to, String subject, String content) {
        MimeMessagePreparator messagePreparator =
                mimeMessage -> {
                    final MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
                    helper.setFrom(fromId);
                    helper.setTo(to);
                    helper.setSubject(subject);
                    helper.setText(content, true);
                };
        mailSender.send(messagePreparator);
    }
}

MailSenderTest

@SpringBootTest
class MailSenderTest {


    @RegisterExtension
     static GreenMailExtension stmp = new GreenMailExtension(new ServerSetup(3025, "localhost", ServerSetup.PROTOCOL_SMTP))
            .withConfiguration(GreenMailConfiguration.aConfig().withUser("test", "test"))
            .withPerMethodLifecycle(false);


    @Autowired
    MailSender mailSender;


    @Test
    public void 이메일_발송_테스트() throws MessagingException, IOException{


        String content = "Have a good day";
        String to = "[email protected]";

        mailSender.sendEmail(to, "Hello Green Mail", content);
        stmp.waitForIncomingEmail(15000, 1); // 비동기로 동작하기 때문에 대기하도록 하였다.

        MimeMessage receivedMessage = stmp.getReceivedMessages()[0];

        try {
            assertEquals(GreenMailUtil.getBody(receivedMessage),content);
            assertEquals(receivedMessage.getAllRecipients()[0].toString(),to);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}
@marcelmay
Copy link
Member

Hi @sunwon12 !

Can you try to configure the test user including the test email:

    @RegisterExtension
    GreenMailExtension greenMail = new GreenMailExtension()
        .withConfiguration(GreenMailConfiguration.aConfig()
            .withUser("[email protected]" /* Provide email */, "test", "test"));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants