How to send an HTML letter?

I successfully sent an email in my web application using JMS, but the result is only displayed in plain text. I want the content to display html. How should I do it? Here is roughly what I have:

Message msg = new MimeMessage(mailSession); try{ msg.setSubject("Test Notification"); msg.setRecipient(Message.RecipientType.TO, new InternetAddress(sentTo, false)); String message = "<div style=\"color:red;\">BRIDGEYE</div>"; msg.setContent(message, "text/html; charset=utf-8"); msg.setSentDate(new Date()); Transport.send(msg); }catch(MessagingException me){ logger.log(Level.SEVERE, "sendEmailNotification: {0}", me.getMessage()); } 
+105
java email javamail
Feb 21 '11 at 16:58
source share
10 answers

According to Javadoc, MimeMessage#setText() sets the mime type to text/plain by default, while you need text/html . Use MimeMessage#setContent() .

 message.setContent(someHtmlMessage, "text/html; charset=utf-8"); 

For more information see:

+240
Feb 21 2018-11-21T00:
source share

Set content type. Take a look at this method .

 message.setContent("<h1>Hello</h1>", "text/html"); 
+18
Feb 21 '11 at 17:05
source share

If you use the Google engine to work with Google / Java, use the following ...

 MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(SENDER_EMAIL_ADDRESS, "Admin")); msg.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress, "user"); msg.setSubject(subject,"UTF-8"); Multipart mp = new MimeMultipart(); MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(message, "text/html"); mp.addBodyPart(htmlPart); msg.setContent(mp); Transport.send(msg); 
+12
Nov 26 '12 at 12:44
source share

Take a look at http://commons.apache.org/email/ , they have an HtmlEmail class that probably does exactly what you need.

+3
Feb 21 '11 at 17:05
source share

you need to call

 msg.saveChanges(); 

after setting the content type.

+3
Nov 17 '13 at 17:58
source share

Starting with version 1.4 of JavaMail, there is an overload of the setText method, which accepts a subtype.

 // Passing null for second argument in order for the method to determine // the actual charset on-the fly. // If you know the charset, pass it. "utf-8" should be fine msg.setText( message, null, "html" ); 
+3
Mar 30 '17 at 17:38
source share

You can find a complete and very simple java class for sending email using a Google account (gmail) here, Send an email using a java application

It uses the following properties

 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"); 
0
Sep 16 '13 at 10:00
source share

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

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 = 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"; } } 
0
Jul 09 '14 at 11:33
source share

I found this method not sure if it works for all CSS primitives

By setting the header property "Content-Type" to "text / html"

 mimeMessage.setHeader("Content-Type", "text/html"); 

now i can do something like

 mimeMessage.setHeader("Content-Type", "text/html"); mimeMessage.setText ("`<html><body><h1 style =\"color:blue;\">My first Header<h1></body></html>`") 

Hello

0
Sep 04 '17 at 20:35
source share

A quick, easy way to do this is to create a web page (for example, create an HTML file on your desktop or something else and edit it with a simple text editor). Then open this file and copy everything on the web page. When you paste this into an email, it should work.

I do this a lot for the monthly periodical, which I edit and publish. Be careful not to link to anything stored on your computer, all links must be accessible online

0
Dec 17 '18 at 11:00
source share



All Articles