Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6573737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:10:18+00:00 2026-05-25T15:10:18+00:00

I need to send email using JAVA Mail with inline images not as an

  • 0

I need to send email using JAVA Mail with inline images not as an attachment. I used the third party JAR files and modified some code but did not get any images in my email. Here is my code.

GMailSender.java

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class GMailSender extends javax.mail.Authenticator 
{   
private String mailhost = "smtp.gmail.com";  
private String user;   
private String password;   
private Session session;
private Transport transport;
private static final int SMTP_HOST_PORT = 465;

static 
{   
    Security.addProvider(new com.power.calculator.gal.JSSEProvider());   
}  

public GMailSender(String user, String password) throws Exception 
{   
    this.user = user;   
    this.password = password;   

    Properties props = new Properties();   
    props.setProperty("mail.transport.protocol", "smtp");   
    props.setProperty("mail.host", mailhost);   
    props.put("mail.smtp.auth", "true");   
    props.put("mail.smtp.port", "465");   
    props.put("mail.smtp.socketFactory.port", "465");   
    props.put("mail.smtp.socketFactory.class",   
            "javax.net.ssl.SSLSocketFactory");   
    props.put("mail.smtp.socketFactory.fallback", "false");   
    props.setProperty("mail.smtp.quitwait", "false");   

    session = Session.getDefaultInstance(props, this); 
    session.setDebug(true);
    transport = session.getTransport();
}   

protected PasswordAuthentication getPasswordAuthentication()
{   
    return new PasswordAuthentication(user, password);   
}   

public synchronized void sendMail(String subject, String body, String sender, String recipients, String imagePath) throws Exception 
{   
    try
    {
        MimeMessage message = new MimeMessage(session);
        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/html")); 
        message.setSubject(subject);
        message.addRecipient(Message.RecipientType.TO,
                 new InternetAddress(sender));
        message.setDataHandler(handler);
        message.setSender(new InternetAddress(sender));

        Multipart multipart = new MimeMultipart();
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(new File(imagePath));
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName("1.png");
        messageBodyPart.setDisposition(MimeBodyPart.INLINE);
        messageBodyPart.setHeader("Content-ID","<vogue>");
        multipart.addBodyPart(messageBodyPart);

        messageBodyPart = new MimeBodyPart();
        String htmlText = body;
        messageBodyPart.setContent(htmlText, "text/html");
        multipart.addBodyPart(messageBodyPart);
        message.setContent(multipart);

        transport.connect(mailhost, SMTP_HOST_PORT, user, password);

        if (recipients.indexOf(',') > 0)   
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   
        else  
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients)); 
        transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
        transport.close();
    }
    catch(Exception e){}
}   

public class ByteArrayDataSource implements DataSource 
{   
    private byte[] data;   
    private String type;   

    public ByteArrayDataSource(byte[] data, String type) 
    {   
        super();   
        this.data = data;   
        this.type = type;   
    }   

    public ByteArrayDataSource(byte[] data) 
    {   
        super();   
        this.data = data;   
    }   

    public void setType(String type) 
    {   
        this.type = type;   
    }   

    public String getContentType() 
    {   
        if (type == null)   
            return "application/octet-stream";   
        else  
            return type;   
    }   

    public InputStream getInputStream() throws IOException 
    {   
        return new ByteArrayInputStream(data);   
    }   

    public String getName() 
    {   
        return "ByteArrayDataSource";   
    }   

    public OutputStream getOutputStream() throws IOException 
    {   
        throw new IOException("Not Supported");   
    }   
}
}    

JSSEProvider.java

import java.security.AccessController;
import java.security.Provider;

