Home > database >  Java Mail is it possible to show users email in from field?
Java Mail is it possible to show users email in from field?

Time:08-14

I am creating a contact form and was wondering if it is possible to show the user's email in the "from "field in a contact email box. I am using java mail api.

Now it looks like this

enter image description here

can I show users' emails instead of [email protected]? It seems like a can`t get access to a user's to account to send the mail from their account, am I right?

CodePudding user response:

Not possible to set the sender address without the email server/provider. Try replyTo Field in your code.

you can try replyTo of MimeMessage class. Here is the spring version MimeMessageHelper class, you can set it using MimeMessage also.

    public void sendEmail(Mail mail) 
    {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setSubject(mail.getMailSubject());
            mimeMessageHelper.setFrom(new InternetAddress(mail.getMailFrom()));
            mimeMessageHelper.setTo(mail.getMailTo());
            mimeMessageHelper.setText(mail.getMailContent());
            mimeMessageHelper.setReplyTo(new InternetAddress("[email protected]"));
            javaMailSender.send(mimeMessageHelper.getMimeMessage());
        } 
        catch (MessagingException e) {
            e.printStackTrace();
        }
    }

Using above code receiver will receive with replyto address on which he directly reply. Gmail preview

CodePudding user response:

Java Mail is an API to talk to the backend (Mail User Agent to Mail Transport Agent communication). It is not related to presenting the mails to users at all.

So of course it is possible to users' email on the screen, be it the sender, the recipient or else. But this is a UI rendering issue, not Java Mail. In other words: You are barking up the wrong tree.

But since you are asking:

  • Related