Lily
Lily

Reputation: 707

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.

Upvotes: 7

Views: 4638

Answers (3)

Oleksandr
Oleksandr

Reputation: 522

Google changed some ways to do this, so now you can add a "special" password for your app.

  1. Go to Manage Your Account
  2. Go to Security
  3. Scroll down and find "Sign in to your Google account"
  4. Go to "Application Passwords"

On this page, you can create a unique pass, which needs used for login into your acc via an application.

Upvotes: 0

JAMSHAID
JAMSHAID

Reputation: 1357

  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)

Upvotes: 17

hamzaislam101
hamzaislam101

Reputation: 73

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

Upvotes: 2

Related Questions