0

The "loginVo.htmlBody(messageBodyPart);" will contain the html formatted designed information, but in mail does not receive it.

JAVA - STRUTS2

package com.action;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import com.opensymphony.xwork2.Action;
import com.bo.LoginBo;
import com.manager.AttendanceManager;
import com.manager.LoginManager;
import com.manager.SSLEmail;
import com.vo.AttendanceManagementVo;
import com.vo.LeaveManagementVo;
import com.vo.LoginVo;
import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeBodyPart;

public class InsertApplyLeaveAction implements Action {
private AttendanceManagementVo attendanceManagementVo;

public AttendanceManagementVo getAttendanceManagementVo() {
    return attendanceManagementVo;
}

public void setAttendanceManagementVo(
        AttendanceManagementVo attendanceManagementVo) {
    this.attendanceManagementVo = attendanceManagementVo;
}

@Override
public String execute() throws Exception {
    String empId=attendanceManagementVo.getEmpId();
    String leaveType=attendanceManagementVo.getLeaveType();
    String leaveStartDate=attendanceManagementVo.getLeaveStartDate();
    String leaveEndDate=attendanceManagementVo.getLeaveEndDate();
    String reason=attendanceManagementVo.getReason();
    String employeeName=attendanceManagementVo.getEmployeeName();
    String manageEmployeeId=empId;
    float totalLeave=attendanceManagementVo.getTotalLeave();
    String leaveStatus=attendanceManagementVo.getLeaveStatus();
//  String approverId=attendanceManagementVo.getApproverId();
    attendanceManagementVo.setEmpId(empId);
    attendanceManagementVo.setLeaveType(leaveType);
    attendanceManagementVo.setLeaveStartDate(leaveStartDate);
    attendanceManagementVo.setLeaveEndDate(leaveEndDate);
    attendanceManagementVo.setReason(reason);
    attendanceManagementVo.setManageEmployeeId(manageEmployeeId);
    attendanceManagementVo.setTotalLeave(totalLeave);
    attendanceManagementVo.setLeaveStatus(leaveStatus);
    attendanceManagementVo.setEmployeeName(employeeName);

    AttendanceManagementVo attendanceManagementVo1=new AttendanceManagementVo();
    AttendanceManager attendanceManager=new AttendanceManager();    
    attendanceManagementVo1=attendanceManager.insertLeaveData(attendanceManagementVo);
    attendanceManagementVo1=attendanceManager.getApproverId(attendanceManagementVo);
    String approverId=attendanceManagementVo1.getApproverId();
    String approverEmployeeName=attendanceManagementVo1.getApproverEmployeeName();
    LoginVo loginVo=new LoginVo();
    LoginManager loginManager=new LoginManager();
    loginVo.setEmpId(approverId);
    loginVo=loginManager.getEmailAddress(loginVo);
    String emailAddress=loginVo.getEmailAddress();
    String subject="LEAVE IS SUBMITTED FOR AN APPROVAL BY THE  - " +employeeName;
//  String body =   "Hi "+approverEmployeeName+" ," + "\n" + "\n" +
//          leaveType+" is Applied for "+totalLeave+" days by the  " +employeeName+ "\n" + "\n" +
//          " Employee Name: " + employeeName +"\n" +
//          " Applied Leave Type: " + leaveType +"\n" +
//          " Total Days: " + totalLeave +"\n" + "\n" +
  //        " To view Leave History, Please visit the employee poratal or copy and paste the below link in your browser: " + "\n" +  

  //        " NOTE : This is an automated message. Please do not reply."+ "\n" +  "\n" +                        

    Session session = Session.getInstance(null, null);

    MimeBodyPart messageBodyPart = new MimeBodyPart();
    MimeMessage message = new MimeMessage(session);
    Multipart multipart = new MimeMultipart();

    String htmlText = ("<div style=\"color:red;\">BRIDGEYE</div>");
    messageBodyPart.setContent(htmlText, "text/html");      
    loginVo.setHtmlBody(messageBodyPart);       
    message.setContent(multipart);
    Transport.send(message);
    loginVo.setSubject(subject);
//  loginVo.setBody(body);
    loginVo.setEmailAddress(emailAddress);
    SSLEmail sSSEmail=new SSLEmail();
    sSSEmail.sendEmail(loginVo);
    return "success";
 }

 }

