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("mekteb48@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("iismayilov028@gmail.com"));

        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);
    }

} 
Islam Ismayilov
  • 111
  • 1
  • 11
  • 1
    Possible duplicate of [How do I send an HTML email?](http://stackoverflow.com/questions/5068827/how-do-i-send-an-html-email) – Jorge Campos Apr 24 '17 at 18:43

1 Answers1

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.

Bhushan Uniyal
  • 5,575
  • 2
  • 22
  • 45
  • 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? – Islam Ismayilov Apr 24 '17 at 19:55
  • Do you user spring javacv mail dependency or else ? – Bhushan Uniyal Apr 24 '17 at 20:00
  • 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; – Islam Ismayilov Apr 24 '17 at 20:01
  • Please add this line. 'message.setContent(MESSAGE, "text/html; charset=utf-8");' also please remove message.setText (message):; line – Bhushan Uniyal Apr 24 '17 at 20:05
  • Please add this line. 'message.setContent(MESSAGE, "text/html; charset=utf-8");' also please remove message.setText (message):; line – Bhushan Uniyal Apr 24 '17 at 20:10
  • i try , but got error again : String can be applied in part to String, java.lang.String :-( – Islam Ismayilov Apr 24 '17 at 20:11
  • Do you mean by this line message.setContent(MESSAGE, "text/html; charset=utf-8"); ? – Bhushan Uniyal Apr 24 '17 at 20:13
  • great job, thanks a lot, i did it . i am greatful to you. – Islam Ismayilov Apr 24 '17 at 20:19