在Android中使用JAVA Mail嵌入内嵌图像?

时间:2011-09-13 07:38:32

标签: android mime-types javamail multipart

我需要使用JAVA Mail发送电子邮件,内联图片不是附件。我使用了第三方JAR文件并修改了一些代码,但没有在我的电子邮件中获得任何图像。这是我的代码。

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;
        }
    });
}
}

我的活动类:

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();
}
}

外部JARS可在此处获取:Sending Email in Android using JavaMail API without using the default/built-in app

完整解决方案:我已编辑了所有文件。

谢谢, AndroidVogue

1 个答案:

答案 0 :(得分:3)

您需要另一个包含引用您图片的HTML的部分,请查看以下小例子:

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();
    }
}
相关问题