If we comment that "loginVo.setHtmlBody(messageBodyPart);" part from coding and uncomment the "String body" means email is sending properly with a plain text.

By using the Above code as like it means it showing the

Struts has detected an unhandled exception: Messages: File: javax/mail/Session.java Line number: 206 "ABOVE IS A ERROR SHOWING WHILE RUNNING THE CODE"

  • 1
    I think you miss this : [Declaring mime type for html-email](http://stackoverflow.com/questions/1666098/declaring-mime-type-for-html-email) – superbob Jul 07 '14 at 12:30
  • Thank for your answer, but in the above link the more details are not given, it just telling to use the "Content-Type: text/html; charset=UTF-8" only. I just tried with that also, still it's send the plain text only.. – Pradap Adwani A Jul 08 '14 at 11:59
  • You don't tell us how you send email (what code, which API), you have to give us more details if you want some more precise help. What I gave you is more an idea than a precise answer... – superbob Jul 08 '14 at 14:44
  • please see the below post, there i attached the full coding.. – Pradap Adwani A Jul 09 '14 at 11:51

1 Answers1

0

To send your message as HTML you should do something

messageBodyPart = new MimeBodyPart();
String htmlText = body.replaceAll("\n","<br>");
messageBodyPart.setContent(htmlText, "text/html");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • messageBodyPart = new MimeBodyPart(); with that we can able to attach the files only rights? how can we use that for an html design in email body?? – Pradap Adwani A Jul 08 '14 at 12:04
  • How could I know what kind of design do you want? I have given you a solution to use HTML instead of plain text to format your message. – Roman C Jul 08 '14 at 13:09
  • I just need a HTML design only instead of plain text.But using the above code some error are showing...It does not convert the plain text to the html design in received "EMAIL" still it sending a plain text message in a "EMAIL" . We need to import any jar files? – Pradap Adwani A Jul 09 '14 at 10:12
  • Post the stacktrace. I have converted the plain text from your post and it doesn't have any errors. – Roman C Jul 09 '14 at 10:20
  • String subject="LEAVE IS SUBMITTED FOR AN APPROVAL BY THE - " +employeeName; Session session = null; MimeBodyPart messageBodyPart = new MimeBodyPart(); MimeMessage message = new MimeMessage(session); Multipart multipart = new MimeMultipart(); String htmlText = ("
    BRIDGEYE
    "); messageBodyPart.setContent(htmlText, "text/html"); loginVo.setHtmlBody(messageBodyPart); message.setContent(multipart); Transport.send(message); loginVo.setSubject(subject); SSLEmail sSSEmail=new SSLEmail(); sSSEmail.sendEmail(loginVo); return "success"; } }
    – Pradap Adwani A Jul 09 '14 at 11:26
  • mail is not going with a value "HtmlBody" – Pradap Adwani A Jul 09 '14 at 11:26
  • Don't post a code in comments, edit a question and add _relevant_ details. You can also post a new question and accept this answer because you got difference between plain text and html messages. – Roman C Jul 09 '14 at 11:44
  • Struts has detected an unhandled exception: Messages: File: javax/mail/Session.java Line number: 206 "ABOVE IS A ERROR SHOWING WHILE RUNNING THE CODE" – Pradap Adwani A Jul 09 '14 at 11:49
  • @PradapAdwaniA If you have another question you can post it by using a link [above](http://stackoverflow.com/questions/ask) don't mess up different questions altogether. If this answer helped you to move further you should accept and upvote it. If you newbye here and don't know how it works see [About](http://stackoverflow.com/about). – Roman C Jul 10 '14 at 16:14