@SuppressWarnings("serial")
public final class JSSEProvider extends Provider 
{
public JSSEProvider() 
{
    super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
    AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() 
    {
        public Void run() 
        {
            put("SSLContext.TLS",
                    "org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
            put("Alg.Alias.SSLContext.TLSv1", "TLS");
            put("KeyManagerFactory.X509",
                    "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
            put("TrustManagerFactory.X509",
                    "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
            return null;
        }
    });
}
}

My Activity Class:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class PowerCalculatorActivity extends Activity implements OnClickListener, Runnable
{
protected static final String TAG = "TAG";
private static final String ERROR = "Error";
private static final String OK = "OK";
private static final int ZERO = 0;
private static final int ONE = 1;
private static final int TWO = 2;
private static final int THREE = 3;
private static final String MESSAGEONE = "  Please enter valid email address  ";
private static final String MESSAGETWO = "Please enter valid name and email address";
private static String mErrorMessages;
private EditText mName, mEmail;
private Button mSubmit;
private Dialog showErrorDialog, showProgressDialog;

private Handler mHandler = new Handler() 
{
    public void handleMessage(Message nMessage) 
    {
        switch (nMessage.what) 
        {
            case ZERO:
                showProgressDialog.dismiss();
                Intent mHelpIntent = new Intent(PowerCalculatorActivity.this, ByOptions.class);
                mHelpIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                startActivity(mHelpIntent);
                PowerCalculatorActivity.this.finish();
                break;
            case ONE:
                showProgressDialog.dismiss();
                showErrorDialog(ERROR, MESSAGEONE, OK);
                break;
            case TWO:
                showProgressDialog.dismiss();
                showErrorDialog(ERROR, MESSAGETWO, OK);
                break;
            case THREE:
                showProgressDialog.dismiss();
                showErrorDialog(ERROR, mErrorMessages, OK);
                break;
        }
    }
};

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.login);

    //Initialize view
    mName = (EditText)findViewById(R.id.NameText);
    mEmail = (EditText)findViewById(R.id.EmailText);

    mSubmit = (Button)findViewById(R.id.Submit);
    mSubmit.setOnClickListener(this);
}

@Override
public void onClick(View nView) 
{
    switch(nView.getId())
    {
        case R.id.Submit:
            showCustomProgressDialog();
            Thread mLoadingThread = new Thread(this);
            mLoadingThread.start();
            break;
    }
}

@Override
public void run() 
{
    if(mName.getText().toString().trim().length() > 0 && mEmail.getText().toString().trim().length() > 0)
    {
        String mSenderEmail = mEmail.getText().toString().trim();
        boolean mValidEMail = isEmailValid(mSenderEmail);
        if(mValidEMail)
        {
            String mEmailSubject = getResources().getString(R.string.emailsubject);
            String mRecipients = getResources().getString(R.string.clientemail);
            String mMyEmail = getResources().getString(R.string.emailaddress);
            String mMessage = "Name: " + mName.getText().toString().trim() + "<br>" + "Email: " + mEmail.getText().toString().trim()
                + "<br><br>" + "<img src=\"cid:vogue\">";
            String mImagePath = "/sdcard/1.png";
            String mPassword = getResources().getString(R.string.emailpassword);
            try 
            {   

                GMailSender mSender = new GMailSender(mMyEmail, mPassword);
                mSender.sendMail(mEmailSubject,   
                        mMessage,   
                        mSenderEmail,
                        mRecipients, mImagePath);   
            } 
            catch(Exception e) 
            {   
                mErrorMessages = e.getMessage();
                Log.e(TAG, e.getMessage(), e);
                Message mMessageSend = new Message();
                mMessageSend.what = THREE;
                mHandler.sendMessage(mMessageSend);
            } 
            Message mMessageSend = new  Message();
            mMessageSend.what = ZERO;//Success
            mHandler.sendMessage(mMessageSend);
        }
        else
        {
            Message mMessageSend = new  Message();
            mMessageSend.what = ONE;//Fail
            mHandler.sendMessage(mMessageSend);
        }
    }
    else
    {
        Message mMessageSend = new  Message();
        mMessageSend.what = TWO;//Fail
        mHandler.sendMessage(mMessageSend);
    }
}

/**
 * This method send data to the client using JAVA Mail
 */
private void showCustomProgressDialog()
{
    showProgressDialog = new Dialog(PowerCalculatorActivity.this, R.style.ProgressDialog);
    showProgressDialog.setContentView(R.layout.customloadingprogress);
    showProgressDialog.getWindow().setGravity(Gravity.CENTER);
    showProgressDialog.setCancelable(true);
    showProgressDialog.show();
}

/**
 * This method is used for checking valid email id format.
 * @param email
 * @return boolean true for valid and false for invalid
 */
public static boolean isEmailValid(String nComingEmail) 
{
    boolean isValid = false;

    String mExpression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
    CharSequence mInputEmail = nComingEmail;

    Pattern mPattern = Pattern.compile(mExpression, Pattern.CASE_INSENSITIVE);
    Matcher mMatcher = mPattern.matcher(mInputEmail);
    if (mMatcher.matches()) 
    {
        isValid = true;
    }
    return isValid;
}

/**
 * This method shows error message when entered information is wrong
 * @param nTitle
 * @param nMessage
 * @param nPosButton
 */
private void showErrorDialog(String nTitle, String nMessage, String nPosButton) 
{
    showErrorDialog = new Dialog(this);
    CustomDialog.Builder customBuilder = new CustomDialog.Builder(this);
    customBuilder.setTitle(nTitle);
    customBuilder.setMessage(nMessage);
    customBuilder.setPositiveButton(nPosButton, new DialogInterface.OnClickListener() 
    {
        public void onClick(DialogInterface dialog, int which) 
        {
            showErrorDialog.dismiss();
        }
    });
    showErrorDialog = customBuilder.create();
    showErrorDialog.setCancelable(true);
    showErrorDialog.show();
}

/**
 * This method create option menu
 * @param menu
 * @return true if menu created successfully
 */
@Override
public boolean onCreateOptionsMenu(Menu nMenu) 
{
    MenuInflater mInflater = getMenuInflater();
    mInflater.inflate(R.menu.menubar, nMenu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    //Handle item selection
    switch (item.getItemId())
    {
        case R.id.help:
            Intent mHelpIntent = new Intent(PowerCalculatorActivity.this, HelpScreen.class);
            mHelpIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
            startActivity(mHelpIntent);
            return true;
        case R.id.contactus:
            Intent mContactUsIntent = new Intent(PowerCalculatorActivity.this, ContactUs.class);
            mContactUsIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
            startActivity(mContactUsIntent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

/**
 * This method catches the back key event and finish the current activity
 * @param keyevent
 * @return boolean true for valid catch
 */
@Override
public boolean onKeyDown(int mKeyCode, KeyEvent mKeyEvent) 
{
    if ((!(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.DONUT)
            && mKeyCode == KeyEvent.KEYCODE_BACK && mKeyEvent.getRepeatCount() == 0)) 
    {
        onBackPressed();
    }
    return super.onKeyDown(mKeyCode, mKeyEvent);
}

public void onBackPressed() 
{
    finish();
}
}

External JARS are available here: Sending Email in Android using JavaMail API without using the default/built-in app

Complete Solution: I have edited all my files.

Thanks,
AndroidVogue

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:10:19+00:00Added an answer on May 25, 2026 at 3:10 pm

    You will need another part that contains the HTML referring to your image, check out this small example:

    public class GMail {
    
        private static final String SMTP_HOST_NAME = "smtp.gmail.com";
        private static final int SMTP_HOST_PORT = 465;
        private static final String SMTP_AUTH_USER = "...@gmail.com";
        private static final String SMTP_AUTH_PWD  = "pwd";
    
        public static void main(String[] args) throws Exception{
           new GMail().send();
        }
    
        public void send() throws Exception{
            // prepare session & transport object
            Properties props = new Properties();
            props.put("mail.transport.protocol", "smtps");
            props.put("mail.smtps.host", SMTP_HOST_NAME);
            props.put("mail.smtps.auth", "true");
            Session mailSession = Session.getDefaultInstance(props);
            mailSession.setDebug(true);
            Transport transport = mailSession.getTransport();
    
            // prepare messge
            MimeMessage message = new MimeMessage(mailSession);
            message.setSubject("Testing embedded image");
            message.addRecipient(Message.RecipientType.TO,
                     new InternetAddress("...@googlemail.com"));
    
            // create multipart
            Multipart multipart = new MimeMultipart();
    
            // create bodypart with image and set content-id
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(new File("/Users/Tim/Desktop/image.png"));
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName("image.png");
            messageBodyPart.setDisposition(MimeBodyPart.INLINE);
            messageBodyPart.setHeader("Content-ID","<vogue>");
            multipart.addBodyPart(messageBodyPart);
    
            // create bodypart with html content and reference to the content-id
            messageBodyPart = new MimeBodyPart();
            String htmlText = "<img src=\"cid:vogue\">";
            messageBodyPart.setContent(htmlText, "text/html");
            multipart.addBodyPart(messageBodyPart);
    
            // add multipart to message
            message.setContent(multipart);
    
            // connect & send message
            transport.connect
              (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
            transport.sendMessage(message,
                message.getRecipients(Message.RecipientType.TO));
            transport.close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to send email using mail() in php. I need the message
I need to send html email. So I am using PEAR library with Mail
i have a situation where i need to send an email with attachment using
Since I need to send email by code, I'm currently using plan text but
When you send an email using System.Net.Mail you can set the SMTP MAIL FROM
I'm using asp.net with vb.net.. I need to send an email to my website
In my app I need to send the email to the mail id provided
I know how to send email using PHPmail. But what if I need to
We need to send email which contains Pound (currency) symbols in ColdFusion. Before sending
I need to be able to periodically send email alerts to subscribed users. PHP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.