JavaMailã§ã®ã¡ã¼ã«éä¿¡ã¾ã¨ããã®4
JavaMailã§ã®ã¡ã¼ã«éä¿¡ã¾ã¨ããã®4ã
Androidã¢ããªããJavamailã§ã¡ã¼ã«éä¿¡ãè¡ããµã³ãã«ã
Android端æ«å°ç¨ã®jarãã¡ã¤ã«(mail.jarãadditionnal.jarãactivation.jar)ãå¿
è¦ã§ãããMimeMessageã®ä½æãéä¿¡æé ã¯åãã§ãã
ã¾ããOutbound Port 25 Blockingã«ããã25çªãã¼ãã§ã¡ã¼ã«éä¿¡ãä¸å¯è½ãªãã¨ããããããgmailã®ã¢ã«ã¦ã³ãã使ç¨ãã¦éä¿¡ãã¦ãã¾ãã
Androidç¨JavaMailã®ãã¦ã³ãã¼ã
androidç¨ã®JavaMailãæä¾ãã¦ãããµã¤ããjavamail-androidããããã¦ã³ãã¼ããã¾ãã
以ä¸ã®jarãã¡ã¤ã«ããã¦ã³ãã¼ããã¦ãã ããã
- javamail-android
http://code.google.com/p/javamail-android/
-
- mail.jar
- additionnal.jar
- activation.jar
libsã¨ãã£ããã£ã¬ã¯ããªãä½æããä¸è¨ã®jarãã¡ã¤ã«ãã³ãã¼ãã¦ãã ããã
ãã®å¾ããã«ããã¹ã«è¿½å ããã°æºåå®äºã§ãã
(eclipseã®å ´åã¯ãjarãã¡ã¤ã«ãå³ã¯ãªãã¯ããå¾ããã«ããã¹ã«è¿½å ãé¸æããã°OKã§ãã)
ãµã³ãã«
gmailã§ã¡ã¼ã«éä¿¡
ãããã¯ã¼ã¯ã¢ã¯ã»ã¹ãçºçããããããã¼ããã·ã§ã³ã®è¨å®ãå¿ è¦ã«ãªãã¾ãã
- AndroidManifest.xmlã«ä»¥ä¸ã®ãã¼ããã·ã§ã³ã追å
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
- gmailçµç±ã§ã®éä¿¡ãµã³ãã«ã³ã¼ã
Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); // SMTPãµã¼ãå props.put("mail.host", "smtp.gmail.com"); // æ¥ç¶ãããã¹ãå props.put("mail.smtp.port", "587"); // SMTPãµã¼ããã¼ã props.put("mail.smtp.auth", "true"); // smtp auth props.put("mail.smtp.starttls.enable", "true"); // STTLS // ã»ãã·ã§ã³ Session session = Session.getDefaultInstance(props); session.setDebug(true); MimeMessage msg = new MimeMessage(session); try { msg.setSubject("gmailã§ã¡ã¼ã«éä¿¡ãã¹ã(utf-8)", "utf-8"); msg.setFrom(new InternetAddress("Fromã¢ãã¬ã¹")); msg.setSender(new InternetAddress("Senderã¢ãã¬ã¹")); msg.setRecipient(Message.RecipientType.TO, new InternetAddress("éä¿¡å ã¢ãã¬ã¹")); msg.setText("gmailçµç±ã§gmail.comåãã¡ã¼ã«éä¿¡ãã¹ã", "utf-8"); Transport t = session.getTransport("smtp"); t.connect("gmailã¢ã«ã¦ã³ã [email protected]", "gmailãã¹ã¯ã¼ã"); t.sendMessage(msg, msg.getAllRecipients()); } catch (MessagingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
æºå¸¯ã¡ã¼ã«ããã³ã¡ãéä¿¡
iPhone(i.softbank.jp)åãã«éä¿¡ãã¦ã¿ã¾ãã
package com.ttshrk.send_mail_from_app; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.NoSuchProviderException; 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; import javax.mail.internet.MimeUtility; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import com.ttshrk.mbemoji.Emoji; import com.ttshrk.mbemoji.EmojiUtility; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.UnknownServiceException; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; public class SendMailFromApp extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { buttonClick1(); } }); Button button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { buttonClick2(); } }); } /** * utf-8 softbank.ne.jp */ private void buttonClick1() { Session session = createSendGMailSession(); MimeMessage msg = new MimeMessage(session); try { msg.setSubject("gmailã§æºå¸¯åãã¡ã¼ã«éä¿¡ãã¹ã(utf-8)", "utf-8"); msg.setFrom(new InternetAddress("[email protected]")); msg.setSender(new InternetAddress("[email protected]")); msg.setRecipient(Message.RecipientType.TO, new InternetAddress( "[email protected]")); msg.setDataHandler(new DataHandler(new TextBinaryDataSource( "gmailçµç±ã§i.softbank.jpåãã¡ã¼ã«éä¿¡ãã¹ã[:1025:][:0105:]" .getBytes(), "utf-8", "plain", Emoji.Mode.SOFTBANK))); sendGmail(msg, session); } catch (MessagingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * utf-8 i.softbank.jp ã§ãã * multipart/related * â multipart/alternative * â â text/plain * â â text/html * â imageãï¼ã¤ã³ã©ã¤ã³ç»å/æ·»ä»ç»åï¼ */ private void buttonClick2() { Session session = createSendGMailSession(); MimeMessage msg = new MimeMessage(session); try { msg.setSubject("gmailã§ã¡ã¼ã«éä¿¡ãã¹ã(utf-8) ã§ãã", "utf-8"); msg.setFrom(new InternetAddress("[email protected]")); msg.setSender(new InternetAddress("[email protected]")); msg.setRecipient(Message.RecipientType.TO, new InternetAddress( "[email protected]")); // related Multipart relatedPart = new MimeMultipart("related"); // alternative MimeBodyPart alternativeBodyPart = new MimeBodyPart(); Multipart alternativePart = new MimeMultipart("alternative"); alternativeBodyPart.setContent(alternativePart); relatedPart.addBodyPart(alternativeBodyPart); // text mail MimeBodyPart textBodyPart = new MimeBodyPart(); textBodyPart.setDataHandler(new DataHandler(new TextBinaryDataSource( "gmailçµç±ã§i.softbank.jpåãã¡ã¼ã«éä¿¡ãã¹ã[:1025:][:0105:]".getBytes(), "utf-8", "plain", Emoji.Mode.SOFTBANK))); textBodyPart.setHeader("Content-Transfer-Encoding", "base64"); alternativePart.addBodyPart(textBodyPart); // html mail MimeBodyPart htmlBodyPart = new MimeBodyPart(); htmlBodyPart.setDataHandler(new DataHandler(new TextBinaryDataSource( "<HTML><BODY>gmailçµç±ã§i.softbank.jpåãã¡ã¼ã«éä¿¡ãã¹ã[:1025:][:0105:]<IMG src=\"cid:12345@12345\"></BODY></HTML>".getBytes(), "utf-8", "html", Emoji.Mode.SOFTBANK))); htmlBodyPart.setHeader("Content-Transfer-Encoding", "base64"); alternativePart.addBodyPart(htmlBodyPart); // inline image MimeBodyPart imageBodyPart = new MimeBodyPart(); imageBodyPart.setDataHandler(new DataHandler(new FileDataSource("/sdcard/sample.gif"))); imageBodyPart.setFileName(MimeUtility.encodeWord("sample.gif")); imageBodyPart.setDisposition("inline"); // inlineæå®ãã¦ãã imageBodyPart.setContentID("12345@12345"); // ã¤ã³ã©ã¤ã³ç»åãæå® relatedPart.addBodyPart(imageBodyPart); // set related msg.setContent(relatedPart); sendGmail(msg, session); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * * @return */ private Session createSendGMailSession() { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); // SMTPãµã¼ãå props.put("mail.host", "smtp.gmail.com"); // æ¥ç¶ãããã¹ãå props.put("mail.smtp.port", "587"); // SMTPãµã¼ããã¼ã props.put("mail.smtp.auth", "true"); // smtp auth props.put("mail.smtp.starttls.enable", "true"); // STTLS // ã»ãã·ã§ã³ Session session = Session.getDefaultInstance(props); session.setDebug(true); return session; } /** * * @param msg * @param session * @throws MessagingException */ private void sendGmail(MimeMessage msg, Session session) throws MessagingException { Transport t = session.getTransport("smtp"); t.connect("gmailã¦ã¼ã¶ã¢ã«ã¦ã³ã", "gmailãã¹ã¯ã¼ã"); t.sendMessage(msg, msg.getAllRecipients()); } } /** * ç¹å®ã®ã³ã¼ãããã¤ããªçµµæåã«å¤æããDataSource */ class TextBinaryDataSource implements DataSource { private byte[] text; private byte[] convertedText; private String charset; private String subType; private Emoji.Mode emojiMode; public TextBinaryDataSource(byte[] text, String charset, String subType, Emoji.Mode emojiMode) throws IOException { this.text = text; this.charset = charset; this.subType = subType; this.emojiMode = emojiMode; initialize(); } public TextBinaryDataSource(String text, String charset, String subType, Emoji.Mode emojiMode) throws IOException { this(text.getBytes(), charset, subType, emojiMode); } public byte[] getPlainText() { return text; } private void initialize() throws IOException { InputStream dec_in = new ByteArrayInputStream(text); ByteArrayOutputStream bo = null; OutputStream os = null; try { bo = new ByteArrayOutputStream(); os = EmojiUtility.decode(bo, charset, emojiMode); byte[] buf = new byte[2048]; int len; while ((len = dec_in.read(buf)) != -1) { os.write(buf, 0, len); } } finally { if (os != null) { os.close(); } } convertedText = bo.toByteArray(); } @Override public InputStream getInputStream() throws IOException { InputStream is = new ByteArrayInputStream(convertedText); return is; } @Override public OutputStream getOutputStream() throws IOException { throw new UnknownServiceException(); } @Override public String getContentType() { return "text/" + subType + "; charset=" + charset; } @Override public String getName() { return ""; } }
ãã³ã¡ã®é¨åã®æ·»ä»ãã¡ã¤ã«ã¯SDã«ã¼ãããåå¾ããããã«ãã¾ããã
ï¼assetsããèªã¿è¾¼ãå ´åããnew InputStreamReader(context.getAssets().open("sample.gif"))ãã®ããã«ãã¦åå¾ãããããInputStreamãè¨å®ã§ããDataSourceãå¿
è¦ãããã®ã§ã»ã»ã»ï¼
- ãµã³ãã«ã³ã¼ã
ã¾ããshift-jisãutf-8ã®ã³ã¼ãã§ãã½ãããã³ã¯æºå¸¯ã¢ãã¬ã¹(XXXX@softbank.ne.jp)ãiPhone(i.softbank.jp)åãã«éä¿¡ãã¦ã¿ãã¨ãããã©ã¡ããéä¿¡ã¯ã§ãã¾ããã以ä¸ã®ãããªéããããã¾ããããã ãã¡ããã¨æ¤è¨¼ããããã§ã¯ãªãã®ã§ãç®å®ç¨åº¦ã«ãã¦ãã ããã
(ã©ã¡ãã®ã¡ã¼ã«ããiPhoneå®ã¦ã«éä¿¡ãã¾ããã)
- softbank.ne.jpå®ã¦ã®ã¡ã¼ã«ã®å ´åãshift-jisãutf-8ã¨ãã«çµµæåãåé¡ãªãæ±ãã¾ãããã¡ã¼ã«ã¯ã©ã¤ã¢ã³ãã«ããã®ããããã¾ãããã»ã»ã»ã
- i.softbank.jpå®ã¦ã®ã¡ã¼ã«ã®å ´åãutf-8ã§ãªãã¨(ãã¤ããªã³ã¼ãã®)çµµæåãæ£ããèªèãã¾ããã§ããã