Trying to send a normal request to my email through the contact form!

Everything turns out but in the message that I receive in the email that the message was sent from me (from my Gmail) and not the one that is specified in the form and in this code. When displaying the object on the console, I get all the data that was transferred through the client.

Why, when viewing the soap of someone from whom the message came, is it written from myself?

public static boolean sendMessage(ContactFormMessage formMessage){ String username = formMessage.getNickName(); System.out.println(username); String email = formMessage.getEmail(); System.out.println(email); // Проблема здесь! String ms = formMessage.getMessage(); System.out.println(ms); Properties props = new Properties(); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(recipientUserName,recipientPassword); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(email)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientUserName)); message.setSubject("Testing from "+username); message.setText(ms); Transport.send(message); } catch (MessagingException e) { throw new RuntimeException(e); } return true; } 

}

  • are you a username too? - Senior Pomidor

1 answer 1

because you in the FROM field complain about the wrong value:

 String email = formMessage.getEmail(); //... message.setFrom(new InternetAddress(email)); 

it is necessary so:

 message.setRecipients(Message.RecipientType.TO, email); //получатель message.setFrom(new InternetAddress(recipientUserName); //отправитель 

Due to spam and phishing filters, you cannot send emails on behalf of others.