-2
My application is behaving in a very strange way, when running the Spring Boot application it generates the logs stating that you sent the email using Scheduled, but when consulting my email account I realize that it did not generate any email.
The Logs messages are these;
Hibernate: select lancamento0_.codigo as codigo1_1_, lancamento0_.codigo_categoria as codigo_c8_1_, lancamento0_.data_pagamento as data_pag2_1_, lancamento0_.data_vencimento as data_ven3_1_, lancamento0_.descricao as descrica4_1_, lancamento0_.observacao as observac5_1_, lancamento0_.codigo_pessoa as codigo_p9_1_, lancamento0_.tipo as tipo6_1_, lancamento0_.valor as valor7_1_ from lancamento lancamento0_ where lancamento0_.data_vencimento<=? and (lancamento0_.data_pagamento is null)
Hibernate: select categoria0_.codigo as codigo1_0_0_, categoria0_.nome as nome2_0_0_ from categoria categoria0_ where categoria0_.codigo=?
Hibernate: select pessoa0_.codigo as codigo1_3_0_, pessoa0_.ativo as ativo2_3_0_, pessoa0_.bairro as bairro3_3_0_, pessoa0_.cep as cep4_3_0_, pessoa0_.cidade as cidade5_3_0_, pessoa0_.complemento as compleme6_3_0_, pessoa0_.estado as estado7_3_0_, pessoa0_.logradouro as logradou8_3_0_, pessoa0_.numero as numero9_3_0_, pessoa0_.nome as nome10_3_0_ from pessoa pessoa0_ where pessoa0_.codigo=?
Hibernate: select categoria0_.codigo as codigo1_0_0_, categoria0_.nome as nome2_0_0_ from categoria categoria0_ where categoria0_.codigo=?
Hibernate: select pessoa0_.codigo as codigo1_3_0_, pessoa0_.ativo as ativo2_3_0_, pessoa0_.bairro as bairro3_3_0_, pessoa0_.cep as cep4_3_0_, pessoa0_.cidade as cidade5_3_0_, pessoa0_.complemento as compleme6_3_0_, pessoa0_.estado as estado7_3_0_, pessoa0_.logradouro as logradou8_3_0_, pessoa0_.numero as numero9_3_0_, pessoa0_.nome as nome10_3_0_ from pessoa pessoa0_ where pessoa0_.codigo=?
Hibernate: select pessoa0_.codigo as codigo1_3_0_, pessoa0_.ativo as ativo2_3_0_, pessoa0_.bairro as bairro3_3_0_, pessoa0_.cep as cep4_3_0_, pessoa0_.cidade as cidade5_3_0_, pessoa0_.complemento as compleme6_3_0_, pessoa0_.estado as estado7_3_0_, pessoa0_.logradouro as logradou8_3_0_, pessoa0_.numero as numero9_3_0_, pessoa0_.nome as nome10_3_0_ from pessoa pessoa0_ where pessoa0_.codigo=?
Hibernate: select pessoa0_.codigo as codigo1_3_0_, pessoa0_.ativo as ativo2_3_0_, pessoa0_.bairro as bairro3_3_0_, pessoa0_.cep as cep4_3_0_, pessoa0_.cidade as cidade5_3_0_, pessoa0_.complemento as compleme6_3_0_, pessoa0_.estado as estado7_3_0_, pessoa0_.logradouro as logradou8_3_0_, pessoa0_.numero as numero9_3_0_, pessoa0_.nome as nome10_3_0_ from pessoa pessoa0_ where pessoa0_.codigo=?
Hibernate: select pessoa0_.codigo as codigo1_3_0_, pessoa0_.ativo as ativo2_3_0_, pessoa0_.bairro as bairro3_3_0_, pessoa0_.cep as cep4_3_0_, pessoa0_.cidade as cidade5_3_0_, pessoa0_.complemento as compleme6_3_0_, pessoa0_.estado as estado7_3_0_, pessoa0_.logradouro as logradou8_3_0_, pessoa0_.numero as numero9_3_0_, pessoa0_.nome as nome10_3_0_ from pessoa pessoa0_ where pessoa0_.codigo=?
Hibernate: select categoria0_.codigo as codigo1_0_0_, categoria0_.nome as nome2_0_0_ from categoria categoria0_ where categoria0_.codigo=?
2018-09-28 12:35:24.779 INFO 6160 --- [pool-2-thread-1] c.p.kwan.service.LancamentoService : Exitem 8 lançamentos vencidos.
Hibernate: select usuario0_.codigo as codigo1_4_, usuario0_.email as email2_4_, usuario0_.nome as nome3_4_, usuario0_.senha as senha4_4_ from usuario usuario0_ left outer join usuario_permissao permissoes1_ on usuario0_.codigo=permissoes1_.codigo_usuario left outer join permissao permissao2_ on permissoes1_.codigo_permissao=permissao2_.codigo where permissao2_.descricao=?
Hibernate: select permissoes0_.codigo_usuario as codigo_u1_5_0_, permissoes0_.codigo_permissao as codigo_p2_5_0_, permissao1_.codigo as codigo1_2_1_, permissao1_.descricao as descrica2_2_1_ from usuario_permissao permissoes0_ inner join permissao permissao1_ on permissoes0_.codigo_permissao=permissao1_.codigo where permissoes0_.codigo_usuario=?
Hibernate: select permissoes0_.codigo_usuario as codigo_u1_5_0_, permissoes0_.codigo_permissao as codigo_p2_5_0_, permissao1_.codigo as codigo1_2_1_, permissao1_.descricao as descrica2_2_1_ from usuario_permissao permissoes0_ inner join permissao permissao1_ on permissoes0_.codigo_permissao=permissao1_.codigo where permissoes0_.codigo_usuario=?
2018-09-28 12:35:30.955 INFO 6160 --- [pool-2-thread-1] c.p.kwan.service.LancamentoService : Envio de e-mail de aviso concluído.
I don’t know what’s wrong, I set up the email protocol correctly.
@Configuration
public class Mailconfig {
@Autowired
private KwanApiProperty property;
@Bean
public JavaMailSender javaMailSender() {
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", true);
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.connectiontimeout", 10000);
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setJavaMailProperties(props);
mailSender.setHost(property.getMail().getHost());
mailSender.setPort(property.getMail().getPort());
mailSender.setUsername(property.getMail().getUsername());
mailSender.setPassword(property.getMail().getPassword());
return mailSender;
}
}
The file that does everything is this;
package com.projeto.kwan.mail;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import com.projeto.kwan.model.Lancamento;
import com.projeto.kwan.model.Usuario;
import com.projeto.kwan.repository.LancamentoRepository;
@Component
public class Mailer {
@Autowired
private JavaMailSender mailSender;
@Autowired
private TemplateEngine thymeleaf;
// @Autowired
// private LancamentoRepository repo;
// @EventListener
// private void teste(ApplicationReadyEvent event) {
// String template = "mail/aviso-lancamentos-vencidos";
//
// List<Lancamento> lista = repo.findAll();
//
// Map<String, Object> variaveis = new HashMap<>();
// variaveis.put("lancamentos", lista);
//
// this.enviarEmail("[email protected]",
// Arrays.asList("[email protected]"),
// "Testando", template, variaveis);
// System.out.println("Terminado o envio de e-mail...");
// }
public void avisarSobreLancamentosVencidos(
List<Lancamento> vencidos, List<Usuario> destinatarios) {
Map<String, Object> variaveis = new HashMap<>();
variaveis.put("lancamentos", vencidos);
List<String> emails = destinatarios.stream()
.map(u -> u.getEmail())
.collect(Collectors.toList());
this.enviarEmail("[email protected]",
emails,
"Lançamentos vencidos",
"mail/aviso-lancamentos-vencidos",
variaveis);
}
public void enviarEmail(String remetente,
List<String> destinatarios, String assunto, String template,
Map<String, Object> variaveis) {
Context context = new Context(new Locale("pt", "BR"));
variaveis.entrySet().forEach(
e -> context.setVariable(e.getKey(), e.getValue()));
String mensagem = thymeleaf.process(template, context);
this.enviarEmail(remetente, destinatarios, assunto, mensagem);
}
public void enviarEmail(String remetente,
List<String> destinatarios, String assunto, String mensagem) {
try {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "UTF-8");
helper.setFrom(remetente);
helper.setTo(destinatarios.toArray(new String[destinatarios.size()]));
helper.setSubject(assunto);
helper.setText(mensagem, true);
mailSender.send(mimeMessage);
} catch (MessagingException e) {
throw new RuntimeException("Problemas com o envio de e-mail!", e);
}
}
}
The Mailer class is what does everything, where I could put the break point? you’re absolutely right about debug, but I don’t know where to put?
– wladyband
could you take a look at my post? I just updated
– wladyband
Now I was confused, because I couldn’t understand which of the two methods of sending email you are using. You can go straight to the method you are using for sending email, which actually performs the sending procedure and add the break point in the first line after the method declaration. .
– Thiago Vulcão
I just debug, he was sending the email to [email protected] , but this email does not exist, it is because my application has user authentication being authenticated from a dummy email account, to fix put real emails, it worked, thank you newbie, was right on the tape huh, brincaderia kkkkk, thank you very much for the help.
– wladyband
Hahahaha ;) thanks man!
– Thiago Vulcão