Stack Exchange
Send Mail from any dummy account without enabling less secure apps option in java
 **Current code**    

    @Autowired
    private JavaMailSender emailSender;
    MimeMessage message = emailSender.createMimeMessage();      
    MimeMessageHelper helper = new MimeMessageHelper(message , true);
    helper.setTo(InternetAddress.parse(userData.getEmailId())); 
    helper.setSubject("Subject");
    helper.setText("some text");
    emailSender.send(message);

application.properties:
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.protocol=smtp
spring.mail.defaultEncoding=UTF-8
spring.mail.username=dummy@gmail.com //only gmail id is working as i have enabled less secure app
spring.mail.password= password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

This code can send email from only gmail (enabled for less secure apps) ,it is able to send mail but what i want is that mail should be sent any corporate id rather than gmail , tried adding other email id in this code but getting error as "Username and Password not accepted" , can anybody suggest what changes do i need to do to make it work for any email id ? please suggest best approach if any ?

Your question isn't very clear. The spring.mail.* settings configure how spring will send the email - what SMTP server it'll use and the authentication for it. You can configure any SMTP server as long as you provide the correct connection details. You cannot connect to gmail smtp with a non-gmail user, so it is logical that you cannot use spring.mail.username=somebody@else.domain to connect to smtp.gmail.com.

Even I tried with spring.mail.host=smtp.office365.com but still mail is not getting sent from office365 Id ,is any other issue with this ?

You should post the exception you are getting. Here are the configuration settings for office365: https://learn.microsoft.com/en-us/exchange/mail-flow-best-practices/how-to-set-up-a-multifunction-device-or-application-to-send-email-using-microsoft-365-or-office-365#how-to-set-up-smtp-auth-client-submission

This is not possible with Gmail. Gmail only allows you to send emails from your own gmail account, because if they allowed other accounts, then that would effectively turn gmail into your personal general purpose mail server, which it is not.
You will need to use a proper mail server or set up your own. Alternatively, you could have a look at integrating your own java mail server into your application (which I don't recommend).
