├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── pom.xml ├── settings.gradle └── src ├── main └── java │ └── io │ └── jstack │ └── sendcloud4j │ ├── SendCloud.java │ ├── mail │ ├── Email.java │ ├── GeneralEmail.java │ ├── MailWebApi.java │ ├── Result.java │ ├── Substitution.java │ └── TemplateEmail.java │ └── util │ └── IOUtils.java └── test ├── java └── io │ └── jstack │ └── sendcloud4j │ ├── mail │ ├── EmailTest.java │ ├── GeneralEmailTest.java │ ├── MailWebApiTest.java │ ├── ResultTest.java │ ├── SubstitutionTest.java │ └── TemplateEmailTest.java │ └── util │ └── IOUtilsTest.java └── resouces ├── 3byte_test.txt ├── empty_test.txt └── log4j.properties /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .gradle/ 3 | 4 | */build/* 5 | 6 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio 7 | 8 | *.iml 9 | 10 | ## Directory-based project format: 11 | .idea/ 12 | # if you remove the above rule, at least ignore the following: 13 | 14 | # User-specific stuff: 15 | # .idea/workspace.xml 16 | # .idea/tasks.xml 17 | # .idea/dictionaries 18 | 19 | # Sensitive or high-churn files: 20 | # .idea/dataSources.ids 21 | # .idea/dataSources.xml 22 | # .idea/sqlDataSources.xml 23 | # .idea/dynamic.xml 24 | # .idea/uiDesigner.xml 25 | 26 | # Gradle: 27 | # .idea/gradle.xml 28 | # .idea/libraries 29 | 30 | # Mongo Explorer plugin: 31 | # .idea/mongoSettings.xml 32 | 33 | ## File-based project format: 34 | *.ipr 35 | *.iws 36 | 37 | ## Plugin-specific files: 38 | 39 | # IntelliJ 40 | /out/ 41 | /build/ 42 | 43 | # mpeltonen/sbt-idea plugin 44 | .idea_modules/ 45 | 46 | # JIRA plugin 47 | atlassian-ide-plugin.xml 48 | 49 | # Eclipse 50 | 51 | .classpath 52 | .project 53 | .settings 54 | 55 | dump.rdb 56 | 57 | /target/ 58 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - oraclejdk7 4 | - oraclejdk8 5 | - openjdk7 6 | after_success: 7 | - ./gradlew cobertura coveralls 8 | 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Denger Tung 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SendCloud SDK For Java [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.jstack/sendcloud4j/badge.svg?style=flat)](https://maven-badges.herokuapp.com/maven-central/io.jstack/sendcloud4j) 2 | 3 | [![Travis CI Build Status](https://travis-ci.org/denger/sendcloud4j.svg)](https://travis-ci.org/denger/sendcloud4j) 4 | [![Coverage Status](https://coveralls.io/repos/denger/sendcloud4j/badge.svg?branch=master&service=github)](https://coveralls.io/github/denger/sendcloud4j?branch=master) 5 | 6 | 7 | [SendCloud](http://sendcloud.sohu.com) SDK For Java 8 | 9 | * 支持 [邮箱API v2](http://sendcloud.sohu.com/doc/email_v2/send_email/#_2) 普通发送和模板发送 10 | * 支持批量发送(模板批量变量替换) 11 | * 支持添加附件发送 12 | * 支持使用 HTTP 代理发送 13 | 14 | 15 | 16 | ## Quick Start 17 | 18 | ##### Maven 19 | 20 | ```xml 21 | 22 | io.jstack 23 | sendcloud4j 24 | 0.0.5 25 | 26 | ``` 27 | 28 | ##### Gradle 29 | 30 | ```groovy 31 | compile 'io.jstack:sendcloud4j:0.0.5' 32 | ``` 33 | 34 | ##### 代码示例 35 | 36 | 1. 初始化 API,通过 SendCloud 后台获取 apiUser 和 apiKey,创建 `SendCloud` 实例 37 | ```java 38 | private String apiUser = "testApiUser"; 39 | private String apiKey = "testApiKey"; 40 | SendCloud webapi = SendCloud.createWebApi(apiUser, apiKey); 41 | ``` 42 | 43 | 1. 创建邮件实例,支持普通邮件和模板邮件。 44 | 45 | 普通邮件,邮件内容支持 HTML 或文本: 46 | ```java 47 | Email email = Email.general() 48 | .from("support@jstack.io") 49 | .fromName("JStack Support") 50 | .html("Hello World!") // or .plain() 51 | .subject("mail title") 52 | .attachment(new File("att.png")) // 添加附件 (File or byte[]) 53 | .to("denger.it@gmail.com"); 54 | ``` 55 | 模块邮件,使用 `Substitution.sub()` 替换变量值: 56 | ```java 57 | Email email = Email.template("template_order_customer") 58 | .from("support@jstack.io") 59 | .fromName("JStack Support") 60 | .substitutionVars(Substitution.sub() // 模板变量替换 61 | .set("product", "iPhone 6S") 62 | .set("name", "denger")) 63 | .attachment(new File("att.png")) // 添加附件 (File or byte[]) 64 | .to("denger.it@gmail.com"); 65 | ``` 66 | 67 | 1. 执行发送 68 | ```java 69 | Result result = webapi.mail().send(email); 70 | ``` 71 | 72 | 1. 处理发送结果 73 | ```java 74 | result.isSuccess(); //API 请求是否成功 75 | result.getStatusCode(); //API 返回码 76 | result.getMessage(); //API 返回码的中文解释 77 | ``` 78 | 79 | ## FQA 80 | 81 | ###### 如何使用代理发送邮件? 82 | 83 | 在发送邮件时通过 `viaProxy` 方法指定代码 ip 和端口,如下所示: 84 | ```java 85 | Result result = webapi.mail().viaProxy("example_ip:port").send(email); 86 | ``` 87 | 88 | 89 | *如果你有新需求或问题欢迎提 issues 或 fork :)* 90 | 91 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenLocal() 4 | mavenCentral() 5 | } 6 | dependencies { 7 | classpath "net.saliman:gradle-cobertura-plugin:2.2.8" 8 | classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.4.0' 9 | } 10 | } 11 | 12 | apply plugin: 'java' 13 | apply plugin: 'idea' 14 | apply plugin: 'maven' 15 | apply plugin: "cobertura" 16 | apply plugin: 'com.github.kt3k.coveralls' 17 | 18 | group = 'io.jstack' 19 | version = '0.0.6-SNAPSHOT' 20 | 21 | repositories { 22 | mavenCentral() 23 | mavenLocal() 24 | } 25 | 26 | dependencies { 27 | // Logging 28 | compile "org.slf4j:slf4j-api:1.7.2" 29 | compile "ch.qos.logback:logback-classic:1.0.9" 30 | compile "org.slf4j:jcl-over-slf4j:1.7.2" 31 | 32 | compile 'org.apache.httpcomponents:fluent-hc:4.5.1' 33 | compile 'org.apache.httpcomponents:httpmime:4.5.1' 34 | compile 'org.json:json:20140107' 35 | 36 | testCompile 'junit:junit:4.12' 37 | } 38 | 39 | cobertura.coverageFormats = ['html', 'xml'] // coveralls plugin depends on xml format report 40 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/denger/sendcloud4j/4f09383bb2f941d29c1222af217c824926097e4d/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Oct 28 12:26:50 CST 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.6-bin.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 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | io.jstack 5 | sendcloud4j 6 | 0.0.6-SNAPSHOT 7 | jar 8 | 9 | 10 | sendcloud4j 11 | 12 | https://github.com/denger/sendcloud4j 13 | SendCloud SDK For Java 14 | 15 | 16 | JSTACK 17 | https://jstack.io 18 | 19 | 20 | 21 | scm:git:git@github.com:denger/sendcloud4j 22 | scm:git:git@github.com:denger/sendcloud4j 23 | sendcloud4j 24 | 25 | 26 | 27 | MIT License 28 | https://opensource.org/licenses/MIT 29 | repo 30 | 31 | 32 | 33 | 34 | 35 | release 36 | 37 | 38 | nexus-release 39 | https://oss.sonatype.org/content/repositories/snapshots/ 40 | 41 | 42 | nexus-release 43 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.apache.maven.plugins 51 | maven-source-plugin 52 | 2.2.1 53 | 54 | 55 | package 56 | 57 | jar-no-fork 58 | 59 | 60 | 61 | 62 | 63 | 64 | org.apache.maven.plugins 65 | maven-javadoc-plugin 66 | 2.9.1 67 | 68 | 69 | package 70 | 71 | jar 72 | 73 | 74 | 75 | 76 | 77 | org.apache.maven.plugins 78 | maven-gpg-plugin 79 | 1.5 80 | 81 | 82 | sign-artifacts 83 | verify 84 | 85 | sign 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | denger 98 | dengertung 99 | denger.it (AT) gmail.com 100 | 101 | Developer 102 | 103 | +8 104 | 105 | 106 | 107 | 108 | 109 | UTF-8 110 | 1.7.2 111 | 4.12 112 | 1.0.9 113 | 4.5.1 114 | 20140107 115 | 116 | 117 | 118 | 119 | org.slf4j 120 | slf4j-api 121 | ${slf4j.version} 122 | 123 | 124 | org.slf4j 125 | jcl-over-slf4j 126 | ${slf4j.version} 127 | 128 | 129 | ch.qos.logback 130 | logback-classic 131 | ${logback.version} 132 | 133 | 134 | 135 | org.apache.httpcomponents 136 | fluent-hc 137 | ${httpcomponents.version} 138 | 139 | 140 | org.apache.httpcomponents 141 | httpmime 142 | ${httpcomponents.version} 143 | 144 | 145 | 146 | org.json 147 | json 148 | ${json.version} 149 | 150 | 151 | 152 | junit 153 | junit 154 | ${junit.version} 155 | test 156 | 157 | 158 | 159 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'sendcloud4j' 2 | -------------------------------------------------------------------------------- /src/main/java/io/jstack/sendcloud4j/SendCloud.java: -------------------------------------------------------------------------------- 1 | package io.jstack.sendcloud4j; 2 | 3 | 4 | import io.jstack.sendcloud4j.mail.MailWebApi; 5 | import org.apache.http.HttpHost; 6 | 7 | /** 8 | * Send Cloud API 9 | * 10 | * @author denger 11 | */ 12 | public class SendCloud { 13 | 14 | public static final String DOMAIN = "http://sendcloud.sohu.com"; 15 | 16 | public static final String API_DOMAIN = "http://api.sendcloud.net"; 17 | 18 | 19 | public static SendCloud createWebApi(String apiUser, String apiKey) { 20 | return new SendCloud(apiUser, apiKey); 21 | } 22 | 23 | private String apiUser; 24 | private String apiKey; 25 | 26 | protected SendCloud(String apiUser, String apiKey) { 27 | this.apiUser = apiUser; 28 | this.apiKey = apiKey; 29 | } 30 | 31 | public String apiUser() { 32 | return this.apiUser; 33 | } 34 | 35 | public String apiKey() { 36 | return this.apiKey; 37 | } 38 | 39 | public MailWebApi mail() { 40 | return MailWebApi.create(this); 41 | } 42 | } 43 | 44 | -------------------------------------------------------------------------------- /src/main/java/io/jstack/sendcloud4j/mail/Email.java: -------------------------------------------------------------------------------- 1 | package io.jstack.sendcloud4j.mail; 2 | 3 | import io.jstack.sendcloud4j.util.IOUtils; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | 7 | import java.io.File; 8 | import java.io.IOException; 9 | import java.util.HashMap; 10 | import java.util.Map; 11 | 12 | /** 13 | * 邮件信息,抽象普通邮箱和模块邮件公共信息。 14 | * 15 | * @param 16 | * @author denger 17 | */ 18 | public abstract class Email> { 19 | 20 | protected Logger logger = LoggerFactory.getLogger(getClass()); 21 | 22 | /** 23 | * 普通发送邮件 24 | * 25 | * @return GeneralEmail 26 | */ 27 | public static GeneralEmail general() { 28 | return new GeneralEmail(); 29 | } 30 | 31 | /** 32 | * 根据模板名创建模块邮件。 33 | * 34 | * @param template name 35 | * @return TemplateEmail 36 | */ 37 | public static TemplateEmail template(String template) { 38 | return new TemplateEmail(template); 39 | } 40 | 41 | protected Map parameters; 42 | 43 | protected Map attachments; 44 | 45 | private boolean isRewrite = false; 46 | 47 | public Email() { 48 | parameters = new HashMap(); 49 | attachments = new HashMap(); 50 | } 51 | 52 | /** 53 | * 发件人地址. 举例: support@test.com, Support

54 | * 必须设置. 55 | * 56 | * @param from 发件人地址 57 | * @return E 58 | */ 59 | public E from(String from) { 60 | return addParameter("from", from); 61 | } 62 | 63 | /** 64 | * 发件人名称. 显示如: Support 65 | * 66 | * @param fromName 发件人名称 67 | * @return E 68 | */ 69 | public E fromName(String fromName) { 70 | return addParameter("fromName", fromName); 71 | } 72 | 73 | /** 74 | * 标题. 不能为空 75 | * 76 | * @param subject 标题 77 | * @return E 78 | */ 79 | public E subject(String subject) { 80 | return addParameter("subject", subject); 81 | } 82 | 83 | /** 84 | * 设置用户默认的回复邮件地址. 如果 replyTo 没有或者为空, 则默认的回复邮件地址为 from. 85 | * 86 | * @param replyTo 回复邮件地址 87 | * @return E 88 | */ 89 | public E replyTo(String replyTo) { 90 | return addParameter("replyTo", replyTo); 91 | } 92 | 93 | /** 94 | * 收件人地址. 发送多个地址支持调用该方法多次或使用';'分隔, 如 denger@gmail.com;andy@gmail.com 95 | * 96 | * @param to 收件人地址 97 | * @return E 98 | */ 99 | public E to(String to) { 100 | if (isNotBlank(to)) { 101 | to(to.split(";")); 102 | } else { 103 | addParameter("to", null); 104 | } 105 | return getThis(); 106 | } 107 | 108 | /** 109 | * 收件人地址,批量设置。 110 | * 111 | * @param toAddrs 地址列表 112 | * @return E 113 | */ 114 | public E to(String[] toAddrs) { 115 | return addParameters("to", toAddrs, ";"); 116 | } 117 | 118 | protected String[] to() { 119 | return getParameters("to", ";"); 120 | } 121 | 122 | /** 123 | * 本次发送所使用的标签ID. 此标签需要事先创建 124 | * 125 | * @param labelid 标签ID 126 | * @return E 127 | */ 128 | public E labelId(int labelid) { 129 | return addParameter("labelId", labelid); 130 | } 131 | 132 | /** 133 | * 邮件头部信息. JSON 格式, 比如:{"header1": "value1", "header2": "value2"} 134 | * 135 | * @param headers 邮件头部信息 136 | * @return E 137 | */ 138 | public E headers(String headers) { 139 | return addParameter("headers", headers); 140 | } 141 | 142 | /** 143 | * 不返回 emailId. 有多个收件人时, 会返回 emailId 的列表 144 | * 145 | * @return E 146 | */ 147 | public E notRespEmailId() { 148 | return addParameter("respEmailId", false); 149 | } 150 | 151 | /** 152 | * 使用发送回执 153 | * 154 | * @return E 155 | */ 156 | public E useNotification() { 157 | return addParameter("useNotification", true); 158 | } 159 | 160 | /** 161 | * 添加附件。附件名默认使用文件名 162 | * 163 | * @param attachments 附件列表 164 | * @return 165 | */ 166 | public E attachments(File[] attachments) { 167 | for (File file : attachments) { 168 | addBinaryAttachment(file); 169 | } 170 | return getThis(); 171 | } 172 | 173 | /** 174 | * 添加附件。附件名默认使用文件名 175 | * 176 | * @param attachment 177 | * @return 178 | */ 179 | public E attachment(File attachment) { 180 | addBinaryAttachment(attachment); 181 | return getThis(); 182 | } 183 | 184 | /** 185 | * 添加附件 186 | * 187 | * @param attachment 附件文件 188 | * @param name 附件名 189 | * @return E 190 | */ 191 | public E attachment(File attachment, String name) { 192 | addBinaryAttachment(attachment, name); 193 | return getThis(); 194 | } 195 | 196 | /** 197 | * 添加附件 198 | * 199 | * @param attachment 附件内容 200 | * @param name 附件名 201 | * @return E 202 | */ 203 | public E attachment(byte[] attachment, String name) { 204 | attachments.put(name, attachment); 205 | return getThis(); 206 | } 207 | 208 | /** 209 | * 获取附件列表。 210 | * 211 | * @return key: name, value: bytes 212 | */ 213 | public Map attachments() { 214 | return attachments; 215 | } 216 | 217 | protected String attachmentsKey() { 218 | return "attachments"; 219 | } 220 | 221 | protected boolean hasAttachment() { 222 | return attachments().size() > 0; 223 | } 224 | 225 | protected void addBinaryAttachment(File file) { 226 | addBinaryAttachment(file, file.getName()); 227 | } 228 | 229 | protected void addBinaryAttachment(File file, String name) { 230 | try { 231 | if (file == null || !isNotBlank(name)) { 232 | throw new IllegalArgumentException( 233 | String.format("Attachment: [file: %s or name: %s illegal]", file, name)); 234 | } 235 | attachments.put(name, IOUtils.read(file)); 236 | } catch (IOException ioe) { 237 | throw new RuntimeException("Can't add attachment:" + file.getAbsolutePath(), ioe); 238 | } 239 | } 240 | 241 | protected E addParameters(String name, String[] values, String sep) { 242 | StringBuffer bufValue = new StringBuffer(); 243 | if (values != null && values.length > 0) { 244 | String origValue = getParameter(name); 245 | if (isNotBlank(origValue)) { 246 | bufValue.append(origValue); 247 | } 248 | for (String value : values) { 249 | if (!isNotBlank(value)) { 250 | continue; 251 | } 252 | if (bufValue.length() > 0) bufValue.append(sep); 253 | bufValue.append(value); 254 | } 255 | } 256 | return addParameter(name, bufValue.toString()); 257 | } 258 | 259 | protected E addParameter(String name, Object value) { 260 | String strValue = String.valueOf(value); 261 | if (value == null || !isNotBlank(strValue)) { 262 | parameters.remove(name); 263 | } else { 264 | parameters.put(name, String.valueOf(value)); 265 | } 266 | return getThis(); 267 | } 268 | 269 | protected String getParameter(String name) { 270 | return parameters.get(name); 271 | } 272 | 273 | protected String[] getParameters(String name, String sep) { 274 | String values = parameters.get(name); 275 | return isNotBlank(values) ? values.split(sep) : new String[0]; 276 | } 277 | 278 | public final Map getParameters() { 279 | if (!isRewrite) 280 | rewriteParameters(); 281 | isRewrite = true; 282 | 283 | return parameters; 284 | } 285 | 286 | private boolean isNotBlank(String value) { 287 | return value != null && value.trim().length() > 0; 288 | } 289 | 290 | protected void rewriteParameters() { 291 | } 292 | 293 | protected abstract E getThis(); 294 | 295 | } 296 | -------------------------------------------------------------------------------- /src/main/java/io/jstack/sendcloud4j/mail/GeneralEmail.java: -------------------------------------------------------------------------------- 1 | package io.jstack.sendcloud4j.mail; 2 | 3 | public class GeneralEmail extends Email { 4 | 5 | /** 6 | * 密送地址. 多个地址使用';'分隔 7 | * 8 | * @param bcc 密送地址 9 | * @return E 10 | */ 11 | public GeneralEmail bcc(String bcc) { 12 | if (bcc != null && bcc.length() > 0) { 13 | return bcc(bcc.split(";")); 14 | } else { 15 | return addParameter("bcc", null); 16 | } 17 | } 18 | 19 | /** 20 | * 密送地址列表 21 | * 22 | * @param bccs 密送地址列表 23 | * @return E 24 | */ 25 | public GeneralEmail bcc(String[] bccs) { 26 | return addParameters("bcc", bccs, ";"); 27 | } 28 | 29 | /** 30 | * 抄送地址. 多个地址使用';'分隔 31 | * 32 | * @param cc 抄送地址 33 | * @return E 34 | */ 35 | public GeneralEmail cc(String cc) { 36 | if (cc != null && cc.length() > 0) { 37 | cc(cc.split(";")); 38 | } else { 39 | return addParameter("cc", null); 40 | } 41 | return getThis(); 42 | } 43 | 44 | /** 45 | * 抄送地址. 多个地址使用';'分隔 46 | * 47 | * @param ccs 抄送地址列表 48 | * @return E 49 | */ 50 | public GeneralEmail cc(String[] ccs) { 51 | return addParameters("cc", ccs, ";"); 52 | } 53 | 54 | /** 55 | * 邮件的内容. 邮件格式为 text/html 56 | * 57 | * @param html 邮件内容 58 | * @return E 59 | */ 60 | public GeneralEmail html(String html) { 61 | return addParameter("html", html); 62 | } 63 | 64 | /** 65 | * 邮件的内容. 邮件格式为 text/plain 66 | * 67 | * @param plain 邮件内容 68 | * @return E 69 | */ 70 | public GeneralEmail plain(String plain) { 71 | return addParameter("plain", plain); 72 | } 73 | 74 | 75 | @Override 76 | protected GeneralEmail getThis() { 77 | return this; 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/io/jstack/sendcloud4j/mail/MailWebApi.java: -------------------------------------------------------------------------------- 1 | package io.jstack.sendcloud4j.mail; 2 | 3 | import io.jstack.sendcloud4j.SendCloud; 4 | import org.apache.http.HttpEntity; 5 | import org.apache.http.HttpHost; 6 | import org.apache.http.client.fluent.Form; 7 | import org.apache.http.client.fluent.Request; 8 | import org.apache.http.entity.ContentType; 9 | import org.apache.http.entity.mime.HttpMultipartMode; 10 | import org.apache.http.entity.mime.MultipartEntityBuilder; 11 | 12 | import java.io.IOException; 13 | import java.nio.charset.Charset; 14 | import java.util.Map; 15 | 16 | /** 17 | * 邮件 API v2 实现。 18 | * 19 | * @author denger 20 | */ 21 | public class MailWebApi { 22 | 23 | private SendCloud sendCloud; 24 | 25 | 26 | private static final int DEFAULT_CONNECT_TIMEOUT = 500; 27 | private static final int DEFAULT_SOCKET_TIMEOUT = 1000; 28 | 29 | private int connectTimeout; 30 | private int socketTimeout; 31 | private HttpHost proxy; 32 | 33 | private static Charset UTF_8 = Charset.forName("UTF-8"); 34 | 35 | public static final ContentType TEXT_PLAIN = ContentType.create("text/plain", UTF_8); 36 | 37 | 38 | public static MailWebApi create(SendCloud sendCloud) { 39 | return new MailWebApi(sendCloud); 40 | } 41 | 42 | private MailWebApi(SendCloud sendCloud) { 43 | this.socketTimeout = DEFAULT_SOCKET_TIMEOUT; 44 | this.connectTimeout = DEFAULT_CONNECT_TIMEOUT; 45 | this.sendCloud = sendCloud; 46 | } 47 | 48 | public MailWebApi viaProxy(String proxy) { 49 | this.proxy = HttpHost.create(proxy); 50 | return this; 51 | } 52 | 53 | public MailWebApi viaProxy(HttpHost proxy) { 54 | this.proxy = proxy; 55 | return this; 56 | } 57 | 58 | public MailWebApi socketTimeout(int socketTimeout) { 59 | this.socketTimeout = socketTimeout; 60 | return this; 61 | } 62 | public MailWebApi connectTimeout(int connectTimeout) { 63 | this.connectTimeout = connectTimeout; 64 | return this; 65 | } 66 | 67 | public Result send(Email email) { 68 | try { 69 | String jsonResult = requestSend(getSendAPIURI(email), email); 70 | return new Result(jsonResult); 71 | } catch (IOException ioe) { 72 | return Result.createExceptionResult(email, ioe); 73 | } 74 | } 75 | 76 | protected String getSendAPIURI(Email email) { 77 | return String.format("%s/apiv2/mail/%s", SendCloud.API_DOMAIN, 78 | (isTemplate(email) ? "sendtemplate" : "send")); 79 | } 80 | 81 | private boolean isTemplate(Email email) { 82 | return (email instanceof TemplateEmail); 83 | } 84 | 85 | private String requestSend(String uri, Email email) throws IOException { 86 | Request request = Request.Post(uri) 87 | .connectTimeout(connectTimeout) 88 | .socketTimeout(socketTimeout); 89 | if (proxy != null) { 90 | request.viaProxy(proxy); 91 | } 92 | if (email.hasAttachment()) { 93 | request.body(getMultipartEmailHttpEntity(email)); 94 | } else { 95 | request.bodyForm(convertFrom(email.getParameters()).build(), UTF_8); 96 | } 97 | return request.execute().returnContent().asString(UTF_8); 98 | } 99 | 100 | private HttpEntity getMultipartEmailHttpEntity(Email email) { 101 | MultipartEntityBuilder entityBuilder = createEntityBuilder(); 102 | 103 | Map attachments = email.attachments(); 104 | for (Map.Entry attachment : attachments.entrySet()) { 105 | entityBuilder.addBinaryBody(email.attachmentsKey(), attachment.getValue(), 106 | ContentType.MULTIPART_FORM_DATA, attachment.getKey()); 107 | } 108 | addParametersToTextBody(entityBuilder, email.getParameters()); 109 | 110 | return entityBuilder.build(); 111 | } 112 | 113 | public MultipartEntityBuilder createEntityBuilder() { 114 | return MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE).setCharset(UTF_8); 115 | } 116 | 117 | private void addParametersToTextBody(MultipartEntityBuilder entityBuilder, Map parameters) { 118 | for (Map.Entry param : parameters.entrySet()) { 119 | entityBuilder.addTextBody(param.getKey(), param.getValue(), TEXT_PLAIN); 120 | } 121 | entityBuilder.addTextBody("apiUser", sendCloud.apiUser(), TEXT_PLAIN); 122 | entityBuilder.addTextBody("apiKey", sendCloud.apiKey(), TEXT_PLAIN); 123 | } 124 | 125 | private Form convertFrom(Map parameters) { 126 | Form form = Form.form(); 127 | for (Map.Entry param : parameters.entrySet()) { 128 | form.add(param.getKey(), param.getValue()); 129 | } 130 | form.add("apiUser", sendCloud.apiUser()); 131 | form.add("apiKey", sendCloud.apiKey()); 132 | 133 | return form; 134 | } 135 | 136 | } 137 | -------------------------------------------------------------------------------- /src/main/java/io/jstack/sendcloud4j/mail/Result.java: -------------------------------------------------------------------------------- 1 | package io.jstack.sendcloud4j.mail; 2 | 3 | import org.apache.http.conn.ConnectTimeoutException; 4 | import org.json.JSONObject; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | 8 | import java.net.SocketTimeoutException; 9 | 10 | public class Result { 11 | private static Logger logger = LoggerFactory.getLogger(Result.class); 12 | 13 | private boolean success = false; 14 | private int statusCode; 15 | private String message; 16 | 17 | private String responseText; 18 | 19 | // fixme: 针对内部错误的处理方式待改进 20 | private static final int IO_EXCEPTION_CODE = 500; 21 | private static final int SOCKET_EXCEPTION_CODE = 501; 22 | private static final int CONNECT_EXCEPTION_CODE = 502; 23 | 24 | protected Result(String responseText) { 25 | this.responseText = responseText; 26 | parserResponseText(responseText); 27 | } 28 | 29 | protected static Result createExceptionResult(Email email, Throwable throwable) { 30 | logger.error("Send mail fail, request params: {}", email.getParameters(), throwable); 31 | return new Result(false, getIOErrorCode(throwable), throwable.getMessage()); 32 | } 33 | 34 | private static int getIOErrorCode(Throwable throwable) { 35 | if (throwable instanceof SocketTimeoutException) { 36 | return SOCKET_EXCEPTION_CODE; 37 | } else if (throwable instanceof ConnectTimeoutException) { 38 | return CONNECT_EXCEPTION_CODE; 39 | } 40 | return IO_EXCEPTION_CODE; 41 | } 42 | 43 | protected Result(boolean success, int statusCode, String message) { 44 | this.success = success; 45 | this.statusCode = statusCode; 46 | this.message = message; 47 | } 48 | 49 | protected void parserResponseText(String responseJson) { 50 | JSONObject jsonObject = new JSONObject(responseJson); 51 | if (jsonObject.has("result")) { 52 | this.success = jsonObject.getBoolean("result"); 53 | } 54 | if (jsonObject.has("message")) { 55 | this.message = jsonObject.getString("message"); 56 | } 57 | if (jsonObject.has("statusCode")) { 58 | this.statusCode = jsonObject.getInt("statusCode"); 59 | } 60 | } 61 | 62 | public int getStatusCode() { 63 | return statusCode; 64 | } 65 | 66 | public boolean isSuccess() { 67 | return success; 68 | } 69 | 70 | public String getMessage() { 71 | return message; 72 | } 73 | 74 | @Override 75 | public String toString() { 76 | return this.responseText; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/io/jstack/sendcloud4j/mail/Substitution.java: -------------------------------------------------------------------------------- 1 | package io.jstack.sendcloud4j.mail; 2 | 3 | 4 | import java.util.ArrayList; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | /** 10 | * 模块变量定义。 11 | * 12 | * @author denger 13 | */ 14 | public class Substitution { 15 | 16 | public static Sub sub() { 17 | return new Sub(); 18 | } 19 | 20 | public static SubGroup subGroup() { 21 | return new SubGroup(); 22 | } 23 | 24 | public static class Sub { 25 | 26 | private Map sub = new HashMap(); 27 | 28 | public Sub set(String name, String value) { 29 | String subName = "%" + name + "%"; 30 | sub.put(subName, value); 31 | return this; 32 | } 33 | 34 | public String get(String name) { 35 | return sub.get("%" + name + "%"); 36 | } 37 | 38 | protected Map get() { 39 | return sub; 40 | } 41 | 42 | private List getList(String value) { 43 | List vars = new ArrayList(1); 44 | 45 | vars.add(value); 46 | return vars; 47 | } 48 | } 49 | 50 | public static class SubGroup { 51 | private Map> subsMap = new HashMap>(); 52 | 53 | private List subs = new ArrayList(); 54 | 55 | protected List getSubs() { 56 | return subs; 57 | } 58 | 59 | public SubGroup add(Sub sub) { 60 | subs.add(sub); 61 | 62 | Map vars = sub.get(); 63 | for (Map.Entry var : vars.entrySet()) { 64 | String subName = var.getKey(); 65 | 66 | List values = subsMap.get(subName); 67 | if (values == null) { 68 | values = new ArrayList(); 69 | } 70 | values.add(var.getValue()); 71 | 72 | subsMap.put(subName, values); 73 | } 74 | return this; 75 | } 76 | 77 | public Map> toSubsMap() { 78 | return this.subsMap; 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/io/jstack/sendcloud4j/mail/TemplateEmail.java: -------------------------------------------------------------------------------- 1 | package io.jstack.sendcloud4j.mail; 2 | 3 | 4 | import org.json.JSONObject; 5 | 6 | import java.util.ArrayList; 7 | import java.util.Arrays; 8 | import java.util.List; 9 | 10 | public class TemplateEmail extends Email { 11 | 12 | private boolean useAddressList = false; 13 | 14 | private Substitution.SubGroup subGroup; 15 | 16 | public TemplateEmail(String template) { 17 | addParameter("templateInvokeName", template); 18 | } 19 | 20 | public TemplateEmail substitutionVars(Substitution.Sub sub) { 21 | this.subGroup = Substitution.subGroup().add(sub); 22 | return getThis(); 23 | } 24 | 25 | public TemplateEmail substitutionVars(Substitution.SubGroup subGroup) { 26 | this.subGroup = subGroup; 27 | return getThis(); 28 | } 29 | 30 | public TemplateEmail useAddressList() { 31 | useAddressList = true; 32 | return addParameter("useAddressList", String.valueOf(useAddressList)); 33 | } 34 | 35 | @Override 36 | protected TemplateEmail getThis() { 37 | return this; 38 | } 39 | 40 | @Override 41 | protected void rewriteParameters() { 42 | JSONObject ret = new JSONObject(); 43 | if (subGroup == null) { 44 | subGroup = Substitution.subGroup(); 45 | } 46 | ret.put("sub", subGroup.toSubsMap()); 47 | List toAddrs = new ArrayList(); 48 | 49 | if (!useAddressList) { 50 | toAddrs.addAll(Arrays.asList(to())); 51 | // 去除 to 参数 52 | to(""); 53 | ret.put("to", toAddrs); 54 | addParameter("xsmtpapi", ret.toString()); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/io/jstack/sendcloud4j/util/IOUtils.java: -------------------------------------------------------------------------------- 1 | package io.jstack.sendcloud4j.util; 2 | 3 | 4 | import java.io.File; 5 | import java.io.FileInputStream; 6 | import java.io.IOException; 7 | import java.io.InputStream; 8 | 9 | public class IOUtils { 10 | 11 | public static byte[] read(File file) throws IOException { 12 | if (!file.isFile() || !file.exists()) { 13 | throw new IOException(file.getAbsolutePath() + " is not file or not exists!"); 14 | } 15 | 16 | byte[] buffer = new byte[(int) file.length()]; 17 | InputStream ios = null; 18 | try { 19 | ios = new FileInputStream(file); 20 | if (ios.read(buffer) == -1) { 21 | throw new IOException( 22 | "EOF reached while trying to read the whole file"); 23 | } 24 | } finally { 25 | try { 26 | if (ios != null) 27 | ios.close(); 28 | } catch (IOException e) { 29 | } 30 | } 31 | return buffer; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/io/jstack/sendcloud4j/mail/EmailTest.java: -------------------------------------------------------------------------------- 1 | package io.jstack.sendcloud4j.mail; 2 | 3 | 4 | import org.junit.Test; 5 | 6 | import java.io.File; 7 | import java.util.Map; 8 | 9 | import static org.junit.Assert.*; 10 | 11 | public class EmailTest { 12 | 13 | private Email email = new MockTestEmail(); 14 | 15 | @Test 16 | public void testAddAndGetParameter() { 17 | String from = "jack@jstack.io"; 18 | email.addParameter("from", from); 19 | 20 | assertEquals(email.getParameter("from"), from); 21 | assertEquals(email.getParameters().size(), 1); 22 | assertEquals(email.getParameters().get("from"), from); 23 | } 24 | 25 | @Test 26 | public void testMultipleRecipients() { 27 | email.to("denger@jstack.io"); 28 | email.to("jack@jstack.io"); 29 | email.to("andy@jstack.io"); 30 | email.to("support@jstack.io"); 31 | email.to("rak@jstack.io;hello@jstack.io"); 32 | 33 | assertEquals(email.to().length, 6); 34 | } 35 | 36 | @Test 37 | public void testRemoveParameterIfSetValueEmptyOrNull() { 38 | email.from("denger@stack.io"); 39 | email.from(null); 40 | assertNull(email.getParameter("from")); 41 | 42 | email.to("denger@jstack.io"); 43 | email.to(""); 44 | assertEquals(email.to().length, 0); 45 | } 46 | 47 | @Test 48 | public void testRewriteParameterWhenGetParameters() { 49 | email.addParameter("test", "hello"); 50 | assertEquals(email.getParameters().get("test"), "hello world"); 51 | } 52 | 53 | @Test 54 | public void testAddFileAttachments() { 55 | email.attachments(new File[]{new File("src/test/resouces/3byte_test.txt")}); 56 | Map attachments = email.attachments(); 57 | 58 | assertTrue(email.hasAttachment()); 59 | assertEquals(attachments.size(), 1); 60 | assertTrue(attachments.containsKey("3byte_test.txt")); 61 | assertEquals(attachments.get("3byte_test.txt").length, 3); 62 | } 63 | 64 | @Test 65 | public void testAddBytesAttachments() { 66 | email.attachment(new byte[]{1,1}, "filename"); 67 | Map attachments = email.attachments(); 68 | 69 | assertTrue(email.hasAttachment()); 70 | assertEquals(attachments.size(), 1); 71 | assertEquals(attachments.get("filename").length, 2); 72 | } 73 | 74 | class MockTestEmail extends Email { 75 | 76 | @Override 77 | protected MockTestEmail getThis() { 78 | return this; 79 | } 80 | 81 | @Override 82 | protected void rewriteParameters() { 83 | if (getParameter("test") != null) { 84 | addParameter("test", getParameter("test") + " world"); 85 | } 86 | } 87 | } 88 | } 89 | 90 | -------------------------------------------------------------------------------- /src/test/java/io/jstack/sendcloud4j/mail/GeneralEmailTest.java: -------------------------------------------------------------------------------- 1 | package io.jstack.sendcloud4j.mail; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.assertEquals; 6 | 7 | public class GeneralEmailTest { 8 | 9 | @Test 10 | public void testAddBccParameter() { 11 | GeneralEmail generalEmail = new GeneralEmail(); 12 | 13 | generalEmail.bcc("jstack@jstack.io"); 14 | generalEmail.bcc("denger@jstack.io;tc@jstack.io"); 15 | 16 | assertEquals(generalEmail.getParameter("bcc"), "jstack@jstack.io;denger@jstack.io;tc@jstack.io"); 17 | } 18 | 19 | @Test 20 | public void testAddCcParameter() { 21 | GeneralEmail generalEmail = new GeneralEmail(); 22 | 23 | generalEmail.cc("jstack@jstack.io"); 24 | generalEmail.cc("denger@jstack.io;tc@jstack.io"); 25 | 26 | assertEquals(generalEmail.getParameter("cc"), "jstack@jstack.io;denger@jstack.io;tc@jstack.io"); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/io/jstack/sendcloud4j/mail/MailWebApiTest.java: -------------------------------------------------------------------------------- 1 | package io.jstack.sendcloud4j.mail; 2 | 3 | import io.jstack.sendcloud4j.SendCloud; 4 | import org.junit.Test; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public class MailWebApiTest { 9 | 10 | private String apiUser = "testApiUser"; 11 | private String apiKey = "testApiKey"; 12 | 13 | SendCloud sendCloud = SendCloud.createWebApi(apiUser, apiKey); 14 | 15 | @Test 16 | public void testGetSendTemplateAPIURI() { 17 | String uri = sendCloud.mail().getSendAPIURI(new TemplateEmail("tmmp")); 18 | assertEquals(uri, SendCloud.API_DOMAIN + "/apiv2/mail/sendtemplate"); 19 | } 20 | 21 | @Test 22 | public void testGetSendAPIURI() { 23 | String uri = sendCloud.mail().getSendAPIURI(new GeneralEmail()); 24 | assertEquals(uri, SendCloud.API_DOMAIN + "/apiv2/mail/send"); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/io/jstack/sendcloud4j/mail/ResultTest.java: -------------------------------------------------------------------------------- 1 | package io.jstack.sendcloud4j.mail; 2 | 3 | import org.apache.http.conn.ConnectTimeoutException; 4 | import org.junit.Test; 5 | 6 | import java.net.SocketTimeoutException; 7 | 8 | import static org.junit.Assert.*; 9 | 10 | public class ResultTest { 11 | private static String SUCCESS_MSG = "{\"result\":true,\"statusCode\":200,\"message\":\"请求成功\",\"info\":{\"emailIdList\":[\"xxxxx@s\"]}}"; 12 | 13 | private static String BLANK_ERROR_MSG = "{\"result\":false,\"statusCode\":40863,\"message\":\"to中有不存在的地址列表. \",\"info\":{\"emailIdList\":[\"xxxxx@s\"]}}"; 14 | 15 | private static String BLANK_MSG = "{}"; 16 | 17 | @Test 18 | public void testFromSuccess() { 19 | Result result = new Result(SUCCESS_MSG); 20 | 21 | assertTrue(result.isSuccess()); 22 | assertEquals(result.getStatusCode(), 200); 23 | assertEquals(result.getMessage(), "请求成功"); 24 | } 25 | 26 | @Test 27 | public void testFromFail() { 28 | Result result = new Result(BLANK_ERROR_MSG); 29 | assertFalse(result.isSuccess()); 30 | assertEquals(result.getStatusCode(), 40863); 31 | assertEquals(result.getMessage(), "to中有不存在的地址列表. "); 32 | } 33 | 34 | @Test 35 | public void testFromException() { 36 | Result result = Result.createExceptionResult(new GeneralEmail(), new NullPointerException("Nullexception")); 37 | 38 | assertFalse(result.isSuccess()); 39 | assertEquals(result.getStatusCode(), 500); 40 | assertEquals(result.getMessage(), "Nullexception"); 41 | } 42 | 43 | @Test 44 | public void testIOException() { 45 | Result result = Result.createExceptionResult(new GeneralEmail(), new SocketTimeoutException()); 46 | 47 | assertFalse(result.isSuccess()); 48 | assertEquals(result.getStatusCode(), 501); 49 | 50 | result = Result.createExceptionResult(new GeneralEmail(), new ConnectTimeoutException()); 51 | 52 | assertFalse(result.isSuccess()); 53 | assertEquals(result.getStatusCode(), 502); 54 | } 55 | 56 | @Test 57 | public void testFromBlankJson() { 58 | Result result = new Result(BLANK_MSG); 59 | assertFalse(result.isSuccess()); 60 | assertEquals(result.getStatusCode(), 0); 61 | assertEquals(result.getMessage(), null); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/test/java/io/jstack/sendcloud4j/mail/SubstitutionTest.java: -------------------------------------------------------------------------------- 1 | package io.jstack.sendcloud4j.mail; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.assertEquals; 6 | 7 | public class SubstitutionTest { 8 | 9 | String varName = "name"; 10 | String varAge = "age"; 11 | 12 | @Test 13 | public void testSingleSub() { 14 | Substitution.Sub sub = Substitution.sub() 15 | .set(varName, "jack") 16 | .set(varAge, "19"); 17 | 18 | assertEquals(sub.get(varName), "jack"); 19 | assertEquals(sub.get(varAge), "19"); 20 | } 21 | 22 | @Test 23 | public void testSubGroups() { 24 | Substitution.Sub subJack = Substitution.sub() 25 | .set(varName, "jack") 26 | .set(varAge, "18"); 27 | Substitution.Sub subDenger = Substitution.sub() 28 | .set(varName, "denger") 29 | .set(varAge, "25"); 30 | 31 | Substitution.SubGroup subGroups = Substitution.subGroup() 32 | .add(subJack) 33 | .add(subDenger); 34 | 35 | assertEquals(subGroups.toSubsMap() 36 | .get("%" + varName + "%") 37 | .get(0), subJack.get(varName)); 38 | assertEquals(subGroups.toSubsMap() 39 | .get("%" + varName + "%") 40 | .get(1), subDenger.get(varName)); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/io/jstack/sendcloud4j/mail/TemplateEmailTest.java: -------------------------------------------------------------------------------- 1 | package io.jstack.sendcloud4j.mail; 2 | 3 | 4 | import org.junit.Test; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | 8 | import java.util.Map; 9 | 10 | import static org.junit.Assert.assertEquals; 11 | import static org.junit.Assert.assertTrue; 12 | 13 | public class TemplateEmailTest { 14 | Logger logger = LoggerFactory.getLogger(TemplateEmailTest.class); 15 | 16 | private static final String NOT_USE_ADD_XSMTPAPI = "\"to\":[\"denger.it@gmail.com\"]"; 17 | 18 | @Test 19 | public void testRewriteParametersWhenNotUseAddressList() { 20 | TemplateEmail templateEmail = new TemplateEmail("test_tmp") 21 | .to("denger.it@gmail.com") 22 | .substitutionVars(Substitution.sub().set("name", "Jack")); 23 | 24 | Map params = templateEmail.getParameters(); 25 | logger.info("Not use address list: {}", params); 26 | 27 | assertEquals(params.get("to"), null); 28 | assertTrue(params.get("xsmtpapi").contains(NOT_USE_ADD_XSMTPAPI)); 29 | } 30 | 31 | @Test 32 | public void testRewriteParametersWhenUseAddressList() { 33 | TemplateEmail templateEmail = new TemplateEmail("test_tmp") 34 | .to("denger.it@gmail.com") 35 | .useAddressList() 36 | .substitutionVars(Substitution.sub().set("name", "Jack")); 37 | 38 | Map params = templateEmail.getParameters(); 39 | logger.info("used address list: {}", params); 40 | 41 | assertEquals(params.get("to"), "denger.it@gmail.com"); 42 | assertEquals(params.get("xsmtpapi"), null); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/io/jstack/sendcloud4j/util/IOUtilsTest.java: -------------------------------------------------------------------------------- 1 | package io.jstack.sendcloud4j.util; 2 | 3 | 4 | import org.junit.Test; 5 | 6 | import java.io.File; 7 | import java.io.IOException; 8 | 9 | import static org.junit.Assert.*; 10 | 11 | public class IOUtilsTest { 12 | 13 | @Test 14 | public void testReadExistsFile() { 15 | byte[] bt = null; 16 | try { 17 | bt = IOUtils.read(new File("src/test/resouces/3byte_test.txt")); 18 | } catch (IOException ioe) { 19 | fail(ioe.getMessage()); 20 | } 21 | assertNotNull(bt); 22 | assertEquals(bt.length, 3); 23 | } 24 | 25 | @Test(expected = IOException.class) 26 | public void testReadNotExistsFileThrowException() throws IOException { 27 | IOUtils.read(new File("src/test/resouces/not_exists_file.txt")); 28 | } 29 | 30 | @Test(expected = IOException.class) 31 | public void testReadEmptyFileThrowException() throws IOException { 32 | IOUtils.read(new File("src/test/resouces/empty.txt")); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/resouces/3byte_test.txt: -------------------------------------------------------------------------------- 1 | abc -------------------------------------------------------------------------------- /src/test/resouces/empty_test.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/denger/sendcloud4j/4f09383bb2f941d29c1222af217c824926097e4d/src/test/resouces/empty_test.txt -------------------------------------------------------------------------------- /src/test/resouces/log4j.properties: -------------------------------------------------------------------------------- 1 | # Root logger option 2 | log4j.rootLogger=DEBUG, stdout 3 | 4 | # Direct log messages to stdout 5 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 6 | log4j.appender.stdout.Target=System.out 7 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 8 | log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n 9 | 10 | --------------------------------------------------------------------------------