0

i want to send below mentioned results to email and want to receive them in email carefully. therefore i decide to use html code, but no result.

caputure.png imageResult what i receive:

Code:

public static void sendresult ( ShowResultModel showuserresulttable) {

    final String username = *****
    final String password = ******;

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "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(username, password);
                }
            });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("[email protected]"));

        message.setSubject("Exammer result :" + showuserresulttable.getUsername() );

        DecimalFormat formatter = new DecimalFormat("#0.00");

         String MESSAGE = " ";
        double resultpercent  =  showuserresulttable.getScore() /(double) showuserresulttable.getTotalanswer()*100;

        MESSAGE+="<b>Username:</b> " + showuserresulttable.getUsername() + "<br><hr>";
        MESSAGE+="<b>Subject</b>" + showuserresulttable.getSubject() + "<br><hr>";
        MESSAGE+="<b>Variant :</b>" + showuserresulttable.getVariant() +   "<br><hr>";
        MESSAGE+="<b>Score of result:</b>" + showuserresulttable.getScore() + "<br><hr>";
        MESSAGE+="<b>Total question :</b>" + showuserresulttable.getTotalanswer()+ "<br><hr>";
        MESSAGE+="<b> Score percent:</b>" + resultpercent + "<br><hr>";
        MESSAGE+="<b>Spendtime:</b>" + showuserresulttable.getSpendtime() + "second" + "<br><hr>";
        MESSAGE+="<b>Examdate:</b>" + showuserresulttable.getExamdate() ;

        message.setText(MESSAGE);
        Transport.send(message);

        System.out.println("Mail gonderildi!");
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }

} 
1

1 Answer 1

2

instead of message.setText(MESSAGE); you should use message.setText(MESSAGE, true); here true is represent html (whether to apply content type "text/html" for an HTML mail, using default content type ("text/plain") else)

Also if you are using thymeleaf or freemarker then u can used there template.

Sign up to request clarification or add additional context in comments.

8 Comments

i try to what do you advice, but when i write true in class i get red underline error, "Set text string in part cannot be applied", what to do?
Do you user spring javacv mail dependency or else ?
i dont use spring. i have coded in ide intellejis , imported below mentioned jars: import java.text.DecimalFormat; import java.util.List; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage;
Please add this line. 'message.setContent(MESSAGE, "text/html; charset=utf-8");' also please remove message.setText (message):; line
Please add this line. 'message.setContent(MESSAGE, "text/html; charset=utf-8");' also please remove message.setText (message):; line
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.