Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

How to Display an Email with an image on Different Email Account Using javax.mail

Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi There again ,
we have a problem in displaying an image on other Email Account
when we send an email with an image from our own Email Application ,
Email Body opens successfully but image is not being displayed.

The class of Email which we have written for sending Email is written below;

//---------------start of class-------

package HandlerBeans.Email;

import javax.mail.*;
import javax.mail.Session;
import javax.mail.internet.*;
import java.util.*;
import javax.activation.*;

public class EmailHandler
public EmailHandler() { }//constructor

// public static boolean sendEmail(String from,String to,String subject,String body,String FilePath)

public boolean sendEmail(String from,String to,String subject,String body,String FilePath)
boolean debug= false;
String host = "";
String filename = FilePath;
String msgText1 = body ;
// create some properties and get the default Session
Properties props = System.getProperties();
props.put("", host);
Session session1 = Session.getDefaultInstance(props, null);
try {
// create a message
MimeMessage msg = new MimeMessage(session1);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = InternetAddress.parse(to);
msg.setRecipients(Message.RecipientType.TO, address);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
String fname = filename;
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource(fname);
mbp2.setDataHandler(new DataHandler(fds));
// add the Multipart to the message

// set the Date: header
msg.setSentDate(new java.util.Date());

// send the message

catch (MessagingException mex) {

Exception ex = null;
if ((ex = mex.getNextException()) != null)
return false;

return true;


// ---------------------- end of class -------------------

JSP code is written below
where we are calling the method
sendEmail(String from,String to,String subject,String body,String FilePath) for sending Email

//--------------- JSP Code start--------------------

<%@ page language="java" import="com.jspsmart.upload.*"%>
<%@ page import="java.sql.*,java.util.*,*"%>

<jsp:useBean id="emailhandlerBean" scope="page" class="HandlerBeans.Email.EmailHandler"/>


String from="";
String to="";
String subject="Qurey About Email Image";

String body= "'<html><head></head><body><table>
<tr><td><strong> From LoneStarASp.Inc </strong ></tr></td></table>
<img src= > </body></html>'" ;

String FilePath=null; //it means no file is attached

// email is going to send here

emailhandlerBean.sendEmail( from , to , subject , body , FilePath) ;


//-------------------- jsp code end --------------

Can someone pleaseeeee tell us what are we doing wrong here, we would be very gratefull. Thanks a million.

Posts: 67747
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please do not repeat the same question. Continue discussion here.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic