Last active
May 21, 2018 16:02
-
-
Save authmane512/3a1e89a50f048e74c7286640970786c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.javamailapp; | |
import android.app.Activity; | |
import android.util.Log; | |
import android.view.View; | |
import android.os.Bundle; | |
import android.widget.*; | |
import javax.mail.MessagingException; | |
import javax.mail.Session; | |
import javax.mail.Store; | |
import java.util.Properties; | |
import javax.activation.CommandMap; | |
import javax.activation.MailcapCommandMap; | |
public class MainActivity extends Activity { | |
RelativeLayout layout; | |
public static void connect(String host, String user, String passwd) { | |
Thread connector = new Thread(new Runnable() { | |
public void run() { | |
try { | |
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap(); | |
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"); | |
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"); | |
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); | |
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); | |
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"); | |
CommandMap.setDefaultCommandMap(mc); | |
Properties properties = new Properties(); | |
properties.put("mail.store.protocol", "imaps"); | |
// properties.put("mail.pop3.port", "995"); | |
// properties.put("mail.pop3.starttls.enable", "true"); | |
Session emailSession = Session.getDefaultInstance(properties); | |
Store store = emailSession.getStore("imaps"); | |
store.connect(host, user, passwd); | |
Log.w("JavaMailApp", "Connection successful!"); | |
} catch (MessagingException e) { | |
Log.e("JavaMailApp", "error", e); | |
} | |
} | |
}); | |
connector.start(); | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
layout = (RelativeLayout)RelativeLayout.inflate(this, R.layout.activity_main, null); | |
setContentView(layout); | |
String host = "imap.gmail.com"; | |
String user = "<your-name>@gmail.com"; | |
String passwd = "<your-password>"; | |
connect(host, user, passwd); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment