2011-09-13 9 views
0

添付ファイルではなく、インラインイメージを使用したJAVA Mailを使用して電子メールを送信する必要があります。私はサードパーティ製のJARファイルを使用していくつかのコードを修正しましたが、私の電子メールにはイメージはありませんでした。ここに私のコードです。アンドロイドのJAVA Mailを使ってインライン画像を埋め込む?

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活動クラス:

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\\.-][email protected]([\\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(); 
} 
} 

外部JARがここに用意されていますSending Email in Android using JavaMail API without using the default/built-in app

解決策:すべてのファイルを編集しました。

おかげで、 AndroidVogue

答えて

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 = "[email protected]"; 
    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("[email protected]")); 

     // 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

を電子メールの一部。なにか提案を?ありがとう。 –

+0

ファイルを更新しました。見てください。 'GmailSender'と' PowerCalculatorActivity'クラスが編集されています。 'GmailSender'クラスであなたのコードを追加しました。' PowerCalculatorActivity'クラスで 'HTML'タグを付けました。ありがとう –

+0

イメージパスは次のようになります: 'cid:content-id'? – timbooo

関連する問題