├── .gitignore
├── .idea
├── .name
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── encodings.xml
├── gradle.xml
├── inspectionProfiles
│ ├── Project_Default.xml
│ └── profiles_settings.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── yuyh
│ │ └── simplemail
│ │ ├── LoginActivity.java
│ │ ├── MailApp.java
│ │ └── MainActivity.java
│ └── res
│ ├── layout
│ ├── activity_login.xml
│ └── activity_main.xml
│ ├── mipmap-hdpi
│ └── ic_launcher.png
│ ├── mipmap-mdpi
│ └── ic_launcher.png
│ ├── mipmap-xhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxxhdpi
│ └── ic_launcher.png
│ ├── values-w820dp
│ └── dimens.xml
│ └── values
│ ├── colors.xml
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── library
├── .gitignore
├── build.gradle
├── libs
│ ├── activation.jar
│ ├── additionnal.jar
│ └── mail.jar
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── yuyh
│ │ └── library
│ │ └── simplemail
│ │ ├── MailHelper.java
│ │ ├── bean
│ │ ├── Attachment.java
│ │ ├── Email.java
│ │ ├── LoginInfo.java
│ │ ├── MailDraft.java
│ │ ├── ReceiverMailInfo.java
│ │ ├── SendMailInfo.java
│ │ └── User.java
│ │ ├── constant
│ │ └── Constants.java
│ │ └── utils
│ │ ├── MailAuthenticator.java
│ │ └── StringUtils.java
│ └── res
│ └── values
│ └── strings.xml
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | SimpleMail
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
24 |
25 |
--------------------------------------------------------------------------------
/.idea/inspectionProfiles/Project_Default.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/inspectionProfiles/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # SimpleMail
2 | ### 一个基于POP3和SMTP协议的邮件客户端。
3 |
4 |
5 | ## 登录
6 | ```java
7 | String address = "smuyyh@126.com";
8 | String pwd = "xxxxxxxxxx";
9 | String host = "smtp." + address.substring(address.lastIndexOf("@") + 1);
10 |
11 | final LoginInfo info = new LoginInfo();
12 | info.mailServerHost = host;
13 | info.mailServerPort = "25";
14 | info.userName = address;
15 | info.password = pwd;
16 | info.validate = true;
17 | new Thread(new Runnable() {
18 | @Override
19 | public void run() {
20 | Session session = MailHelper.getInstance(MainActivity.this).login(info);
21 | if(session != null){
22 | // 登录成功
23 | }
24 | }
25 | }).start();
26 | ```
27 |
28 | ## 收件箱
29 | ```java
30 | try {
31 | List list = MailHelper.getInstance(MainActivity.this).getAllMail(Constants.MailFolder.INBOX, info, session);
32 | Log.i("TAG", list.size()+"");
33 | } catch (MessagingException e) {
34 | e.printStackTrace();
35 | }
36 |
37 | ```
38 |
39 | ## 发送邮件
40 | ```java
41 | SendMailInfo info = new SendMailInfo();
42 | info.fromAddress = address;
43 | info.subject = "测试邮件";
44 | info.content = "测试邮件 内容";
45 | info.receivers = new String[]{"352091626@qq.com"};
46 | MailHelper.getInstance(MainActivity.this).sendMail(info, session);
47 | ```
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.3"
6 |
7 | defaultConfig {
8 | applicationId "com.yuyh.simplemail"
9 | minSdkVersion 14
10 | targetSdkVersion 23
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(include: ['*.jar'], dir: 'libs')
24 | compile project(':library')
25 | compile 'com.android.support:appcompat-v7:23.4.0'
26 | compile 'com.android.support:design:23.4.0'
27 | }
28 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\AndroidDev\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yuyh/simplemail/LoginActivity.java:
--------------------------------------------------------------------------------
1 | package com.yuyh.simplemail;
2 |
3 | import android.content.Intent;
4 | import android.os.AsyncTask;
5 | import android.os.Bundle;
6 | import android.support.annotation.Nullable;
7 | import android.support.v7.app.AppCompatActivity;
8 | import android.view.View;
9 | import android.widget.Button;
10 | import android.widget.EditText;
11 | import android.widget.ProgressBar;
12 | import android.widget.Toast;
13 |
14 | import com.yuyh.library.simplemail.MailHelper;
15 | import com.yuyh.library.simplemail.bean.LoginInfo;
16 |
17 | import javax.mail.Session;
18 |
19 | /**
20 | * A login screen that offers login via email/password.
21 | */
22 | public class LoginActivity extends AppCompatActivity {
23 |
24 | private Button btnLogin;
25 |
26 | private EditText etMail;
27 | private EditText etPwd;
28 |
29 | private ProgressBar progressBar;
30 |
31 | @Override
32 | protected void onCreate(@Nullable Bundle savedInstanceState) {
33 | super.onCreate(savedInstanceState);
34 | setContentView(R.layout.activity_login);
35 |
36 | progressBar = (ProgressBar) findViewById(R.id.login_progress);
37 |
38 | etMail = (EditText) findViewById(R.id.email);
39 | etPwd = (EditText) findViewById(R.id.password);
40 |
41 | btnLogin = (Button) findViewById(R.id.email_sign_in_button);
42 | }
43 |
44 | public void login(View view) {
45 | String mail = etMail.getText().toString().trim();
46 | String pwd = etPwd.getText().toString().trim();
47 |
48 | new LoginTask().execute(mail, pwd);
49 | }
50 |
51 | class LoginTask extends AsyncTask {
52 |
53 | @Override
54 | protected void onPreExecute() {
55 | super.onPreExecute();
56 | progressBar.setVisibility(View.VISIBLE);
57 | }
58 |
59 | @Override
60 | protected Boolean doInBackground(String... params) {
61 | String address = params[0];
62 | String pwd = params[1];
63 | String host = "smtp." + address.substring(address.lastIndexOf("@") + 1);
64 |
65 | final LoginInfo info = new LoginInfo();
66 | info.mailServerHost = host;
67 | info.mailServerPort = "25";
68 | info.userName = address;
69 | info.password = pwd;
70 | info.validate = true;
71 | Session session = MailHelper.getInstance(LoginActivity.this).login(info);
72 | if (session != null) {
73 | MailApp.session = session;
74 | MailApp.info = info;
75 | return true;
76 | }
77 | return false;
78 | }
79 |
80 | @Override
81 | protected void onPostExecute(Boolean result) {
82 | if (result) {
83 | Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
84 | startActivity(new Intent(LoginActivity.this, MainActivity.class));
85 | } else {
86 | Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
87 | }
88 | progressBar.setVisibility(View.GONE);
89 | }
90 | }
91 | }
92 |
93 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yuyh/simplemail/MailApp.java:
--------------------------------------------------------------------------------
1 | package com.yuyh.simplemail;
2 |
3 | import android.app.Application;
4 |
5 | import com.yuyh.library.simplemail.bean.LoginInfo;
6 |
7 | import javax.mail.Session;
8 |
9 | /**
10 | * @author yuyh.
11 | * @date 2016/8/1.
12 | */
13 | public class MailApp extends Application {
14 |
15 | public static Session session;
16 | public static LoginInfo info;
17 |
18 | @Override
19 | public void onCreate() {
20 | super.onCreate();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yuyh/simplemail/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.yuyh.simplemail;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.util.Log;
6 |
7 | import com.yuyh.library.simplemail.MailHelper;
8 | import com.yuyh.library.simplemail.bean.ReceiverMailInfo;
9 | import com.yuyh.library.simplemail.bean.SendMailInfo;
10 | import com.yuyh.library.simplemail.constant.Constants;
11 |
12 | import java.util.List;
13 |
14 | import javax.mail.MessagingException;
15 | import javax.mail.Session;
16 |
17 | public class MainActivity extends AppCompatActivity {
18 |
19 | private MailHelper helper;
20 |
21 | @Override
22 | protected void onCreate(Bundle savedInstanceState) {
23 | super.onCreate(savedInstanceState);
24 | setContentView(R.layout.activity_main);
25 |
26 | helper = MailHelper.getInstance(MainActivity.this);
27 |
28 | new Thread(new Runnable() {
29 | @Override
30 | public void run() {
31 | Session session = MailApp.session;
32 | if(session != null){
33 | try {
34 | List list = helper.getAllMail(Constants.MailFolder.INBOX, MailApp.info, session);
35 | Log.i("TAG", list.size()+"");
36 |
37 | SendMailInfo info = new SendMailInfo();
38 | info.fromAddress = MailApp.info.userName;
39 | info.subject = "测试邮件";
40 | info.content = "测试邮件 内容";
41 | info.receivers = new String[]{"352091626@qq.com"};
42 | helper.sendMail(info, session);
43 | } catch (MessagingException e) {
44 | e.printStackTrace();
45 | }
46 | }
47 | }
48 | }).start();
49 |
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_login.xml:
--------------------------------------------------------------------------------
1 |
12 |
13 |
14 |
21 |
22 |
26 |
27 |
32 |
33 |
36 |
37 |
46 |
47 |
48 |
49 |
52 |
53 |
65 |
66 |
67 |
68 |
77 |
78 |
79 |
80 |
81 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smuyyh/SimpleMail/5398221b449be8f5583cba08daef659e9f9152e4/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smuyyh/SimpleMail/5398221b449be8f5583cba08daef659e9f9152e4/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smuyyh/SimpleMail/5398221b449be8f5583cba08daef659e9f9152e4/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smuyyh/SimpleMail/5398221b449be8f5583cba08daef659e9f9152e4/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smuyyh/SimpleMail/5398221b449be8f5583cba08daef659e9f9152e4/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | SimpleMail
3 | sign
4 |
5 |
6 | Email
7 | Password
8 | 登录
9 | Sign in
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.1.0'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smuyyh/SimpleMail/5398221b449be8f5583cba08daef659e9f9152e4/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.3"
6 |
7 | defaultConfig {
8 | minSdkVersion 14
9 | targetSdkVersion 23
10 | versionCode 1
11 | versionName "1.0"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | compile fileTree(dir: 'libs', include: ['*.jar'])
23 | compile 'com.android.support:appcompat-v7:23.4.0'
24 | }
25 |
--------------------------------------------------------------------------------
/library/libs/activation.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smuyyh/SimpleMail/5398221b449be8f5583cba08daef659e9f9152e4/library/libs/activation.jar
--------------------------------------------------------------------------------
/library/libs/additionnal.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smuyyh/SimpleMail/5398221b449be8f5583cba08daef659e9f9152e4/library/libs/additionnal.jar
--------------------------------------------------------------------------------
/library/libs/mail.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smuyyh/SimpleMail/5398221b449be8f5583cba08daef659e9f9152e4/library/libs/mail.jar
--------------------------------------------------------------------------------
/library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\AndroidDev\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yuyh/library/simplemail/MailHelper.java:
--------------------------------------------------------------------------------
1 | package com.yuyh.library.simplemail;
2 |
3 | import android.content.Context;
4 |
5 | import com.yuyh.library.simplemail.bean.Attachment;
6 | import com.yuyh.library.simplemail.bean.LoginInfo;
7 | import com.yuyh.library.simplemail.bean.ReceiverMailInfo;
8 | import com.yuyh.library.simplemail.bean.SendMailInfo;
9 | import com.yuyh.library.simplemail.constant.Constants;
10 | import com.yuyh.library.simplemail.utils.MailAuthenticator;
11 |
12 | import java.io.UnsupportedEncodingException;
13 | import java.util.ArrayList;
14 | import java.util.Date;
15 | import java.util.HashMap;
16 | import java.util.List;
17 |
18 | import javax.activation.CommandMap;
19 | import javax.activation.DataHandler;
20 | import javax.activation.FileDataSource;
21 | import javax.activation.MailcapCommandMap;
22 | import javax.mail.Address;
23 | import javax.mail.BodyPart;
24 | import javax.mail.Folder;
25 | import javax.mail.Message;
26 | import javax.mail.MessagingException;
27 | import javax.mail.Multipart;
28 | import javax.mail.Session;
29 | import javax.mail.Store;
30 | import javax.mail.Transport;
31 | import javax.mail.internet.InternetAddress;
32 | import javax.mail.internet.MimeBodyPart;
33 | import javax.mail.internet.MimeMessage;
34 | import javax.mail.internet.MimeMultipart;
35 | import javax.mail.internet.MimeUtility;
36 |
37 | /**
38 | * @author yuyh.
39 | * @date 2016/8/1.
40 | */
41 | public class MailHelper {
42 |
43 | private static MailHelper instance;
44 | private Context context;
45 | private List mailList;
46 | private HashMap serviceHashMap;
47 |
48 | public static MailHelper getInstance(Context context) {
49 | if (instance == null)
50 | instance = new MailHelper(context);
51 | return instance;
52 | }
53 |
54 | public MailHelper(Context context) {
55 | this.context = context;
56 | }
57 |
58 | /**
59 | * 邮箱登录
60 | *
61 | * @param info
62 | * @return
63 | */
64 | public Session login(LoginInfo info) {
65 | // 创建密码验证器
66 | MailAuthenticator authenticator = null;
67 | if (info.validate) {
68 | authenticator = new MailAuthenticator(info.userName, info.password);
69 | }
70 |
71 | Session session = Session.getDefaultInstance(info.getProperties(), authenticator);
72 |
73 | try {
74 | Transport transport = session.getTransport("smtp");
75 | transport.connect(info.mailServerHost, info.userName, info.password);
76 | } catch (MessagingException e) {
77 | e.printStackTrace();
78 | return null;
79 | }
80 |
81 | return session;
82 | }
83 |
84 |
85 | /**
86 | * 发送邮件
87 | *
88 | * @param mailInfo
89 | * @param session
90 | * @return
91 | */
92 | public boolean sendMail(SendMailInfo mailInfo, Session session) {
93 | try {
94 | Message mailMessage = new MimeMessage(session);
95 |
96 | // 发件人
97 | Address fromAddress = new InternetAddress(mailInfo.fromAddress);
98 | mailMessage.setFrom(fromAddress);
99 |
100 | // 收件人
101 | Address[] toAddress = null;
102 | String[] receivers = mailInfo.receivers;
103 | if (receivers != null) {
104 | toAddress = new InternetAddress[receivers.length];
105 | for (int i = 0; i < receivers.length; i++) {
106 | toAddress[i] = new InternetAddress(receivers[i]);
107 | }
108 | mailMessage.setRecipients(Message.RecipientType.TO, toAddress);
109 | } else {
110 | return false;
111 | }
112 |
113 | mailMessage.setSubject(mailInfo.subject);
114 | mailMessage.setSentDate(new Date());
115 |
116 | Multipart multipart = new MimeMultipart();
117 | BodyPart mbpContent = new MimeBodyPart();
118 | mbpContent.setContent(mailInfo.content, "text/html;charset=gb2312");// 给BodyPart对象设置内容和格式/编码方式
119 | multipart.addBodyPart(mbpContent);
120 |
121 | FileDataSource fds;
122 | List list = mailInfo.attachmentInfos;
123 | if(list != null && !list.isEmpty()) {
124 | for (Attachment atta : list) {
125 | fds = new FileDataSource(atta.filePath);
126 | BodyPart mbpFile = new MimeBodyPart();
127 | mbpFile.setDataHandler(new DataHandler(fds));
128 | try {
129 | mbpFile.setFileName(MimeUtility.encodeText(fds.getName()));
130 | } catch (UnsupportedEncodingException e) {
131 | e.printStackTrace();
132 | }
133 | multipart.addBodyPart(mbpFile);
134 | }
135 | }
136 | mailMessage.setContent(multipart);
137 | mailMessage.saveChanges();
138 |
139 | // 设置邮件支持多种格式
140 | MailcapCommandMap mcm = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
141 | mcm.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
142 | mcm.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
143 | mcm.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
144 | mcm.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
145 | mcm.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
146 | CommandMap.setDefaultCommandMap(mcm);
147 |
148 | // 发送邮件
149 | Transport.send(mailMessage);
150 | return true;
151 | } catch (MessagingException ex) {
152 | ex.printStackTrace();
153 | }
154 | return false;
155 | }
156 |
157 | public List getAllMail(Constants.MailFolder mailFolder, LoginInfo info, Session session) throws MessagingException {
158 | List mailList = new ArrayList<>();
159 | // 连接服务器
160 | Store store = session.getStore("pop3");
161 | String temp = info.mailServerHost;
162 | String host = temp.replace("smtp", "pop");
163 | store.connect(host, info.userName, info.password);
164 | // 打开文件夹
165 | Folder folder = store.getFolder(mailFolder.value);
166 | folder.open(Folder.READ_ONLY);
167 | // 总的邮件数
168 | int mailCount = folder.getMessageCount();
169 | if (mailCount == 0) {
170 | folder.close(true);
171 | store.close();
172 | return null;
173 | } else {
174 | // 取得所有的邮件
175 | Message[] messages = folder.getMessages();
176 | for (int i = 0; i < messages.length; i++) {
177 | // 自定义的邮件对象
178 | ReceiverMailInfo reciveMail = new ReceiverMailInfo((MimeMessage) messages[i]);
179 | mailList.add(reciveMail);// 添加到邮件列表中
180 | }
181 | return mailList;
182 | }
183 | }
184 | }
185 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yuyh/library/simplemail/bean/Attachment.java:
--------------------------------------------------------------------------------
1 | package com.yuyh.library.simplemail.bean;
2 |
3 | import java.io.File;
4 | import java.io.Serializable;
5 |
6 | /**
7 | * @author yuyh.
8 | * @date 2016/8/1.
9 | */
10 | public class Attachment implements Serializable {
11 |
12 | public String filePath;
13 | public String fileName;
14 | public long fileSize;
15 |
16 | public Attachment() {
17 | super();
18 | }
19 |
20 | public Attachment(String filePath, String fileName, long fileSize) {
21 | super();
22 | this.filePath = filePath;
23 | this.fileName = fileName;
24 | this.fileSize = fileSize;
25 | }
26 |
27 | public static String convertStorage(long size) {
28 | long kb = 1024;
29 | long mb = kb * 1024;
30 | long gb = mb * 1024;
31 |
32 | if (size >= gb) {
33 | return String.format("%.1f GB", (float) size / gb);
34 | } else if (size >= mb) {
35 | float f = (float) size / mb;
36 | return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
37 | } else if (size >= kb) {
38 | float f = (float) size / kb;
39 | return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
40 | } else
41 | return String.format("%d B", size);
42 | }
43 |
44 | /**
45 | * 获取完整的目录名字
46 | *
47 | * @param filepath
48 | * @return
49 | */
50 | public static String getNameFromFilepath(String filepath) {
51 | int pos = filepath.lastIndexOf('/');
52 | if (pos != -1) {
53 | return filepath.substring(pos + 1);
54 | }
55 | return "";
56 | }
57 |
58 |
59 | /**
60 | * 获取附件
61 | *
62 | * @param filePath
63 | * @return
64 | */
65 | public static Attachment GetFileInfo(String filePath) {
66 | File file = new File(filePath);
67 | if (!file.exists())
68 | return null;
69 | Attachment fileInfo = new Attachment();
70 | fileInfo.fileName = getNameFromFilepath(filePath);
71 | fileInfo.filePath = filePath;
72 | fileInfo.fileSize = file.length();
73 | return fileInfo;
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yuyh/library/simplemail/bean/Email.java:
--------------------------------------------------------------------------------
1 | package com.yuyh.library.simplemail.bean;
2 |
3 | import java.io.Serializable;
4 | import java.util.ArrayList;
5 |
6 | /**
7 | * @author yuyh.
8 | * @date 2016/8/1.
9 | */
10 | public class Email implements Serializable {
11 |
12 | public String messageID;
13 | public String from;
14 | public String to;
15 | public String cc;
16 | public String bcc;
17 | public String subject;
18 | public String sentdata;
19 | public String content;
20 | public boolean replysign;
21 | public boolean html;
22 | public boolean news;
23 | public ArrayList attachments;
24 | public String charset;
25 |
26 | }
--------------------------------------------------------------------------------
/library/src/main/java/com/yuyh/library/simplemail/bean/LoginInfo.java:
--------------------------------------------------------------------------------
1 | package com.yuyh.library.simplemail.bean;
2 |
3 | import java.io.Serializable;
4 | import java.util.Properties;
5 |
6 | /**
7 | * @author yuyh.
8 | * @date 2016/8/1.
9 | */
10 | public class LoginInfo implements Serializable {
11 |
12 | /**
13 | * 发件服务器地址和端口
14 | */
15 | public String mailServerHost;
16 | public String mailServerPort = "25";
17 | /**
18 | * 发件用户名与密码
19 | */
20 | public String userName;
21 | public String password;
22 | /**
23 | * 是否需要身份验证
24 | */
25 | public boolean validate = false;
26 |
27 | /**
28 | * 获得邮件会话属性
29 | */
30 | public Properties getProperties() {
31 | Properties p = new Properties();
32 | p.put("mail.smtp.host", this.mailServerHost);
33 | p.put("mail.smtp.port", this.mailServerPort);
34 | p.put("mail.smtp.auth", validate ? "true" : "false");
35 | return p;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yuyh/library/simplemail/bean/MailDraft.java:
--------------------------------------------------------------------------------
1 | package com.yuyh.library.simplemail.bean;
2 |
3 | /**
4 | * @author yuyh.
5 | * @date 2016/8/1.
6 | */
7 | public class MailDraft {
8 |
9 | public int id;
10 | public String mailfrom;
11 | public String mailto;
12 | public String subject;
13 | public String content;
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yuyh/library/simplemail/bean/ReceiverMailInfo.java:
--------------------------------------------------------------------------------
1 | package com.yuyh.library.simplemail.bean;
2 |
3 | import com.yuyh.library.simplemail.utils.StringUtils;
4 |
5 | import java.io.IOException;
6 | import java.io.InputStream;
7 | import java.io.Serializable;
8 | import java.text.SimpleDateFormat;
9 | import java.util.ArrayList;
10 | import java.util.Date;
11 | import java.util.Locale;
12 |
13 | import javax.mail.Flags;
14 | import javax.mail.Message;
15 | import javax.mail.MessagingException;
16 | import javax.mail.Multipart;
17 | import javax.mail.Part;
18 | import javax.mail.internet.InternetAddress;
19 | import javax.mail.internet.MimeMessage;
20 | import javax.mail.internet.MimeMultipart;
21 | import javax.mail.internet.MimeUtility;
22 | import javax.mail.util.ByteArrayDataSource;
23 |
24 | /**
25 | * @author yuyh.
26 | * @date 2016/8/1.
27 | */
28 | public class ReceiverMailInfo implements Serializable {
29 |
30 | private MimeMessage mimeMessage = null;
31 | private StringBuffer mailContent = new StringBuffer();// 邮件内容
32 | private String dataFormat = "yyyy-MM-dd HH:mm:ss";
33 | private String charset;
34 | private boolean html;
35 | private ArrayList attachments = new ArrayList();
36 | private ArrayList attachmentsInputStreams = new ArrayList();
37 |
38 | public ReceiverMailInfo(MimeMessage mimeMessage) {
39 | this.mimeMessage = mimeMessage;
40 | try {
41 | charset = parseCharset(mimeMessage.getContentType());
42 | } catch (MessagingException e) {
43 | e.printStackTrace();
44 | }
45 | }
46 |
47 | /**
48 | * 解析邮件字符集编码
49 | *
50 | * @param contentType
51 | * @return
52 | */
53 | private String parseCharset(String contentType) {
54 | if (!contentType.contains("charset")) {
55 | return null;
56 | }
57 | if (contentType.contains("gbk")) {
58 | return "GBK";
59 | } else if (contentType.contains("GB2312") || contentType.contains("gb18030")) {
60 | return "gb2312";
61 | } else {
62 | String sub = contentType.substring(contentType.indexOf("charset") + 8).replace("\"", "");
63 | if (sub.contains(";")) {
64 | return sub.substring(0, sub.indexOf(";"));
65 | } else {
66 | return sub;
67 | }
68 | }
69 | }
70 |
71 | /**
72 | * 解析流
73 | *
74 | * @param is
75 | * @return
76 | * @throws IOException
77 | * @throws MessagingException
78 | */
79 | public String parseInputStream(InputStream is) throws IOException, MessagingException {
80 | StringBuffer str = new StringBuffer();
81 | byte[] readByte = new byte[1024];
82 | int count;
83 | try {
84 | while ((count = is.read(readByte)) != -1) {
85 | if (charset == null) {
86 | str.append(new String(readByte, 0, count, "GBK"));
87 | } else {
88 | str.append(new String(readByte, 0, count, charset));
89 | }
90 | }
91 | } catch (Exception e) {
92 | e.printStackTrace();
93 | }
94 | return str.toString();
95 | }
96 |
97 | /**
98 | * 获取发件人姓名和地址
99 | *
100 | * @return
101 | * @throws Exception
102 | */
103 | public String getFrom() throws Exception {
104 | InternetAddress address[] = (InternetAddress[]) mimeMessage.getFrom();
105 | String addr = address[0].getAddress();
106 | String name = address[0].getPersonal();
107 | if (addr == null) {
108 | addr = "";
109 | }
110 | if (name == null) {
111 | name = "";
112 | } else if (charset == null) {
113 | name = StringUtils.encodeGBK(name);
114 | }
115 | String nameAddr = name + "<" + addr + ">";
116 | return nameAddr;
117 | }
118 |
119 | /**
120 | * 获取收件人姓名和地址
121 | *
122 | * @param type TO:收件人
123 | * CC:抄送人
124 | * BCC:密送人
125 | * @return
126 | * @throws Exception
127 | */
128 | public String getMailAddress(Message.RecipientType type) throws Exception {
129 | String mailAddr = "";
130 | InternetAddress[] address = (InternetAddress[]) mimeMessage.getRecipients(type);
131 | if (address != null) {
132 | for (int i = 0; i < address.length; i++) {
133 | String mailaddress = address[i].getAddress();
134 | if (mailaddress != null) {
135 | mailaddress = MimeUtility.decodeText(mailaddress);
136 | } else {
137 | mailaddress = "";
138 | }
139 | String name = address[i].getPersonal();
140 | if (name != null) {
141 | name = MimeUtility.decodeText(name);
142 | } else {
143 | name = "";
144 | }
145 | mailAddr = name + "<" + mailaddress + ">";
146 | }
147 | }
148 | return mailAddr;
149 | }
150 |
151 | /**
152 | * 获取邮件标题
153 | *
154 | * @return
155 | */
156 | public String getSubject() {
157 | String subject = "";
158 | try {
159 | subject = mimeMessage.getSubject();
160 | if (subject.indexOf("=?gb18030?") != -1) {
161 | subject = subject.replace("gb18030", "gb2312");
162 | }
163 | subject = MimeUtility.decodeText(subject);
164 | if (charset == null) {
165 | subject = StringUtils.encodeGBK(subject);
166 | }
167 | } catch (Exception e) {
168 | }
169 | return subject;
170 | }
171 |
172 | /**
173 | * 取得邮件日期
174 | *
175 | * @throws MessagingException
176 | */
177 | public String getSentData() throws MessagingException {
178 | Date sentdata = mimeMessage.getSentDate();
179 | if (sentdata != null) {
180 | SimpleDateFormat format = new SimpleDateFormat(dataFormat, Locale.CHINA);
181 | return format.format(sentdata);
182 | } else {
183 | return "未知";
184 | }
185 | }
186 |
187 | /**
188 | * 取得邮件内容
189 | *
190 | * @throws Exception
191 | */
192 | public String getMailContent() throws Exception {
193 | compileMailContent((Part) mimeMessage);
194 | String content = mailContent.toString();
195 | if (content.indexOf("") != -1) {
196 | html = true;
197 | }
198 | mailContent.setLength(0);
199 | return content;
200 | }
201 |
202 | private void compileMailContent(Part part) throws Exception {
203 | String contentType = part.getContentType();
204 | boolean connName = false;
205 | if (contentType.indexOf("name") != -1) {
206 | connName = true;
207 | }
208 | if (part.isMimeType("text/plain") && !connName) {
209 | //String content = parseInputStream((InputStream) part.getContent());
210 | String content = part.getContent().toString();
211 | mailContent.append(content);
212 | } else if (part.isMimeType("text/html") && !connName) {
213 | html = true;
214 | //String content = parseInputStream((InputStream) part.getContent());
215 | String content = part.getContent().toString();
216 | mailContent.append(content);
217 | } else if (part.isMimeType("multipart/*") || part.isMimeType("message/rfc822")) {
218 | if (part.getContent() instanceof Multipart) {
219 | Multipart multipart = (Multipart) part.getContent();
220 | int counts = multipart.getCount();
221 | for (int i = 0; i < counts; i++) {
222 | compileMailContent(multipart.getBodyPart(i));
223 | }
224 | } else {
225 | Multipart multipart = new MimeMultipart(new ByteArrayDataSource(part.getInputStream(), "multipart/*"));
226 | int counts = multipart.getCount();
227 | for (int i = 0; i < counts; i++) {
228 | compileMailContent(multipart.getBodyPart(i));
229 | }
230 | }
231 | } else if (part.getDisposition() != null && part.getDisposition().equals(Part.ATTACHMENT)) {
232 | // 获取附件
233 | String filename = part.getFileName();
234 | if (filename != null) {
235 | if (filename.indexOf("=?gb18030?") != -1) {
236 | filename = filename.replace("gb18030", "gb2312");
237 | }
238 | filename = MimeUtility.decodeText(filename);
239 | attachments.add(filename);
240 | attachmentsInputStreams.add(part.getInputStream());
241 | }
242 | }
243 | }
244 |
245 | /**
246 | * 是否有回执
247 | *
248 | * @throws MessagingException
249 | */
250 | public boolean getReplySign() throws MessagingException {
251 | boolean replySign = false;
252 | String needreply[] = mimeMessage.getHeader("Disposition-Notification-To");
253 | if (needreply != null) {
254 | replySign = true;
255 | }
256 | return replySign;
257 | }
258 |
259 | /**
260 | * 获取「message-ID」
261 | *
262 | * @throws MessagingException
263 | */
264 | public String getMessageID() throws MessagingException {
265 | return mimeMessage.getMessageID();
266 | }
267 |
268 | /**
269 | * 是否新邮件
270 | *
271 | * @throws MessagingException
272 | */
273 | public boolean isNew() throws MessagingException {
274 | boolean isnew = false;
275 | Flags flags = ((Message) mimeMessage).getFlags();
276 | Flags.Flag[] flag = flags.getSystemFlags();
277 | for (int i = 0; i < flag.length; i++) {
278 | if (flag[i] == Flags.Flag.SEEN) {
279 | isnew = true;
280 | break;
281 | }
282 | }
283 | return isnew;
284 | }
285 | }
286 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yuyh/library/simplemail/bean/SendMailInfo.java:
--------------------------------------------------------------------------------
1 | package com.yuyh.library.simplemail.bean;
2 |
3 | import java.io.Serializable;
4 | import java.util.List;
5 |
6 | /**
7 | * @author yuyh.
8 | * @date 2016/8/1.
9 | */
10 | public class SendMailInfo implements Serializable {
11 | /**
12 | * 发件人地址
13 | */
14 | public String fromAddress;
15 | /**
16 | * 邮件主题
17 | */
18 | public String subject;
19 | /**
20 | * 邮件内容
21 | */
22 | public String content;
23 | /**
24 | * 邮件附件
25 | */
26 | public List attachmentInfos;
27 | /**
28 | * 邮件的接收者(可以有多个)
29 | */
30 | public String[] receivers;
31 | }
32 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yuyh/library/simplemail/bean/User.java:
--------------------------------------------------------------------------------
1 | package com.yuyh.library.simplemail.bean;
2 |
3 | /**
4 | * @author yuyh.
5 | * @date 2016/8/1.
6 | */
7 | public class User {
8 |
9 | public int id;
10 | public String name;
11 | public String address;
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yuyh/library/simplemail/constant/Constants.java:
--------------------------------------------------------------------------------
1 | package com.yuyh.library.simplemail.constant;
2 |
3 | /**
4 | * @author yuyh.
5 | * @date 2016/8/1.
6 | */
7 | public class Constants {
8 |
9 | public enum MailFolder {
10 | INBOX("INBOX");
11 |
12 | public String value;
13 |
14 | MailFolder(String value) {
15 | this.value = value;
16 | }
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yuyh/library/simplemail/utils/MailAuthenticator.java:
--------------------------------------------------------------------------------
1 | package com.yuyh.library.simplemail.utils;
2 |
3 | import javax.mail.Authenticator;
4 | import javax.mail.PasswordAuthentication;
5 |
6 | /**
7 | * 邮件账号密码校验器
8 | *
9 | * @author yuyh.
10 | * @date 2016/8/1.
11 | */
12 | public class MailAuthenticator extends Authenticator {
13 | private String userName = null;
14 | private String password = null;
15 |
16 | public MailAuthenticator(String username, String password) {
17 | this.userName = username;
18 | this.password = password;
19 | }
20 |
21 | @Override
22 | protected PasswordAuthentication getPasswordAuthentication() {
23 | return new PasswordAuthentication(userName, password);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yuyh/library/simplemail/utils/StringUtils.java:
--------------------------------------------------------------------------------
1 | package com.yuyh.library.simplemail.utils;
2 |
3 | import android.text.SpannableString;
4 | import android.text.SpannableStringBuilder;
5 | import android.text.Spanned;
6 | import android.text.style.AbsoluteSizeSpan;
7 | import android.text.style.ForegroundColorSpan;
8 |
9 | /**
10 | * @author yuyh.
11 | * @date 2016/8/1.
12 | */
13 | public class StringUtils {
14 |
15 | public static String encodeGBK(String str) {
16 | try {
17 | String strEncode = StringUtils.getEncoding(str);
18 | String temp = new String(str.getBytes(strEncode), "GBK");
19 | return temp;
20 | } catch (java.io.IOException ex) {
21 |
22 | return null;
23 | }
24 | }
25 |
26 | public static String getEncoding(String str) {
27 | String encode = "GB2312";
28 | try {
29 | if (str.equals(new String(str.getBytes(encode), encode))) {
30 | String s = encode;
31 | return s;
32 | }
33 | } catch (Exception exception) {
34 | }
35 | encode = "ISO-8859-1";
36 | try {
37 | if (str.equals(new String(str.getBytes(encode), encode))) {
38 | String s1 = encode;
39 | return s1;
40 | }
41 | } catch (Exception exception1) {
42 | }
43 | encode = "UTF-8";
44 | try {
45 | if (str.equals(new String(str.getBytes(encode), encode))) {
46 | String s2 = encode;
47 | return s2;
48 | }
49 | } catch (Exception exception2) {
50 | }
51 | encode = "GBK";
52 | try {
53 | if (str.equals(new String(str.getBytes(encode), encode))) {
54 | String s3 = encode;
55 | return s3;
56 | }
57 | } catch (Exception exception3) {
58 | }
59 | return "";
60 | }
61 |
62 | /**
63 | * 创建{文字内容、字体颜色、字体大小}分段文字集合体
64 | *
65 | * @param text
66 | * @param color
67 | * @param textSize
68 | * @return
69 | */
70 | public static SpannableStringBuilder creSpanString(String[] text, int[] color, int[] textSize) {
71 | if (text == null || color == null || textSize == null)
72 | throw new IllegalArgumentException("参数不能为空");
73 | if (text.length != color.length || text.length != textSize.length)
74 | throw new IllegalArgumentException("参数长度不一致");
75 | SpannableStringBuilder sb = new SpannableStringBuilder();
76 | try {
77 | for (int i = 0; i < text.length; i++) {
78 | SpannableString sp = new SpannableString(text[i]);
79 | sp.setSpan(new ForegroundColorSpan(color[i]), 0, sp.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
80 | sp.setSpan(new AbsoluteSizeSpan(textSize[i], true), 0, sp.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
81 | sb.append(sp);
82 | }
83 | return sb;
84 | } catch (Exception e) {
85 | e.printStackTrace();
86 | }
87 | return sb;
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | library
3 |
4 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':library'
2 |
--------------------------------------------------------------------------------