How do I send an HTML email?
up vote
105
down vote
favorite
I have successfully sent email in my web application using JMS, but the result only displays in plain text. I want the content to be able to display html. How do 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());
}
java email javamail
add a comment |
up vote
105
down vote
favorite
I have successfully sent email in my web application using JMS, but the result only displays in plain text. I want the content to be able to display html. How do 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());
}
java email javamail
add a comment |
up vote
105
down vote
favorite
up vote
105
down vote
favorite
I have successfully sent email in my web application using JMS, but the result only displays in plain text. I want the content to be able to display html. How do 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());
}
java email javamail
I have successfully sent email in my web application using JMS, but the result only displays in plain text. I want the content to be able to display html. How do 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());
}
java email javamail
java email javamail
edited Jun 4 '16 at 20:00
Dave Jarvis
20.8k30130253
20.8k30130253
asked Feb 21 '11 at 16:58
Thang Pham
14.5k69178275
14.5k69178275
add a comment |
add a comment |
9 Answers
9
active
oldest
votes
up vote
240
down vote
accepted
As per the Javadoc, the MimeMessage#setText()
sets a default mime type of text/plain
, while you need text/html
. Rather use MimeMessage#setContent()
instead.
message.setContent(someHtmlMessage, "text/html; charset=utf-8");
For additional details, see:
- GMail Media Queries
- GMail CSS Design
- CSS support in mail clients
Thanks, at first I did not read carefully, I have setText and setContent together, so it does not work, but now after taking out setText(), it work now. Thank you.
– Thang Pham
Feb 21 '11 at 17:47
add a comment |
up vote
18
down vote
Set content type. Look at this method.
message.setContent("<h1>Hello</h1>", "text/html");
For Groovy. Don't forget to convertGString
tojava.lang.String
.
– it3xl
Mar 6 at 8:29
add a comment |
up vote
12
down vote
If you are using Google app engine/Java, then 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);
msg.setSubject(subject,"UTF-8");
does not work. It should bemsg.setSubject(subject);
– Fizer Khan
Sep 9 '14 at 17:08
@FizerKhan - really? docs.oracle.com/javaee/6/api/javax/mail/internet/…
– Nick Grealy
Mar 31 '15 at 3:49
add a comment |
up vote
3
down vote
Take a look at http://commons.apache.org/email/ they have an HtmlEmail class that probably does exactly what you need.
commons.apache.org/proper/commons-email/apidocs/org/apache/…
– Anand Rockzz
Apr 30 '17 at 22:31
add a comment |
up vote
3
down vote
you have to call
msg.saveChanges();
after setting content type.
add a comment |
up vote
3
down vote
Since JavaMail version 1.4, there is an overload of setText
method that accepts the 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" );
add a comment |
up vote
0
down vote
You can find a complete and very simple java class for sending emails using Google(gmail) account here,
Send email message using java application
It uses 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");
2
This doesn't at all address the question. The OP was asking to send specifically HTML emails.
– Bassinator
Nov 10 '17 at 2:33
add a comment |
up vote
0
down vote
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 = 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";
}
}
add a comment |
up vote
0
down vote
I found this way not sure it works for all the CSS primitives
By setting the header property "Content-Type" to "text/html"
mimeMessage.setHeader("Content-Type", "text/html");
now I can do stuff like
mimeMessage.setHeader("Content-Type", "text/html");
mimeMessage.setText ("`<html><body><h1 style ="color:blue;">My first Header<h1></body></html>`")
Regards
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5068827%2fhow-do-i-send-an-html-email%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
240
down vote
accepted
As per the Javadoc, the MimeMessage#setText()
sets a default mime type of text/plain
, while you need text/html
. Rather use MimeMessage#setContent()
instead.
message.setContent(someHtmlMessage, "text/html; charset=utf-8");
For additional details, see:
- GMail Media Queries
- GMail CSS Design
- CSS support in mail clients
Thanks, at first I did not read carefully, I have setText and setContent together, so it does not work, but now after taking out setText(), it work now. Thank you.
– Thang Pham
Feb 21 '11 at 17:47
add a comment |
up vote
240
down vote
accepted
As per the Javadoc, the MimeMessage#setText()
sets a default mime type of text/plain
, while you need text/html
. Rather use MimeMessage#setContent()
instead.
message.setContent(someHtmlMessage, "text/html; charset=utf-8");
For additional details, see:
- GMail Media Queries
- GMail CSS Design
- CSS support in mail clients
Thanks, at first I did not read carefully, I have setText and setContent together, so it does not work, but now after taking out setText(), it work now. Thank you.
– Thang Pham
Feb 21 '11 at 17:47
add a comment |
up vote
240
down vote
accepted
up vote
240
down vote
accepted
As per the Javadoc, the MimeMessage#setText()
sets a default mime type of text/plain
, while you need text/html
. Rather use MimeMessage#setContent()
instead.
message.setContent(someHtmlMessage, "text/html; charset=utf-8");
For additional details, see:
- GMail Media Queries
- GMail CSS Design
- CSS support in mail clients
As per the Javadoc, the MimeMessage#setText()
sets a default mime type of text/plain
, while you need text/html
. Rather use MimeMessage#setContent()
instead.
message.setContent(someHtmlMessage, "text/html; charset=utf-8");
For additional details, see:
- GMail Media Queries
- GMail CSS Design
- CSS support in mail clients
edited Sep 13 at 19:57
Dave Jarvis
20.8k30130253
20.8k30130253
answered Feb 21 '11 at 17:06
BalusC
839k29531073189
839k29531073189
Thanks, at first I did not read carefully, I have setText and setContent together, so it does not work, but now after taking out setText(), it work now. Thank you.
– Thang Pham
Feb 21 '11 at 17:47
add a comment |
Thanks, at first I did not read carefully, I have setText and setContent together, so it does not work, but now after taking out setText(), it work now. Thank you.
– Thang Pham
Feb 21 '11 at 17:47
Thanks, at first I did not read carefully, I have setText and setContent together, so it does not work, but now after taking out setText(), it work now. Thank you.
– Thang Pham
Feb 21 '11 at 17:47
Thanks, at first I did not read carefully, I have setText and setContent together, so it does not work, but now after taking out setText(), it work now. Thank you.
– Thang Pham
Feb 21 '11 at 17:47
add a comment |
up vote
18
down vote
Set content type. Look at this method.
message.setContent("<h1>Hello</h1>", "text/html");
For Groovy. Don't forget to convertGString
tojava.lang.String
.
– it3xl
Mar 6 at 8:29
add a comment |
up vote
18
down vote
Set content type. Look at this method.
message.setContent("<h1>Hello</h1>", "text/html");
For Groovy. Don't forget to convertGString
tojava.lang.String
.
– it3xl
Mar 6 at 8:29
add a comment |
up vote
18
down vote
up vote
18
down vote
Set content type. Look at this method.
message.setContent("<h1>Hello</h1>", "text/html");
Set content type. Look at this method.
message.setContent("<h1>Hello</h1>", "text/html");
edited May 6 '13 at 14:40
Doug Porter
6,75843253
6,75843253
answered Feb 21 '11 at 17:05
CoolBeans
18.1k107094
18.1k107094
For Groovy. Don't forget to convertGString
tojava.lang.String
.
– it3xl
Mar 6 at 8:29
add a comment |
For Groovy. Don't forget to convertGString
tojava.lang.String
.
– it3xl
Mar 6 at 8:29
For Groovy. Don't forget to convert
GString
to java.lang.String
.– it3xl
Mar 6 at 8:29
For Groovy. Don't forget to convert
GString
to java.lang.String
.– it3xl
Mar 6 at 8:29
add a comment |
up vote
12
down vote
If you are using Google app engine/Java, then 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);
msg.setSubject(subject,"UTF-8");
does not work. It should bemsg.setSubject(subject);
– Fizer Khan
Sep 9 '14 at 17:08
@FizerKhan - really? docs.oracle.com/javaee/6/api/javax/mail/internet/…
– Nick Grealy
Mar 31 '15 at 3:49
add a comment |
up vote
12
down vote
If you are using Google app engine/Java, then 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);
msg.setSubject(subject,"UTF-8");
does not work. It should bemsg.setSubject(subject);
– Fizer Khan
Sep 9 '14 at 17:08
@FizerKhan - really? docs.oracle.com/javaee/6/api/javax/mail/internet/…
– Nick Grealy
Mar 31 '15 at 3:49
add a comment |
up vote
12
down vote
up vote
12
down vote
If you are using Google app engine/Java, then 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);
If you are using Google app engine/Java, then 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);
edited May 6 '13 at 14:27
Doug Porter
6,75843253
6,75843253
answered Nov 26 '12 at 12:44
Knowledge Serve
51858
51858
msg.setSubject(subject,"UTF-8");
does not work. It should bemsg.setSubject(subject);
– Fizer Khan
Sep 9 '14 at 17:08
@FizerKhan - really? docs.oracle.com/javaee/6/api/javax/mail/internet/…
– Nick Grealy
Mar 31 '15 at 3:49
add a comment |
msg.setSubject(subject,"UTF-8");
does not work. It should bemsg.setSubject(subject);
– Fizer Khan
Sep 9 '14 at 17:08
@FizerKhan - really? docs.oracle.com/javaee/6/api/javax/mail/internet/…
– Nick Grealy
Mar 31 '15 at 3:49
msg.setSubject(subject,"UTF-8");
does not work. It should be msg.setSubject(subject);
– Fizer Khan
Sep 9 '14 at 17:08
msg.setSubject(subject,"UTF-8");
does not work. It should be msg.setSubject(subject);
– Fizer Khan
Sep 9 '14 at 17:08
@FizerKhan - really? docs.oracle.com/javaee/6/api/javax/mail/internet/…
– Nick Grealy
Mar 31 '15 at 3:49
@FizerKhan - really? docs.oracle.com/javaee/6/api/javax/mail/internet/…
– Nick Grealy
Mar 31 '15 at 3:49
add a comment |
up vote
3
down vote
Take a look at http://commons.apache.org/email/ they have an HtmlEmail class that probably does exactly what you need.
commons.apache.org/proper/commons-email/apidocs/org/apache/…
– Anand Rockzz
Apr 30 '17 at 22:31
add a comment |
up vote
3
down vote
Take a look at http://commons.apache.org/email/ they have an HtmlEmail class that probably does exactly what you need.
commons.apache.org/proper/commons-email/apidocs/org/apache/…
– Anand Rockzz
Apr 30 '17 at 22:31
add a comment |
up vote
3
down vote
up vote
3
down vote
Take a look at http://commons.apache.org/email/ they have an HtmlEmail class that probably does exactly what you need.
Take a look at http://commons.apache.org/email/ they have an HtmlEmail class that probably does exactly what you need.
answered Feb 21 '11 at 17:05
CarlosZ
5,46111615
5,46111615
commons.apache.org/proper/commons-email/apidocs/org/apache/…
– Anand Rockzz
Apr 30 '17 at 22:31
add a comment |
commons.apache.org/proper/commons-email/apidocs/org/apache/…
– Anand Rockzz
Apr 30 '17 at 22:31
commons.apache.org/proper/commons-email/apidocs/org/apache/…
– Anand Rockzz
Apr 30 '17 at 22:31
commons.apache.org/proper/commons-email/apidocs/org/apache/…
– Anand Rockzz
Apr 30 '17 at 22:31
add a comment |
up vote
3
down vote
you have to call
msg.saveChanges();
after setting content type.
add a comment |
up vote
3
down vote
you have to call
msg.saveChanges();
after setting content type.
add a comment |
up vote
3
down vote
up vote
3
down vote
you have to call
msg.saveChanges();
after setting content type.
you have to call
msg.saveChanges();
after setting content type.
edited Nov 17 '13 at 18:20
rolfl
15.6k63166
15.6k63166
answered Nov 17 '13 at 17:58
Afodunrinbi Lanre
311
311
add a comment |
add a comment |
up vote
3
down vote
Since JavaMail version 1.4, there is an overload of setText
method that accepts the 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" );
add a comment |
up vote
3
down vote
Since JavaMail version 1.4, there is an overload of setText
method that accepts the 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" );
add a comment |
up vote
3
down vote
up vote
3
down vote
Since JavaMail version 1.4, there is an overload of setText
method that accepts the 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" );
Since JavaMail version 1.4, there is an overload of setText
method that accepts the 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" );
edited Feb 1 at 22:18
Dave Moten
10.1k12836
10.1k12836
answered Mar 30 '17 at 17:38
Alexander Pogrebnyak
37.9k885112
37.9k885112
add a comment |
add a comment |
up vote
0
down vote
You can find a complete and very simple java class for sending emails using Google(gmail) account here,
Send email message using java application
It uses 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");
2
This doesn't at all address the question. The OP was asking to send specifically HTML emails.
– Bassinator
Nov 10 '17 at 2:33
add a comment |
up vote
0
down vote
You can find a complete and very simple java class for sending emails using Google(gmail) account here,
Send email message using java application
It uses 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");
2
This doesn't at all address the question. The OP was asking to send specifically HTML emails.
– Bassinator
Nov 10 '17 at 2:33
add a comment |
up vote
0
down vote
up vote
0
down vote
You can find a complete and very simple java class for sending emails using Google(gmail) account here,
Send email message using java application
It uses 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");
You can find a complete and very simple java class for sending emails using Google(gmail) account here,
Send email message using java application
It uses 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");
answered Sep 16 '13 at 10:00
Lasa
52
52
2
This doesn't at all address the question. The OP was asking to send specifically HTML emails.
– Bassinator
Nov 10 '17 at 2:33
add a comment |
2
This doesn't at all address the question. The OP was asking to send specifically HTML emails.
– Bassinator
Nov 10 '17 at 2:33
2
2
This doesn't at all address the question. The OP was asking to send specifically HTML emails.
– Bassinator
Nov 10 '17 at 2:33
This doesn't at all address the question. The OP was asking to send specifically HTML emails.
– Bassinator
Nov 10 '17 at 2:33
add a comment |
up vote
0
down vote
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 = 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";
}
}
add a comment |
up vote
0
down vote
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 = 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";
}
}
add a comment |
up vote
0
down vote
up vote
0
down vote
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 = 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";
}
}
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 = 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";
}
}
answered Jul 9 '14 at 11:33
Pradap Adwani A
12
12
add a comment |
add a comment |
up vote
0
down vote
I found this way not sure it works for all the CSS primitives
By setting the header property "Content-Type" to "text/html"
mimeMessage.setHeader("Content-Type", "text/html");
now I can do stuff like
mimeMessage.setHeader("Content-Type", "text/html");
mimeMessage.setText ("`<html><body><h1 style ="color:blue;">My first Header<h1></body></html>`")
Regards
add a comment |
up vote
0
down vote
I found this way not sure it works for all the CSS primitives
By setting the header property "Content-Type" to "text/html"
mimeMessage.setHeader("Content-Type", "text/html");
now I can do stuff like
mimeMessage.setHeader("Content-Type", "text/html");
mimeMessage.setText ("`<html><body><h1 style ="color:blue;">My first Header<h1></body></html>`")
Regards
add a comment |
up vote
0
down vote
up vote
0
down vote
I found this way not sure it works for all the CSS primitives
By setting the header property "Content-Type" to "text/html"
mimeMessage.setHeader("Content-Type", "text/html");
now I can do stuff like
mimeMessage.setHeader("Content-Type", "text/html");
mimeMessage.setText ("`<html><body><h1 style ="color:blue;">My first Header<h1></body></html>`")
Regards
I found this way not sure it works for all the CSS primitives
By setting the header property "Content-Type" to "text/html"
mimeMessage.setHeader("Content-Type", "text/html");
now I can do stuff like
mimeMessage.setHeader("Content-Type", "text/html");
mimeMessage.setText ("`<html><body><h1 style ="color:blue;">My first Header<h1></body></html>`")
Regards
edited Sep 4 '17 at 20:53
luk2302
32.5k176791
32.5k176791
answered Sep 4 '17 at 20:35
Balint
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5068827%2fhow-do-i-send-an-html-email%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown