Home > Blockchain >  href argument of anchor tag is not getting passed
href argument of anchor tag is not getting passed

Time:08-17

So I am trying to send an email using JavaMailSender, MimeMessage and MimeMessageHelper and add an unsubscribe clickable link to the end of the mail. This is my MailService method

package com.emailScheduler.emailScheduler.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.spring5.SpringTemplateEngine;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;

@Service
public class MailService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private SpringTemplateEngine springTemplateEngine;

    //Simple mail sender method
    public void sendMail(String to, String sub, String msg){
        SimpleMailMessage mailMessage = new SimpleMailMessage();

        mailMessage.setFrom("Sender Name");
        mailMessage.setTo(to);
        mailMessage.setSubject(sub);
        mailMessage.setText(msg);

        javaMailSender.send(mailMessage);
    }

    //HTML mail sender method
    public void sendMail2 (String to, String sub, String msg) throws MessagingException, UnsupportedEncodingException {

        MimeMessage mailMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);

        /*Context context = new Context();
        context.setVariables(mailModel);*/

        String html =  "<p>"   msg   "</p>"   "<a href= \"localhost:8080/unsubscribe\">unsubscribe1</a>";
        String html2 = "<p>"   msg   "</p>"   "<a href= 'localhost:8080/unsubscribe'>unsubscribe2</a>";
        String html3 = html   html2;

        messageHelper.setFrom("[email protected]", "Sender Name");
        messageHelper.setTo(to);
        messageHelper.setSubject(sub);
        messageHelper.setText(html3, true);

        System.out.println(html);
        System.out.println(html2);

        javaMailSender.send(mailMessage);
    }
}

I can successfully send the eamil but in my mail body for some reason unsubscribe is not shown as hyperlink but plain text, on inspecting in chrome browser's inspect element it shows like this <a>unsubscribe1</a>

CodePudding user response:

use the http or https scheme in the hyperlink URL. only in the case of localhost:8080 or localhost:80 or any other port, Gmail is making HTML email to text. domain.com and other domains will also work without a scheme.

HTML and body tags are optional.

String html =  "<html><body><p>"   msg   "</p>"   "<a href= 'http://localhost:8080'>unsubscribe1</a>";
        String html2 = "<p>"   msg   "</p>"   "<a href= 'http://localhost:8080'>unsubscribe2</a></body></html>";
        String html3 = html   html2;

email with hyperlink

CodePudding user response:

Try using only ' instead of doing escape char = "

  • Related