Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Mail using office365 account

Following is the error trace

554 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message. 16.55847:7B140000,  4.30158:DC040000 [Hostname=MWHPR22MB0815.namprd22.prod.outlook.com]
DEBUG SMTP: got response code 554, with response: 554 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message. 16.55847:7B140000, 4.30158:DC040000 [Hostname=MWHPR22MB0815.namprd22.prod.outlook.com]

RSET
250 2.0.0 Resetting
DEBUG SMTP: MessagingException while sending, THROW: 
com.sun.mail.smtp.SMTPSendFailedException: 554 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message. 16.55847:7B140000, 4.30158:DC040000 [Hostname=MWHPR22MB0815.namprd22.prod.outlook.com]

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2358)
    at com.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:2110)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1316)
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:462)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:321)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:310)
    at com.hltcapital.virtualsecretaryapi.core.JavaEmailService.sendSimpleMessage(JavaEmailService.java:22)
    at com.hltcapital.virtualsecretaryapi.DemoApplication.dataSource(DemoApplication.java:57)
    at com.hltcapital.virtualsecretaryapi.DemoApplication$$EnhancerBySpringCGLIB$$256155c4.CGLIB$dataSource$0(<generated>)
    at com.hltcapital.virtualsecretaryapi.DemoApplication$$EnhancerBySpringCGLIB$$256155c4$$FastClassBySpringCGLIB$$ad34c0c5.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361)
    at com.hltcapital.virtualsecretaryapi.DemoApplication$$EnhancerBySpringCGLIB$$256155c4.dataSource(<generated>)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:582)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1247)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1096)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:535)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.resolveBeanReference(ConfigurationClassEnhancer.java:392)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:364)
    at com.hltcapital.virtualsecretaryapi.DemoApplication$$EnhancerBySpringCGLIB$$256155c4.dataSource(<generated>)
    at com.hltcapital.virtualsecretaryapi.DemoApplication.localContainerEntityManagerFactoryBean(DemoApplication.java:76)
    at com.hltcapital.virtualsecretaryapi.DemoApplication$$EnhancerBySpringCGLIB$$256155c4.CGLIB$localContainerEntityManagerFactoryBean$1(<generated>)
    at com.hltcapital.virtualsecretaryapi.DemoApplication$$EnhancerBySpringCGLIB$$256155c4$$FastClassBySpringCGLIB$$ad34c0c5.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361)
    at com.hltcapital.virtualsecretaryapi.DemoApplication$$EnhancerBySpringCGLIB$$256155c4.localContainerEntityManagerFactoryBean(<generated>)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:582)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1247)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1096)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:535)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:859)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:330)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)
    at com.hltcapital.virtualsecretaryapi.DemoApplication.main(DemoApplication.java:111)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
QUIT
221 2.0.0 Service closing transmission channel

I am trying to send the email using java spring boot email, Below are the code blocks for the same.

1) Configuration File

@Configuration
public class EmailSendConfiguration{

@Bean
public JavaMailSender getJavaMailSender() {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setHost("m.outlook.com");
    mailSender.setPort(587);

    mailSender.setUsername("[email protected]");
    mailSender.setPassword("password");

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

    return mailSender;
}

}

2) File which is used to send email

@Service
public class JavaEmailService{

@Autowired
public JavaMailSender emailSender;

public void sendSimpleMessage(String to, String subject, String text) {

    SimpleMailMessage message = new SimpleMailMessage(); 
    message.setTo(to); 
    message.setSubject(subject); 
    message.setText(text);
    EmailSendConfiguration esc = new EmailSendConfiguration();
    esc.getJavaMailSender().send(message);
}
}

Can anyone please let me know the source of error?

like image 980
Vishrut Patel Avatar asked Sep 20 '18 19:09

Vishrut Patel


People also ask

Can I use Office 365 as an SMTP server?

SMTP relay lets Microsoft 365 or Office 365 relay emails on your behalf by using a connector that's configured with your public IP address or a TLS certificate.

Can I use IMAP with Office 365?

Office365 provides IMAP access to your Office365 account, so you can connect to your emails from your desktop email program or your mobile mail app.


2 Answers

I had the same problem, and solved it:

The above mentioned error happened because I was sending email from the email account with 'usernameX' but the sent email had property 'From' with different value than 'usernameX'. After I set email 'From' field to 'usernameX' .... the email was sent successfully.

like image 163
st100 Avatar answered Sep 22 '22 16:09

st100


I had the same issue. I add set to resolve the problem.

SimpleMailMessage msg = new SimpleMailMessage();
msg.setTo("XXXXXXX", "XXXXXXXX");
msg.setFrom("XXXXXXXX"); // missed line
msg.setSubject("Testing from Spring Boot");
msg.setText("Hello World \n Spring Boot Email");
javaMailSender.send(msg);
like image 31
Rajesh Narayanan Avatar answered Sep 22 '22 16:09

Rajesh Narayanan