├── .gitignore ├── .idea ├── encodings.xml ├── gradle.xml ├── inspectionProfiles │ └── Project_Default.xml ├── markdown-navigator.xml ├── markdown-navigator │ └── profiles_settings.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── louisgeek │ │ └── javamail │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── louisgeek │ │ │ └── javamail │ │ │ ├── MainActivity.java │ │ │ ├── microsoft │ │ │ ├── OutlookEmailFactory.java │ │ │ └── OutlookProtocol.java │ │ │ ├── netease │ │ │ ├── NeteaseEmailFactory.java │ │ │ └── NeteaseProtocolSmtp.java │ │ │ ├── sina │ │ │ ├── SinaEmailFactory.java │ │ │ └── SinaProtocolSmtp.java │ │ │ └── tencent │ │ │ ├── TencentEmailFactory.java │ │ │ └── TencentProtocolSmtp.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── louisgeek │ └── javamail │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── louisgeek │ │ └── javamail │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── louisgeek │ │ │ └── javamail │ │ │ ├── EmailMessage.java │ │ │ ├── EmailProtocol.java │ │ │ ├── EmailService.java │ │ │ ├── JavaEmailHelper.java │ │ │ ├── MyLog.java │ │ │ ├── abstracts │ │ │ └── AbstractProtocolSmtp.java │ │ │ └── interfaces │ │ │ └── IEmailFactory.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── louisgeek │ └── javamail │ └── ExampleUnitTest.java └── 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 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 36 | -------------------------------------------------------------------------------- /.idea/markdown-navigator.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 36 | 37 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /.idea/markdown-navigator/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | Android 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 49 | 50 | C:\Users\classichu\AppData\Roaming\Subversion 51 | 52 | 53 | 54 | 55 | 56 | 57 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | Step 1. Add the JitPack repository to your build file 4 | 5 | Add it in your root build.gradle at the end of repositories: 6 | 7 | ```groovy 8 | allprojects { 9 | repositories { 10 | jcenter() 11 | maven { url "https://jitpack.io" } 12 | } 13 | } 14 | ``` 15 | 16 | 17 | 18 | Step 2. Add the dependency [![img](https://jitpack.io/v/louisgeek/LouisJavaMail.svg)](https://jitpack.io/#louisgeek/LouisJavaMail) 19 | 20 | ``` 21 | dependencies { 22 | compile 'com.github.louisgeek:LouisJavaMail:x.x.x' 23 | } 24 | ``` 25 | 26 | 27 | 28 | 使用方法: 29 | 30 | 1 继承 AbstractProtocolSmtp 配置邮箱Smtp服务信息 31 | 32 | ```java 33 | public class NeteaseProtocolSmtp extends AbstractProtocolSmtp { 34 | private static final String MAIL_HOST = "smtp.163.com"; 35 | private static final int MAIL_HOST_PORT = 25; 36 | private static final int MAIL_HOST_PORT_SSL = 465;// 465 / 994 37 | 38 | public NeteaseProtocolSmtp(EmailService emailService) { 39 | super(emailService); 40 | } 41 | 42 | @Override 43 | public EmailProtocol setupEmailProtocol() { 44 | return EmailProtocol.create(MAIL_HOST, MAIL_HOST_PORT, MAIL_HOST_PORT_SSL); 45 | } 46 | } 47 | ``` 48 | 49 | 2 实现 IEmailFactory 工厂 配置账户信息 50 | ```java 51 | public class NeteaseEmailFactory implements IEmailFactory { 52 | private static final String USER_NAME = "bsoft_app@163.com"; 53 | private static final String AUTH_CODE = "xxx";//163 的授权码 54 | //发送方的邮箱 55 | private static final String FROM_EMAIL = "bsoft_app@163.com"; 56 | //发送方姓名 57 | private static final String FROM_NAME = "louisgeek_netease"; 58 | 59 | @Override 60 | public AbstractProtocolSmtp getProtocolSmtp() { 61 | return new NeteaseProtocolSmtp(EmailService.create(USER_NAME, AUTH_CODE, FROM_EMAIL, FROM_NAME)); 62 | } 63 | } 64 | ``` 65 | 66 | 67 | 3 发送邮件 68 | ```java 69 | // 普通 70 | IEmailFactory neteaseEmailFactory = new NeteaseEmailFactory(); 71 | try { 72 | EmailMessage emailMessage = EmailMessage.newBuilder() 73 | .setTitle("哇陈搜有限公司") 74 | .setText("哇陈搜有限公司1") 75 | .setContent("哇陈搜有限公司2") 76 | .setTOAddresses(new Address[]{new InternetAddress(toEmail)}) 77 | .build(); 78 | 79 | neteaseEmailFactory.getProtocolSmtp().send(emailMessage); 80 | 81 | // 82 | File imagePath = new File(Environment.getExternalStorageDirectory() + File.separator + "temp" + File.separator + "zfq.jpg"); 83 | // 84 | File filePath = new File(getFilesDir() + File.separator + "temp" + File.separator); 85 | if (!filePath.exists()) { 86 | filePath.mkdirs(); 87 | } 88 | File file = new File(filePath, "test_email.txt"); 89 | try { 90 | if (!file.exists()) { 91 | file.createNewFile(); 92 | } 93 | FileOutputStream fileOutputStream = new FileOutputStream(file); 94 | fileOutputStream.write("test_email content 中文".getBytes("utf-8")); 95 | fileOutputStream.close(); 96 | // 97 | EmailMessage emailMessageWithFile = EmailMessage.newBuilder() 98 | .setTitle("test_163_email") 99 | .setText("test_163_email text") 100 | // .setContent("test_163_email 带附件") 101 | // .setFiles(new File[]{file}) 102 | .setTOAddresses(new Address[]{new InternetAddress(toEmail)}) 103 | .build(); 104 | 105 | //带附件 106 | neteaseEmailFactory.getProtocolSmtp().send(emailMessageWithFile); 107 | 108 | // 109 | 110 | EmailMessage emailMessageWithImage = EmailMessage.newBuilder() 111 | .setTitle("test_163_email") 112 | .setText("test_163_email text") 113 | .setContent("test_163_email 图文 ") 114 | .setImageFiles(new File[]{imagePath}) 115 | .setTOAddresses(new Address[]{new InternetAddress(toEmail)}) 116 | .build(); 117 | 118 | // 图文 119 | neteaseEmailFactory.getProtocolSmtp().send(emailMessageWithImage); 120 | // 121 | EmailMessage emailMessageWithImageAndFile = EmailMessage.newBuilder() 122 | .setTitle("test_163_email") 123 | .setText("test_163_email text") 124 | .setContent("test_163_email 图文 带附件") 125 | .setImageFiles(new File[]{imagePath}) 126 | .setFiles(new File[]{file}) 127 | .setTOAddresses(new Address[]{new InternetAddress(toEmail)}) 128 | .build(); 129 | 130 | // 图文 带附件 131 | neteaseEmailFactory.getProtocolSmtp().send(emailMessageWithImageAndFile); 132 | 133 | ``` 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 详见博客: 142 | 143 | http://blog.csdn.net/RichieZhu/article/details/79578483 144 | 145 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 26 5 | defaultConfig { 6 | applicationId "com.louisgeek.javamail" 7 | minSdkVersion 15 8 | targetSdkVersion 22 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | // Enabling multidex support. 13 | multiDexEnabled true 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | 22 | } 23 | 24 | dependencies { 25 | implementation fileTree(include: ['*.jar'], dir: 'libs') 26 | implementation 'com.android.support:appcompat-v7:26.1.0' 27 | implementation 'com.android.support.constraint:constraint-layout:1.0.2' 28 | testImplementation 'junit:junit:4.12' 29 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 30 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 31 | implementation project(':library') 32 | implementation 'com.sun.mail:android-mail:1.6.0' 33 | implementation 'com.sun.mail:android-activation:1.6.0' 34 | } 35 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/louisgeek/javamail/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.louisgeek.javamail; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.louisgeek.javamail", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/louisgeek/javamail/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.louisgeek.javamail; 2 | 3 | import android.os.Bundle; 4 | import android.os.Environment; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | import android.widget.Toast; 8 | 9 | import com.louisgeek.javamail.interfaces.IEmailFactory; 10 | import com.louisgeek.javamail.microsoft.OutlookEmailFactory; 11 | import com.louisgeek.javamail.netease.NeteaseEmailFactory; 12 | import com.louisgeek.javamail.sina.SinaEmailFactory; 13 | 14 | import java.io.File; 15 | import java.io.FileOutputStream; 16 | import java.io.IOException; 17 | import java.util.concurrent.ExecutorService; 18 | import java.util.concurrent.Executors; 19 | 20 | import javax.mail.Address; 21 | import javax.mail.internet.AddressException; 22 | import javax.mail.internet.InternetAddress; 23 | 24 | 25 | public class MainActivity extends AppCompatActivity { 26 | private String toEmail = "louisgeek@126.com"; 27 | private String ccEmail = "louisgeek@163.com"; 28 | private String bccEmail = "louisgeek@qq.com"; 29 | 30 | 31 | private ExecutorService executorService = Executors.newFixedThreadPool(2); 32 | 33 | @Override 34 | protected void onCreate(Bundle savedInstanceState) { 35 | super.onCreate(savedInstanceState); 36 | 37 | 38 | setContentView(R.layout.activity_main); 39 | findViewById(R.id.id_test).setOnClickListener(new View.OnClickListener() { 40 | @Override 41 | public void onClick(View v) { 42 | 43 | executorService.execute(new Runnable() { 44 | @Override 45 | public void run() { 46 | 47 | 48 | IEmailFactory neteaseEmailFactory = new NeteaseEmailFactory(); 49 | try { 50 | EmailMessage emailMessage = EmailMessage.newBuilder() 51 | .setTitle("杭船业软件有限公司") 52 | .setText("杭船业软件有限公司1") 53 | .setContent("杭船业软件有限公司2") 54 | .setTOAddresses(new Address[]{new InternetAddress(toEmail)}) 55 | .build(); 56 | 57 | neteaseEmailFactory.getProtocolSmtp().send(emailMessage); 58 | 59 | 60 | EmailMessage emailMessage2 = EmailMessage.newBuilder() 61 | .setTitle("杭船业软件有限公司回执") 62 | .setText("杭船业软件有限公司1 回执") 63 | .setContent("杭船业软件有限公司2 回执") 64 | .setCCAddresses(new Address[]{new InternetAddress(ccEmail)}) 65 | .setBCCAddresses(new Address[]{new InternetAddress(bccEmail)}) 66 | .setTOAddresses(new Address[]{new InternetAddress(toEmail)}) 67 | .setReadReceipt(true) 68 | .build(); 69 | 70 | neteaseEmailFactory.getProtocolSmtp().send(emailMessage2); 71 | } catch (AddressException e) { 72 | MyLog.e(e.getMessage()); 73 | } 74 | //网易 163 邮箱发 email 75 | File imagePath = new File(Environment.getExternalStorageDirectory() + File.separator + "temp" + File.separator + "zfq.jpg"); 76 | // 77 | File filePath = new File(getFilesDir() + File.separator + "temp" + File.separator); 78 | if (!filePath.exists()) { 79 | filePath.mkdirs(); 80 | } 81 | File file = new File(filePath, "test_email.txt"); 82 | try { 83 | if (!file.exists()) { 84 | file.createNewFile(); 85 | } 86 | FileOutputStream fileOutputStream = new FileOutputStream(file); 87 | fileOutputStream.write("test_email content 中文".getBytes("utf-8")); 88 | fileOutputStream.close(); 89 | // 90 | EmailMessage emailMessageWithFile = EmailMessage.newBuilder() 91 | .setTitle("test_163_email") 92 | .setText("test_163_email text") 93 | // .setContent("test_163_email 带附件") 94 | // .setFiles(new File[]{file}) 95 | .setTOAddresses(new Address[]{new InternetAddress(toEmail)}) 96 | .build(); 97 | 98 | //带附件 99 | //neteaseEmailFactory.getProtocolSmtp().send(emailMessageWithFile); 100 | 101 | 102 | EmailMessage emailMessageWithImage = EmailMessage.newBuilder() 103 | .setTitle("test_163_email") 104 | .setText("test_163_email text") 105 | .setContent("test_163_email 图文 ") 106 | .setImageFiles(new File[]{imagePath}) 107 | .setTOAddresses(new Address[]{new InternetAddress(toEmail)}) 108 | .build(); 109 | 110 | // 图文 111 | // neteaseEmailFactory.getProtocolSmtp().send(emailMessageWithImage); 112 | 113 | EmailMessage emailMessageWithImageAndFile = EmailMessage.newBuilder() 114 | .setTitle("test_163_email") 115 | .setText("test_163_email text") 116 | .setContent("test_163_email 图文 带附件") 117 | .setImageFiles(new File[]{imagePath}) 118 | .setFiles(new File[]{file}) 119 | .setTOAddresses(new Address[]{new InternetAddress(toEmail)}) 120 | .build(); 121 | 122 | // 图文 带附件 123 | //neteaseEmailFactory.getProtocolSmtp().send(emailMessageWithImageAndFile); 124 | 125 | 126 | // 127 | IEmailFactory sinaEmailFactory = new SinaEmailFactory(); 128 | 129 | EmailMessage emailMessageS = EmailMessage.newBuilder() 130 | .setTitle("test_sina_email") 131 | .setText("test_sina_email text") 132 | .setContent("test_sina_email 图文 带附件") 133 | .setTOAddresses(new Address[]{new InternetAddress(toEmail)}) 134 | .build(); 135 | 136 | // sinaEmailFactory.getProtocolSmtp().send(emailMessageS); 137 | 138 | 139 | EmailMessage emailMessageSWithImage = EmailMessage.newBuilder() 140 | .setTitle("test_sss_email") 141 | .setText("test_sss_email text") 142 | .setContent("test_sss_email 图文 ") 143 | .setImageFiles(new File[]{imagePath}) 144 | .setTOAddresses(new Address[]{new InternetAddress(toEmail)}) 145 | .build(); 146 | //sinaEmailFactory.getProtocolSmtp().send(emailMessageSWithImage); 147 | 148 | 149 | IEmailFactory outlookEmailFactory = new OutlookEmailFactory(); 150 | EmailMessage emailMessageSWithImage55 = EmailMessage.newBuilder() 151 | .setTitle("test_out_email") 152 | .setText("test_out_email text") 153 | .setContent("test_out_email 图文 ") 154 | .setImageFiles(new File[]{imagePath}) 155 | .setTOAddresses(new Address[]{new InternetAddress(toEmail)}) 156 | .build(); 157 | //outlookEmailFactory.getProtocolSmtp().send(emailMessageSWithImage55); 158 | 159 | 160 | } catch (IOException e) { 161 | MyLog.e(e.getMessage()); 162 | } catch (AddressException e) { 163 | MyLog.e(e.getMessage()); 164 | } 165 | 166 | 167 | } 168 | }); 169 | 170 | 171 | // 172 | Toast.makeText(MainActivity.this, "点击成功!", Toast.LENGTH_SHORT).show(); 173 | 174 | } 175 | }); 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /app/src/main/java/com/louisgeek/javamail/microsoft/OutlookEmailFactory.java: -------------------------------------------------------------------------------- 1 | package com.louisgeek.javamail.microsoft; 2 | 3 | import com.louisgeek.javamail.EmailService; 4 | import com.louisgeek.javamail.abstracts.AbstractProtocolSmtp; 5 | import com.louisgeek.javamail.interfaces.IEmailFactory; 6 | 7 | /** 8 | * Created by classichu on 2018/3/19. 9 | */ 10 | 11 | public class OutlookEmailFactory implements IEmailFactory { 12 | private static final String USER_NAME = "louisgeek@live.com"; 13 | private static final String AUTH_CODE = "xxx";//密码 14 | //发送方的邮箱 15 | private static final String FROM_EMAIL = "louisgeek@live.com"; 16 | //发送方姓名 17 | private static final String FROM_NAME = "louisgeek_outlook"; 18 | 19 | @Override 20 | public AbstractProtocolSmtp getProtocolSmtp() { 21 | return new OutlookProtocol(EmailService.create(USER_NAME, AUTH_CODE, FROM_EMAIL, FROM_NAME)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/louisgeek/javamail/microsoft/OutlookProtocol.java: -------------------------------------------------------------------------------- 1 | package com.louisgeek.javamail.microsoft; 2 | 3 | import com.louisgeek.javamail.EmailProtocol; 4 | import com.louisgeek.javamail.EmailService; 5 | import com.louisgeek.javamail.abstracts.AbstractProtocolSmtp; 6 | 7 | /** 8 | * Created by classichu on 2018/3/19. 9 | */ 10 | 11 | public class OutlookProtocol extends AbstractProtocolSmtp { 12 | private static final String MAIL_HOST = "smtp-mail.outlook.com"; 13 | private static final int MAIL_HOST_PORT = 25; 14 | private static final int MAIL_HOST_PORT_SSL = 587; 15 | 16 | public OutlookProtocol(EmailService mEmailService) { 17 | super(mEmailService); 18 | } 19 | 20 | @Override 21 | protected EmailProtocol setupEmailProtocol() { 22 | return EmailProtocol.create(MAIL_HOST, MAIL_HOST_PORT, MAIL_HOST_PORT_SSL); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/louisgeek/javamail/netease/NeteaseEmailFactory.java: -------------------------------------------------------------------------------- 1 | package com.louisgeek.javamail.netease; 2 | 3 | 4 | import com.louisgeek.javamail.EmailService; 5 | import com.louisgeek.javamail.abstracts.AbstractProtocolSmtp; 6 | import com.louisgeek.javamail.interfaces.IEmailFactory; 7 | 8 | /** 9 | * Created by classichu on 2018/3/14. 10 | */ 11 | 12 | public class NeteaseEmailFactory implements IEmailFactory { 13 | private static final String USER_NAME = "bsoft_app@163.com"; 14 | private static final String AUTH_CODE = "xxx";//163 的授权码 15 | //发送方的邮箱 16 | private static final String FROM_EMAIL = "bsoft_app@163.com"; 17 | //发送方姓名 18 | private static final String FROM_NAME = "louisgeek_netease"; 19 | 20 | @Override 21 | public AbstractProtocolSmtp getProtocolSmtp() { 22 | return new NeteaseProtocolSmtp(EmailService.create(USER_NAME, AUTH_CODE, FROM_EMAIL, FROM_NAME)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/louisgeek/javamail/netease/NeteaseProtocolSmtp.java: -------------------------------------------------------------------------------- 1 | package com.louisgeek.javamail.netease; 2 | 3 | 4 | import com.louisgeek.javamail.EmailProtocol; 5 | import com.louisgeek.javamail.EmailService; 6 | import com.louisgeek.javamail.abstracts.AbstractProtocolSmtp; 7 | 8 | /** 9 | * Created by classichu on 2018/3/14. 10 | */ 11 | 12 | public class NeteaseProtocolSmtp extends AbstractProtocolSmtp { 13 | private static final String MAIL_HOST = "smtp.163.com"; 14 | private static final int MAIL_HOST_PORT = 25; 15 | private static final int MAIL_HOST_PORT_SSL = 465;// 465 / 994 16 | 17 | public NeteaseProtocolSmtp(EmailService emailService) { 18 | super(emailService); 19 | } 20 | 21 | @Override 22 | public EmailProtocol setupEmailProtocol() { 23 | return EmailProtocol.create(MAIL_HOST, MAIL_HOST_PORT, MAIL_HOST_PORT_SSL); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/louisgeek/javamail/sina/SinaEmailFactory.java: -------------------------------------------------------------------------------- 1 | package com.louisgeek.javamail.sina; 2 | 3 | import com.louisgeek.javamail.EmailService; 4 | import com.louisgeek.javamail.abstracts.AbstractProtocolSmtp; 5 | import com.louisgeek.javamail.interfaces.IEmailFactory; 6 | 7 | /** 8 | * Created by louisgeek on 2018/3/19. 9 | */ 10 | 11 | public class SinaEmailFactory implements IEmailFactory { 12 | private static final String USER_NAME = "louisgeek@sina.com"; 13 | private static final String AUTH_CODE = "xxx";//密码 14 | //发送方的邮箱 15 | private static final String FROM_EMAIL = "louisgeek@sina.com"; 16 | //发送方姓名 17 | private static final String FROM_NAME = "louisgeek_sina"; 18 | 19 | @Override 20 | public AbstractProtocolSmtp getProtocolSmtp() { 21 | return new SinaProtocolSmtp(EmailService.create(USER_NAME, AUTH_CODE, FROM_EMAIL, FROM_NAME)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/louisgeek/javamail/sina/SinaProtocolSmtp.java: -------------------------------------------------------------------------------- 1 | package com.louisgeek.javamail.sina; 2 | 3 | 4 | import com.louisgeek.javamail.EmailProtocol; 5 | import com.louisgeek.javamail.EmailService; 6 | import com.louisgeek.javamail.abstracts.AbstractProtocolSmtp; 7 | 8 | /** 9 | * Created by louisgeek on 2018/3/19. 10 | */ 11 | 12 | public class SinaProtocolSmtp extends AbstractProtocolSmtp { 13 | private static final String MAIL_HOST = "smtp.sina.com"; 14 | private static final int MAIL_HOST_PORT = 25; 15 | private static final int MAIL_HOST_PORT_SSL = 465;// 465 / 587 16 | 17 | public SinaProtocolSmtp(EmailService mEmailService) { 18 | super(mEmailService); 19 | } 20 | 21 | @Override 22 | public EmailProtocol setupEmailProtocol() { 23 | return EmailProtocol.create(MAIL_HOST, MAIL_HOST_PORT, MAIL_HOST_PORT_SSL); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/louisgeek/javamail/tencent/TencentEmailFactory.java: -------------------------------------------------------------------------------- 1 | package com.louisgeek.javamail.tencent; 2 | 3 | 4 | import com.louisgeek.javamail.EmailService; 5 | import com.louisgeek.javamail.abstracts.AbstractProtocolSmtp; 6 | import com.louisgeek.javamail.interfaces.IEmailFactory; 7 | 8 | /** 9 | * Created by classichu on 2018/3/14. 10 | */ 11 | 12 | public class TencentEmailFactory implements IEmailFactory { 13 | private static final String USER_NAME = "louisgeek@qq.com"; 14 | //报 535 错误 更改QQ密码以及独立密码会触发授权码过期,需要重新获取新的授权码登录。 15 | private static final String AUTH_CODE = "xxx";//qq邮箱 授权码 16 | //发送方的邮箱 17 | private static final String FROM_EMAIL = "louisgeek@qq.com"; 18 | //发送方姓名 19 | private static final String FROM_NAME = "louisgeek_tencent"; 20 | 21 | @Override 22 | public AbstractProtocolSmtp getProtocolSmtp() { 23 | return new TencentProtocolSmtp(EmailService.create(USER_NAME, AUTH_CODE, FROM_EMAIL, FROM_NAME)); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/louisgeek/javamail/tencent/TencentProtocolSmtp.java: -------------------------------------------------------------------------------- 1 | package com.louisgeek.javamail.tencent; 2 | 3 | 4 | import com.louisgeek.javamail.EmailProtocol; 5 | import com.louisgeek.javamail.EmailService; 6 | import com.louisgeek.javamail.abstracts.AbstractProtocolSmtp; 7 | 8 | /** 9 | * Created by classichu on 2018/3/14. 10 | */ 11 | 12 | public class TencentProtocolSmtp extends AbstractProtocolSmtp { 13 | private static final String MAIL_HOST = "smtp.qq.com"; 14 | private static final int MAIL_HOST_PORT = 25; 15 | private static final int MAIL_HOST_PORT_SSL = 465;// 465 / 587 16 | 17 | public TencentProtocolSmtp(EmailService emailService) { 18 | super(emailService); 19 | } 20 | 21 | @Override 22 | public EmailProtocol setupEmailProtocol() { 23 | return EmailProtocol.create(MAIL_HOST, MAIL_HOST_PORT, MAIL_HOST_PORT_SSL); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/louisgeek/LG_JavaMail/612cd0aa6070128d74e73ad53179d7012f3d6100/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/louisgeek/LG_JavaMail/612cd0aa6070128d74e73ad53179d7012f3d6100/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/louisgeek/LG_JavaMail/612cd0aa6070128d74e73ad53179d7012f3d6100/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/louisgeek/LG_JavaMail/612cd0aa6070128d74e73ad53179d7012f3d6100/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/louisgeek/LG_JavaMail/612cd0aa6070128d74e73ad53179d7012f3d6100/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/louisgeek/LG_JavaMail/612cd0aa6070128d74e73ad53179d7012f3d6100/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/louisgeek/LG_JavaMail/612cd0aa6070128d74e73ad53179d7012f3d6100/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/louisgeek/LG_JavaMail/612cd0aa6070128d74e73ad53179d7012f3d6100/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/louisgeek/LG_JavaMail/612cd0aa6070128d74e73ad53179d7012f3d6100/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/louisgeek/LG_JavaMail/612cd0aa6070128d74e73ad53179d7012f3d6100/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | LouisJavaMail 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/louisgeek/javamail/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.louisgeek.javamail; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | 5 | repositories { 6 | google() 7 | jcenter() 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.0.1' 11 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0' // Add this line 12 | 13 | // NOTE: Do not place your application dependencies here; they belong 14 | // in the individual module build.gradle files 15 | } 16 | } 17 | 18 | allprojects { 19 | repositories { 20 | google() 21 | jcenter() 22 | } 23 | } 24 | 25 | task clean(type: Delete) { 26 | delete rootProject.buildDir 27 | } 28 | -------------------------------------------------------------------------------- /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 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/louisgeek/LG_JavaMail/612cd0aa6070128d74e73ad53179d7012f3d6100/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Mar 16 09:53:30 CST 2018 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-4.1-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 | apply plugin: 'com.github.dcendents.android-maven' 3 | group = 'com.github.louisgeek' 4 | 5 | android { 6 | compileSdkVersion 26 7 | defaultConfig { 8 | minSdkVersion 15 9 | targetSdkVersion 22 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | packagingOptions { 22 | // 23 | //pickFirst 'META-INF/LICENSE.txt' // picks the JavaMail license file 24 | } 25 | } 26 | 27 | dependencies { 28 | implementation fileTree(include: ['*.jar'], dir: 'libs') 29 | implementation 'com.android.support:appcompat-v7:26.1.0' 30 | implementation 'com.android.support.constraint:constraint-layout:1.0.2' 31 | testImplementation 'junit:junit:4.12' 32 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 33 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 34 | implementation 'com.sun.mail:android-mail:1.6.0' 35 | implementation 'com.sun.mail:android-activation:1.6.0' 36 | } 37 | -------------------------------------------------------------------------------- /library/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /library/src/androidTest/java/com/louisgeek/javamail/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.louisgeek.javamail; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.louisgeek.javamail.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /library/src/main/java/com/louisgeek/javamail/EmailMessage.java: -------------------------------------------------------------------------------- 1 | package com.louisgeek.javamail; 2 | 3 | import java.io.File; 4 | import java.util.ArrayList; 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import javax.mail.Address; 9 | import javax.mail.internet.AddressException; 10 | import javax.mail.internet.InternetAddress; 11 | 12 | /** 13 | * Created by louisgeek on 2018/3/18. 14 | */ 15 | 16 | public class EmailMessage { 17 | private Address fromAddress; 18 | private List
toAddresses; 19 | private List
ccAddresses; 20 | private List
bccAddresses; 21 | private String title; 22 | private String text; 23 | private String content; 24 | private List imageFiles; 25 | private List files; 26 | private boolean readReceipt; 27 | 28 | public Address getFromAddress() { 29 | return fromAddress; 30 | } 31 | 32 | public List
getToAddresses() { 33 | return toAddresses; 34 | } 35 | 36 | public List
getCcAddresses() { 37 | return ccAddresses; 38 | } 39 | 40 | public List
getBccAddresses() { 41 | return bccAddresses; 42 | } 43 | 44 | public String getTitle() { 45 | return title; 46 | } 47 | 48 | public String getText() { 49 | return text; 50 | } 51 | 52 | public String getContent() { 53 | return content; 54 | } 55 | 56 | public List getImageFiles() { 57 | return imageFiles; 58 | } 59 | 60 | public List getFiles() { 61 | return files; 62 | } 63 | 64 | public boolean isReadReceipt() { 65 | return readReceipt; 66 | } 67 | 68 | private EmailMessage(Builder builder) { 69 | fromAddress = builder.fromAddress; 70 | toAddresses = builder.toAddresses; 71 | ccAddresses = builder.ccAddresses; 72 | bccAddresses = builder.bccAddresses; 73 | title = builder.title; 74 | text = builder.text; 75 | content = builder.content; 76 | imageFiles = builder.imageFiles; 77 | files = builder.files; 78 | readReceipt = builder.readReceipt; 79 | } 80 | 81 | public static Builder newBuilder() { 82 | return new Builder(); 83 | } 84 | 85 | public static final class Builder { 86 | private Address fromAddress; 87 | private List
toAddresses; 88 | private List
ccAddresses; 89 | private List
bccAddresses; 90 | private String title; 91 | private String text; 92 | private String content; 93 | private List imageFiles; 94 | private List files; 95 | private boolean readReceipt; 96 | 97 | private Builder() { 98 | } 99 | 100 | public Builder setFromAddress(Address val) { 101 | fromAddress = val; 102 | return this; 103 | } 104 | 105 | public Builder setFromAddress(String val) { 106 | try { 107 | fromAddress = new InternetAddress(val); 108 | } catch (AddressException e) { 109 | MyLog.e(e.getMessage()); 110 | } 111 | return this; 112 | } 113 | 114 | public Builder setTOAddresses(List
val) { 115 | toAddresses = val; 116 | return this; 117 | } 118 | 119 | public Builder setTOAddresses(Address[] val) { 120 | if (val != null) { 121 | toAddresses = new ArrayList<>(Arrays.asList(val)); 122 | } 123 | return this; 124 | } 125 | 126 | public Builder addTOAddresses(List
val) { 127 | if (val != null) { 128 | if (toAddresses == null) { 129 | toAddresses = new ArrayList<>(); 130 | } 131 | toAddresses.addAll(val); 132 | } 133 | return this; 134 | } 135 | 136 | public Builder addTOAddresses(Address[] val) { 137 | if (val != null) { 138 | if (toAddresses == null) { 139 | toAddresses = new ArrayList<>(); 140 | } 141 | List
addresses = new ArrayList<>(Arrays.asList(val)); 142 | toAddresses.addAll(addresses); 143 | } 144 | return this; 145 | } 146 | 147 | public Builder addTOAddress(Address val) { 148 | if (val != null) { 149 | if (toAddresses == null) { 150 | toAddresses = new ArrayList<>(); 151 | } 152 | toAddresses.add(val); 153 | } 154 | return this; 155 | } 156 | 157 | public Builder addTOAddress(String val) { 158 | if (val != null) { 159 | if (toAddresses == null) { 160 | toAddresses = new ArrayList<>(); 161 | } 162 | try { 163 | Address address = new InternetAddress(val); 164 | toAddresses.add(address); 165 | } catch (AddressException e) { 166 | MyLog.e(e.getMessage()); 167 | } 168 | } 169 | return this; 170 | } 171 | 172 | /////////////////////// 173 | public Builder setCCAddresses(List
val) { 174 | ccAddresses = val; 175 | return this; 176 | } 177 | 178 | public Builder setCCAddresses(Address[] val) { 179 | if (val != null) { 180 | ccAddresses = new ArrayList<>(Arrays.asList(val)); 181 | } 182 | return this; 183 | } 184 | 185 | public Builder addCCAddresses(List
val) { 186 | if (val != null) { 187 | if (ccAddresses == null) { 188 | ccAddresses = new ArrayList<>(); 189 | } 190 | ccAddresses.addAll(val); 191 | } 192 | return this; 193 | } 194 | 195 | public Builder addCCAddresses(Address[] val) { 196 | if (val != null) { 197 | if (ccAddresses == null) { 198 | ccAddresses = new ArrayList<>(); 199 | } 200 | List
addresses = new ArrayList<>(Arrays.asList(val)); 201 | ccAddresses.addAll(addresses); 202 | } 203 | return this; 204 | } 205 | 206 | public Builder addCCAddress(Address val) { 207 | if (val != null) { 208 | if (ccAddresses == null) { 209 | ccAddresses = new ArrayList<>(); 210 | } 211 | ccAddresses.add(val); 212 | } 213 | return this; 214 | } 215 | 216 | public Builder addCCAddress(String val) { 217 | if (val != null) { 218 | if (ccAddresses == null) { 219 | ccAddresses = new ArrayList<>(); 220 | } 221 | try { 222 | Address address = new InternetAddress(val); 223 | ccAddresses.add(address); 224 | } catch (AddressException e) { 225 | MyLog.e(e.getMessage()); 226 | } 227 | } 228 | return this; 229 | } 230 | 231 | ////////////////////// 232 | public Builder setBCCAddresses(List
val) { 233 | bccAddresses = val; 234 | return this; 235 | } 236 | 237 | public Builder setBCCAddresses(Address[] val) { 238 | if (val != null) { 239 | bccAddresses = new ArrayList<>(Arrays.asList(val)); 240 | } 241 | return this; 242 | } 243 | 244 | public Builder addBCCAddresses(List
val) { 245 | if (val != null) { 246 | if (bccAddresses == null) { 247 | bccAddresses = new ArrayList<>(); 248 | } 249 | bccAddresses.addAll(val); 250 | } 251 | return this; 252 | } 253 | 254 | public Builder addBCCAddresses(Address[] val) { 255 | if (val != null) { 256 | if (bccAddresses == null) { 257 | bccAddresses = new ArrayList<>(); 258 | } 259 | List
addresses = new ArrayList<>(Arrays.asList(val)); 260 | bccAddresses.addAll(addresses); 261 | } 262 | return this; 263 | } 264 | 265 | public Builder addBCCAddress(Address val) { 266 | if (val != null) { 267 | if (bccAddresses == null) { 268 | bccAddresses = new ArrayList<>(); 269 | } 270 | bccAddresses.add(val); 271 | } 272 | return this; 273 | } 274 | 275 | public Builder addBCCAddress(String val) { 276 | if (val != null) { 277 | if (bccAddresses == null) { 278 | bccAddresses = new ArrayList<>(); 279 | } 280 | try { 281 | Address address = new InternetAddress(val); 282 | bccAddresses.add(address); 283 | } catch (AddressException e) { 284 | MyLog.e(e.getMessage()); 285 | } 286 | } 287 | return this; 288 | } 289 | 290 | /// 291 | public Builder setTitle(String val) { 292 | title = val; 293 | return this; 294 | } 295 | 296 | public Builder setText(String val) { 297 | text = val; 298 | return this; 299 | } 300 | 301 | public Builder setContent(String val) { 302 | content = val; 303 | return this; 304 | } 305 | 306 | public Builder setImageFiles(List val) { 307 | imageFiles = val; 308 | return this; 309 | } 310 | 311 | public Builder setImageFiles(File[] val) { 312 | if (val != null) { 313 | imageFiles = new ArrayList<>(Arrays.asList(val)); 314 | } 315 | return this; 316 | } 317 | 318 | public Builder addImageFiles(List val) { 319 | if (val != null) { 320 | if (imageFiles == null) { 321 | imageFiles = new ArrayList<>(); 322 | } 323 | imageFiles.addAll(val); 324 | } 325 | return this; 326 | } 327 | 328 | public Builder addImageFiles(File[] val) { 329 | if (val != null) { 330 | if (imageFiles == null) { 331 | imageFiles = new ArrayList<>(); 332 | } 333 | List addresses = new ArrayList<>(Arrays.asList(val)); 334 | imageFiles.addAll(addresses); 335 | } 336 | return this; 337 | } 338 | 339 | public Builder addImageFile(File val) { 340 | if (val != null) { 341 | if (imageFiles == null) { 342 | imageFiles = new ArrayList<>(); 343 | } 344 | imageFiles.add(val); 345 | } 346 | return this; 347 | } 348 | 349 | 350 | /////// 351 | public Builder setFiles(List val) { 352 | files = val; 353 | return this; 354 | } 355 | 356 | public Builder setFiles(File[] val) { 357 | if (val != null) { 358 | files = new ArrayList<>(Arrays.asList(val)); 359 | } 360 | return this; 361 | } 362 | 363 | public Builder addFiles(List val) { 364 | if (val != null) { 365 | if (files == null) { 366 | files = new ArrayList<>(); 367 | } 368 | files.addAll(val); 369 | } 370 | return this; 371 | } 372 | 373 | public Builder addFiles(File[] val) { 374 | if (val != null) { 375 | if (files == null) { 376 | files = new ArrayList<>(); 377 | } 378 | List addresses = new ArrayList<>(Arrays.asList(val)); 379 | files.addAll(addresses); 380 | } 381 | return this; 382 | } 383 | 384 | public Builder addFile(File val) { 385 | if (val != null) { 386 | if (files == null) { 387 | files = new ArrayList<>(); 388 | } 389 | files.add(val); 390 | } 391 | return this; 392 | } 393 | 394 | 395 | public Builder setReadReceipt(boolean val) { 396 | readReceipt = val; 397 | return this; 398 | } 399 | 400 | public EmailMessage build() { 401 | return new EmailMessage(this); 402 | } 403 | } 404 | } 405 | -------------------------------------------------------------------------------- /library/src/main/java/com/louisgeek/javamail/EmailProtocol.java: -------------------------------------------------------------------------------- 1 | package com.louisgeek.javamail; 2 | 3 | /** 4 | * Created by louisgeek on 2018/3/18. 5 | */ 6 | 7 | public class EmailProtocol { 8 | private String emailHost; 9 | private int emailHostPort; 10 | private int emailHostPortSSL; 11 | private boolean debug; 12 | 13 | public static EmailProtocol create(String emailHost) { 14 | return new EmailProtocol(emailHost, 0, 0); 15 | } 16 | 17 | public static EmailProtocol create(String emailHost, int emailHostPort) { 18 | return new EmailProtocol(emailHost, emailHostPort, 0); 19 | } 20 | 21 | public static EmailProtocol create(String emailHost, int emailHostPort, int emailHostPortSSL) { 22 | return new EmailProtocol(emailHost, emailHostPort, emailHostPortSSL); 23 | } 24 | 25 | private EmailProtocol(String emailHost, int emailHostPort, int emailHostPortSSL) { 26 | this.emailHost = emailHost; 27 | this.emailHostPort = emailHostPort; 28 | this.emailHostPortSSL = emailHostPortSSL; 29 | } 30 | 31 | public String getEmailHost() { 32 | return emailHost; 33 | } 34 | 35 | public int getEmailHostPort() { 36 | return emailHostPort; 37 | } 38 | 39 | public int getEmailHostPortSSL() { 40 | return emailHostPortSSL; 41 | } 42 | 43 | public void setDebug(boolean debug) { 44 | this.debug = debug; 45 | } 46 | 47 | public boolean isDebug() { 48 | return debug; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /library/src/main/java/com/louisgeek/javamail/EmailService.java: -------------------------------------------------------------------------------- 1 | package com.louisgeek.javamail; 2 | 3 | 4 | /** 5 | * Created by louisgeek on 2018/3/18. 6 | */ 7 | 8 | public class EmailService { 9 | private String account; 10 | private String authCode; 11 | private String fromEmail; 12 | private String fromName; 13 | 14 | public static EmailService create(String account, String authCode, String fromEmail) { 15 | return new EmailService(account, authCode, fromEmail, null); 16 | } 17 | 18 | public static EmailService create(String account, String authCode, String fromEmail, String fromName) { 19 | return new EmailService(account, authCode, fromEmail, fromName); 20 | } 21 | 22 | private EmailService(String account, String authCode, String fromEmail, String fromName) { 23 | this.account = account; 24 | this.authCode = authCode; 25 | this.fromEmail = fromEmail; 26 | this.fromName = fromName; 27 | } 28 | 29 | public String getAccount() { 30 | return account; 31 | } 32 | 33 | public String getAuthCode() { 34 | return authCode; 35 | } 36 | 37 | public String getFromEmail() { 38 | return fromEmail; 39 | } 40 | 41 | public String getFromName() { 42 | return fromName; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /library/src/main/java/com/louisgeek/javamail/JavaEmailHelper.java: -------------------------------------------------------------------------------- 1 | package com.louisgeek.javamail; 2 | 3 | import android.text.TextUtils; 4 | 5 | import java.io.UnsupportedEncodingException; 6 | import java.util.regex.Matcher; 7 | import java.util.regex.Pattern; 8 | 9 | import javax.mail.Address; 10 | import javax.mail.internet.AddressException; 11 | import javax.mail.internet.InternetAddress; 12 | 13 | /** 14 | * Created by classichu on 2018/3/14. 15 | */ 16 | 17 | public class JavaEmailHelper { 18 | 19 | private static final Pattern EMAIL_PATTERN = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); 20 | 21 | public static Address[] createAddresses(String address) { 22 | return new Address[]{createAddress(address)}; 23 | } 24 | 25 | public static Address createAddress(String address) { 26 | try { 27 | return new InternetAddress(address); 28 | } catch (AddressException e) { 29 | MyLog.e(e.getMessage()); 30 | } 31 | return null; 32 | } 33 | 34 | public static Address createAddressAndName(String address, String name) { 35 | 36 | try { 37 | return new InternetAddress(address, name, "utf-8"); 38 | } catch (UnsupportedEncodingException e) { 39 | MyLog.e(e.getMessage()); 40 | } 41 | 42 | return null; 43 | } 44 | 45 | public boolean verifyEmailAddress(String emailAddr) { 46 | if (TextUtils.isEmpty(emailAddr)) { 47 | return false; 48 | } 49 | Matcher m = EMAIL_PATTERN.matcher(emailAddr); 50 | return m.matches(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /library/src/main/java/com/louisgeek/javamail/MyLog.java: -------------------------------------------------------------------------------- 1 | package com.louisgeek.javamail; 2 | 3 | import android.util.Log; 4 | 5 | /** 6 | * Created by louisgeek on 2018/3/19. 7 | */ 8 | 9 | public class MyLog { 10 | private static final boolean isLog = true; 11 | private static final String TAG = "MyLog"; 12 | 13 | public static void e(String msg) { 14 | if (isLog) { 15 | Log.e(TAG, msg); 16 | } 17 | } 18 | 19 | public static void d(String msg) { 20 | if (isLog) { 21 | Log.d(TAG, msg); 22 | } 23 | } 24 | 25 | public static void i(String msg) { 26 | if (isLog) { 27 | Log.i(TAG, msg); 28 | } 29 | } 30 | 31 | public static void w(String msg) { 32 | if (isLog) { 33 | Log.w(TAG, msg); 34 | } 35 | } 36 | 37 | public static void v(String msg) { 38 | if (isLog) { 39 | Log.v(TAG, msg); 40 | } 41 | } 42 | 43 | 44 | public static void e(String tag, String msg) { 45 | if (isLog) { 46 | Log.e(tag, msg); 47 | } 48 | } 49 | 50 | public static void d(String tag, String msg) { 51 | if (isLog) { 52 | Log.d(tag, msg); 53 | } 54 | } 55 | 56 | public static void i(String tag, String msg) { 57 | if (isLog) { 58 | Log.i(tag, msg); 59 | } 60 | } 61 | 62 | public static void w(String tag, String msg) { 63 | if (isLog) { 64 | Log.w(tag, msg); 65 | } 66 | } 67 | 68 | public static void v(String tag, String msg) { 69 | if (isLog) { 70 | Log.v(tag, msg); 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /library/src/main/java/com/louisgeek/javamail/abstracts/AbstractProtocolSmtp.java: -------------------------------------------------------------------------------- 1 | package com.louisgeek.javamail.abstracts; 2 | 3 | import android.text.TextUtils; 4 | 5 | import com.louisgeek.javamail.EmailMessage; 6 | import com.louisgeek.javamail.EmailProtocol; 7 | import com.louisgeek.javamail.EmailService; 8 | import com.louisgeek.javamail.JavaEmailHelper; 9 | import com.louisgeek.javamail.MyLog; 10 | 11 | import java.io.File; 12 | import java.io.UnsupportedEncodingException; 13 | import java.util.Date; 14 | import java.util.List; 15 | import java.util.Properties; 16 | 17 | import javax.activation.DataHandler; 18 | import javax.activation.FileDataSource; 19 | import javax.mail.Address; 20 | import javax.mail.Authenticator; 21 | import javax.mail.BodyPart; 22 | import javax.mail.Message; 23 | import javax.mail.MessagingException; 24 | import javax.mail.PasswordAuthentication; 25 | import javax.mail.Session; 26 | import javax.mail.Transport; 27 | import javax.mail.internet.AddressException; 28 | import javax.mail.internet.InternetAddress; 29 | import javax.mail.internet.MimeBodyPart; 30 | import javax.mail.internet.MimeMessage; 31 | import javax.mail.internet.MimeMultipart; 32 | import javax.mail.internet.MimeUtility; 33 | 34 | /** 35 | * Created by classichu on 2018/3/14. 36 | *

37 | * Smtp 协议 Abstract 38 | */ 39 | 40 | public abstract class AbstractProtocolSmtp { 41 | 42 | private static final String SUBTYPE_MIXED = "mixed";//纯文本,超文本二选一 内嵌资源 附件 43 | private static final String SUBTYPE_RELATED = "related";//纯文本&纯文本,超文本二选一 内嵌资源 44 | private static final String SUBTYPE_ALTERNATIVE = "alternative";//纯文本,超文本二选一 45 | 46 | private EmailService mEmailService; 47 | private EmailProtocol mEmailProtocol; 48 | 49 | 50 | public AbstractProtocolSmtp(EmailService mEmailService) { 51 | this.mEmailService = mEmailService; 52 | this.mEmailProtocol = setupEmailProtocol(); 53 | } 54 | 55 | private Properties mProperties; 56 | private Session mSession; 57 | // 58 | private String mEmailHost; 59 | private int mEmailHostPort; 60 | private int mEmailHostPortSSL; 61 | private boolean mIsDebug; 62 | // 63 | private String mFromEmail; 64 | private String mFromName; 65 | private String mAccount; 66 | private String mAuthCode; 67 | // 68 | private MimeMessage mMimeMessage; 69 | // 70 | private Address mUserAddress; 71 | private EmailMessage mEmailMessage; 72 | 73 | protected abstract EmailProtocol setupEmailProtocol(); 74 | 75 | public void send(EmailMessage emailMessage) { 76 | if (mEmailProtocol == null) { 77 | MyLog.e("send: mEmailProtocol == null || mEmailService == null"); 78 | return; 79 | } 80 | if (mEmailService == null) { 81 | MyLog.e("send: || mEmailService == null"); 82 | return; 83 | } 84 | if (emailMessage == null) { 85 | MyLog.e("send: emailMessage == null "); 86 | return; 87 | } 88 | mEmailMessage = emailMessage; 89 | // 90 | mEmailHost = mEmailProtocol.getEmailHost(); 91 | mEmailHostPort = mEmailProtocol.getEmailHostPort(); 92 | mEmailHostPortSSL = mEmailProtocol.getEmailHostPortSSL(); 93 | mIsDebug = mEmailProtocol.isDebug(); 94 | if (TextUtils.isEmpty(mEmailHost)) { 95 | MyLog.e("send: mEmailHost isEmpty"); 96 | return; 97 | } 98 | if (mEmailHostPort == 0) { 99 | mEmailHostPort = 25; 100 | } 101 | if (mEmailHostPortSSL == 0) { 102 | mEmailHostPortSSL = 465; 103 | } 104 | mFromEmail = mEmailService.getFromEmail(); 105 | mFromName = mEmailService.getFromName(); 106 | mAccount = mEmailService.getAccount(); 107 | mAuthCode = mEmailService.getAuthCode(); 108 | 109 | if (!TextUtils.isEmpty(mFromEmail)) { 110 | try { 111 | mUserAddress = new InternetAddress(mFromEmail); 112 | if (!TextUtils.isEmpty(mFromName)) { 113 | mUserAddress = JavaEmailHelper.createAddressAndName(mFromEmail, mFromName); 114 | } 115 | } catch (AddressException e) { 116 | MyLog.e(e.getMessage()); 117 | } 118 | } 119 | if (mUserAddress == null) { 120 | MyLog.e("send: mUserAddress==null"); 121 | return; 122 | } 123 | createProperties(); 124 | createSession(); 125 | createMimeMessage(); 126 | transportMessage(); 127 | } 128 | 129 | // 4 130 | private void transportMessage() { 131 | if (mMimeMessage == null) { 132 | MyLog.e("transportMessage:mMimeMessage==null "); 133 | return; 134 | } 135 | try { 136 | //Transport 邮件发送器 137 | // PasswordAuthentication 同样也是设置 USERNAME 和 PASSWORD Transport.send(msg, USERNAME, PASSWORD); 138 | //### Transport.send(msg, USERNAME, AUTH_CODE); 139 | //### Transport.send(msg); 140 | // Transport.send 如果有问题 554 错误 用这个 141 | Transport transport = mSession.getTransport(); 142 | // transport.connect(MAIL_HOST, USERNAME, AUTH_CODE); 143 | transport.connect(); 144 | transport.sendMessage(mMimeMessage, mMimeMessage.getAllRecipients()); 145 | transport.close(); 146 | } catch (MessagingException e) { 147 | MyLog.e("transportMessage:" + e.getMessage()); 148 | MyLog.e("transportMessage:" + e.getNextException()); 149 | } 150 | 151 | } 152 | 153 | 154 | // 3 155 | private void createMimeMessage() { 156 | String title = mEmailMessage.getTitle(); 157 | if (TextUtils.isEmpty(title)) { 158 | MyLog.e("send: title == null "); 159 | return; 160 | } 161 | String text = mEmailMessage.getText(); 162 | if (TextUtils.isEmpty(text)) { 163 | MyLog.e("send: content == null "); 164 | } 165 | String content = mEmailMessage.getContent(); 166 | if (TextUtils.isEmpty(content)) { 167 | MyLog.e("send: content == null "); 168 | } 169 | boolean readReceipt = mEmailMessage.isReadReceipt(); 170 | 171 | File[] imageFiles = null; 172 | List imageFileList = mEmailMessage.getImageFiles(); 173 | if (imageFileList != null) { 174 | imageFiles = imageFileList.toArray(new File[imageFileList.size()]); 175 | } 176 | File[] files = null; 177 | List fileList = mEmailMessage.getFiles(); 178 | if (fileList != null) { 179 | files = fileList.toArray(new File[fileList.size()]); 180 | } 181 | 182 | List

toAddressList = mEmailMessage.getToAddresses(); 183 | if (toAddressList == null) { 184 | MyLog.e("send: toAddressList == null "); 185 | return; 186 | } 187 | Address[] toAddresses = toAddressList.toArray(new Address[toAddressList.size()]); 188 | // 189 | Address[] ccAddresses = null; 190 | List
ccAddressList = mEmailMessage.getCcAddresses(); 191 | if (ccAddressList != null) { 192 | ccAddresses = ccAddressList.toArray(new Address[ccAddressList.size()]); 193 | } 194 | // 195 | Address[] bccAddresses = null; 196 | List
bccAddressList = mEmailMessage.getBccAddresses(); 197 | if (bccAddressList != null) { 198 | bccAddresses = bccAddressList.toArray(new Address[bccAddressList.size()]); 199 | } 200 | 201 | try { 202 | // MimeMessage 邮件类 203 | mMimeMessage = new MimeMessage(mSession); 204 | // !!!设置发件人地址,针对邮件来说的,是邮件类的属性,收件人邮箱里可以看到的,知道邮件是由谁发的 205 | // mMimeMessage.setFrom(new InternetAddress("xx软件" + "<" + xx@qq.com + ">")); 206 | mMimeMessage.setFrom(mUserAddress); 207 | //多个收信人地址 逗号隔开 可以用 Address InternetAddress 封装 208 | // mMimeMessage.addRecipients(Message.RecipientType.TO, TO_EMAIL); 209 | mMimeMessage.setRecipients(Message.RecipientType.TO, toAddresses); 210 | //抄送 (抄送一份给发件人,降低 163之类的 报 554 错误(垃圾邮件,屏蔽问题) 的概率) 211 | mMimeMessage.addRecipient(Message.RecipientType.CC, mUserAddress); 212 | if (ccAddresses != null) { 213 | //抄送 214 | mMimeMessage.addRecipients(Message.RecipientType.CC, ccAddresses); 215 | } 216 | if (bccAddresses != null) { 217 | //密送 218 | mMimeMessage.addRecipients(Message.RecipientType.BCC, bccAddresses); 219 | } 220 | mMimeMessage.setSubject(title, "UTF-8"); 221 | // mMimeMessage.setSubject(title);//设置主题 222 | mMimeMessage.setSentDate(new Date());//设置时间 223 | if (text != null) { 224 | mMimeMessage.setText(text); 225 | } 226 | if (content != null) { 227 | //纯文本 mMimeMessage.setText(content);//设置内容 可以直接使用 setContent 替代,兼容 html代码 228 | //html mMimeMessage.setContent(htmlText, "text/html;charset=UTF-8"); 229 | // mMimeMessage.setContent("html邮件测试...","text/html;charset=gbk"); 230 | /* mMimeMessage.setContent( 231 | "

这是系统自动发送的邮件,请勿回复!


"+ 232 | content+"
", 233 | "text/html;charset=UTF-8");*/ 234 | //mixed 混合 235 | MimeMultipart multipart = new MimeMultipart(SUBTYPE_MIXED); 236 | // 附件部分 237 | BodyPart fileBodyPart = createFileBodyPart(files); 238 | // 创建图文部分 239 | BodyPart contentBodyPart = createContentBodyPart(content, imageFiles); 240 | // 241 | if (fileBodyPart != null) { 242 | multipart.addBodyPart(fileBodyPart); 243 | } 244 | if (contentBodyPart != null) { 245 | multipart.addBodyPart(contentBodyPart); 246 | } 247 | mMimeMessage.setContent(multipart); 248 | } 249 | // 设置回复人 ( 收件人回复此邮件时,不设置就是默认收件人 ) 250 | // mMimeMessage.setReplyTo(); 251 | // 设置优先级(1:紧急 3:普通 5:低) 252 | // mMimeMessage.setHeader("X-Priority", "1"); 253 | if (readReceipt) { 254 | // 要求阅读回执(收件人阅读邮件时会提示回复发件人,表明邮件已收到,并已阅读) 255 | mMimeMessage.setHeader("Disposition-Notification-To", "1"); 256 | } 257 | mMimeMessage.saveChanges(); 258 | } catch (MessagingException mex) { 259 | MyLog.e("getMimeMessage:" + mex.getMessage()); 260 | MyLog.e("getMimeMessage:" + mex.getNextException()); 261 | } 262 | 263 | } 264 | 265 | // 2 266 | private void createSession() { 267 | //getDefaultInstance 会复用Properties 268 | //mSession = Session.getDefaultInstance(props, null); 269 | //mSession = Session.getInstance(props, null); 270 | //和 mProperties.put("mail.debug", true); 功能一致 271 | // mSession.setDebug(true); 272 | mSession = Session.getInstance(mProperties, new Authenticator() { 273 | @Override 274 | protected PasswordAuthentication getPasswordAuthentication() { 275 | return new PasswordAuthentication(mAccount, mAuthCode); 276 | } 277 | }); 278 | } 279 | 280 | // 1 281 | private void createProperties() { 282 | mProperties = new Properties(); 283 | //邮件的协议 pop3 smtp imap 284 | mProperties.put("mail.transport.protocol", "smtp"); 285 | //== The default host name of the mail server for both Stores and Transports. Used if the mail.xxx.host property isn't set. 286 | //如果 mail.xxx.host 未设置的时候取它的值 287 | //mProperties.put("mail.host", MAIL_HOST); 288 | //== The default user name to use when connecting to the mail server. Used if the mail.protocol.user property isn't set. 289 | //如果 mail.xxx.user 未设置的时候取它的值 290 | //mProperties.put("mail.user", FROM_USER); 291 | mProperties.put("mail.smtp.host", mEmailHost); 292 | mProperties.put("mail.smtp.port", mEmailHostPort); 293 | mProperties.put("mail.smtp.user", mUserAddress);//登录邮件服务器的用户名 294 | //mProperties.put("mail.smtp.class", "mail.smtp.class"); 295 | //qq 邮箱必须要有这个 否则报 530 错误 296 | mProperties.put("mail.smtp.starttls.enable", true); 297 | // 开启debug调试 298 | mProperties.put("mail.debug", mIsDebug); 299 | // 发件人地址,针对服务器来说的 mail.from / mail.smtp.from 邮件被退回(bounced)等时,被退到的邮箱地址 可以设置成 fromAddress 300 | //服务使用的地址,用来设置邮件的 return 地址。缺省是Message.getFrom()或InternetAddress.getLocalAddress()。mail.user / mail.smtp.user 会优先使用 301 | mProperties.put("mail.from", mUserAddress); 302 | // mProperties.put("mail.mime.address.strict", true);//严格 303 | // mProperties.put("mail.store.protocol", "mail.store.protocol"); 304 | // 发送服务器需要身份验证 305 | mProperties.put("mail.smtp.auth", true); 306 | 307 | //使用SSL安全连接 java 1.8 有问题 308 | /* mProperties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); 309 | mProperties.put("mail.smtp.socketFactory.fallback", "false"); 310 | mProperties.put("mail.smtp.socketFactory.port", mEmailProtocol.getEmailHostPortSsl());*/ 311 | } 312 | 313 | ////// 314 | 315 | private MimeBodyPart createContentBodyPart(String htmlText, File[] imgFiles) { 316 | try { 317 | MimeBodyPart contentBodyPart = new MimeBodyPart(); 318 | // 319 | //html文本 320 | MimeBodyPart htmlBodyPart = new MimeBodyPart(); 321 | //二选一 alternative 纯正文 和 超文本 322 | MimeMultipart htmlMultipart = new MimeMultipart(SUBTYPE_ALTERNATIVE); 323 | MimeBodyPart htmlBodyPartTemp = new MimeBodyPart(); 324 | htmlBodyPartTemp.setContent(htmlText, "text/html;charset=utf-8"); 325 | htmlMultipart.addBodyPart(htmlBodyPartTemp); 326 | htmlBodyPart.setContent(htmlMultipart); 327 | //图片 328 | MimeBodyPart imgBodyPart = null; 329 | if (imgFiles != null && imgFiles.length > 0) { 330 | imgBodyPart = new MimeBodyPart(); 331 | //=== 内嵌 related 把多张图打包起来 332 | MimeMultipart imgMultipart = new MimeMultipart(SUBTYPE_RELATED); 333 | for (File imgFile : imgFiles) { 334 | MimeBodyPart imgBodyPartTemp = new MimeBodyPart(); 335 | FileDataSource fileDataSource = new FileDataSource(imgFile); 336 | imgBodyPartTemp.setDataHandler(new DataHandler(fileDataSource)); 337 | //cid 338 | imgBodyPartTemp.setContentID(imgFile.getName()); 339 | imgMultipart.addBodyPart(imgBodyPartTemp); 340 | } 341 | imgBodyPart.setContent(imgMultipart); 342 | } 343 | //内嵌 related 把图文打包起来 344 | MimeMultipart contentMultipart = new MimeMultipart(SUBTYPE_RELATED); 345 | contentMultipart.addBodyPart(htmlBodyPart); 346 | if (imgBodyPart != null) { 347 | contentMultipart.addBodyPart(imgBodyPart); 348 | } 349 | // 350 | contentBodyPart.setContent(contentMultipart); 351 | return contentBodyPart; 352 | } catch (MessagingException e) { 353 | MyLog.e("createContentBodyPart:" + e.getMessage()); 354 | MyLog.e("createContentBodyPart:" + e.getNextException()); 355 | } 356 | return null; 357 | } 358 | 359 | private MimeBodyPart createFileBodyPart(File[] files) { 360 | if (files == null || files.length <= 0) { 361 | return null; 362 | } 363 | try { 364 | MimeBodyPart fileBodyPart = new MimeBodyPart(); 365 | //===内嵌 related 把多文件打包起来 366 | MimeMultipart fileMultipart = new MimeMultipart(SUBTYPE_RELATED); 367 | for (File file : files) { 368 | MimeBodyPart fileBodyPartTemp = new MimeBodyPart(); 369 | FileDataSource fileDataSource = new FileDataSource(file); 370 | fileBodyPartTemp.setDataHandler(new DataHandler(fileDataSource)); 371 | //fileBodyPartTemp.setFileName(file.getName()); 372 | fileBodyPartTemp.setFileName(MimeUtility.encodeText(file.getName())); 373 | fileMultipart.addBodyPart(fileBodyPartTemp); 374 | } 375 | fileBodyPart.setContent(fileMultipart); 376 | return fileBodyPart; 377 | } catch (UnsupportedEncodingException e) { 378 | MyLog.e("createFileBodyPart:" + e.getMessage()); 379 | } catch (MessagingException e) { 380 | MyLog.e("createFileBodyPart:" + e.getMessage()); 381 | MyLog.e("createFileBodyPart:" + e.getNextException()); 382 | } 383 | return null; 384 | } 385 | 386 | 387 | } 388 | -------------------------------------------------------------------------------- /library/src/main/java/com/louisgeek/javamail/interfaces/IEmailFactory.java: -------------------------------------------------------------------------------- 1 | package com.louisgeek.javamail.interfaces; 2 | 3 | 4 | import com.louisgeek.javamail.abstracts.AbstractProtocolSmtp; 5 | 6 | /** 7 | * Created by classichu on 2018/3/14. 8 | */ 9 | 10 | public interface IEmailFactory { 11 | AbstractProtocolSmtp getProtocolSmtp(); 12 | } 13 | -------------------------------------------------------------------------------- /library/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | library 3 | 4 | -------------------------------------------------------------------------------- /library/src/test/java/com/louisgeek/javamail/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.louisgeek.javamail; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':library' 2 | --------------------------------------------------------------------------------