Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send email via smtp.gmail in Spring Faremework

I am working on sending the email via smtp.gmail in Spring boot.

EmailConfig.java

@Configuration
public class EmailConfig
{
    @Bean
    public JavaMailSender getJavaMailSender()
    {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost("smtp.gmail.com");
        mailSender.setPort(25);

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

        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;
    }

    @Bean
    public SimpleMailMessage emailTemplate()
    {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo("[email protected]");
        message.setFrom("[email protected]");
        message.setText("FATAL - Application crash. Save your job !!");
        return message;
    }
}

SendEmailService.java

@Service("emailService")
public class SendEmailService {

    @Autowired
    JavaMailSender mailSender;

    @Autowired
    private SimpleMailMessage preConfiguredMessage;

    public void sendPreConfiguredMail(String message)
    {
        SimpleMailMessage mailMessage = new SimpleMailMessage(preConfiguredMessage);
        mailMessage.setText(message);
        mailSender.send(mailMessage);
    }
}

Error : I have received an email on my account "Sign-in attempt was blocked" Someone just used your password to try to sign in to your account from a non-Google app. Google blocked them, but you should check what happened. Review your account activity to make sure no one else has access

How to figure out this.

like image 608
Lily Avatar asked Dec 04 '19 16:12

Lily


2 Answers

  1. Go to gmail.com
  2. Click on your profile picture and goto Manage Your account
  3. On the new page, go to the Security tab
    enter image description here
  4. Scroll down and turn-on less secure app access
    enter image description here
  5. Confirm the access in your email(optional, sometimes needed only)
like image 106
JAMSHAID Avatar answered Sep 21 '22 00:09

JAMSHAID


just go to your account security and allow less secure apps to "ON"

like image 25
hamzaislam101 Avatar answered Sep 20 '22 00:09

hamzaislam101