Skip to content

Commit

Permalink
Merge pull request #24 from KeenenCharles/master
Browse files Browse the repository at this point in the history
Added the ability to set email mime type
  • Loading branch information
yesidlazaro authored Jan 13, 2017
2 parents 835d668 + 891c006 commit bea54e5
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ a small library to send a email in background withou user interaction
.withUsername("username@gmail.com")
.withPassword("password12345")
.withMailto("toemail@gmail.com")
.withType(BackgroundMail.TYPE_PLAIN)
.withSubject("this is the subject")
.withBody("this is the body")
.withOnSuccessCallback(new BackgroundMail.OnSuccessCallback() {
Expand Down
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
compileSdkVersion 25
buildToolsVersion "25.0.1"

defaultConfig {
applicationId "com.creativityapps.gmailbackground"
minSdkVersion 11
targetSdkVersion 23
targetSdkVersion 25
versionCode 2
versionName "1.1"
}
Expand All @@ -21,7 +21,7 @@ android {

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:design:25.0.1'
compile project(':gmailbackgroundlibrary')
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.android.tools.build:gradle:2.2.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
8 changes: 4 additions & 4 deletions gmailbackgroundlibrary/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
compileSdkVersion 25
buildToolsVersion "25.0.1"

defaultConfig {
minSdkVersion 11
targetSdkVersion 23
targetSdkVersion 25
versionCode 2
versionName "1.1"
}
Expand All @@ -20,5 +20,5 @@ android {

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:support-annotations:23.1.1'
compile 'com.android.support:support-annotations:25.0.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class BackgroundMail {
private String mailto;
private String subject;
private String body;
private String type;
private String sendingMessage;
private String sendingMessageSuccess;
private String sendingMessageError;
Expand All @@ -36,6 +37,9 @@ public class BackgroundMail {
private OnSuccessCallback onSuccessCallback;
private OnFailCallback onFailCallback;

public final static String TYPE_PLAIN = "text/plain";
public final static String TYPE_HTML = "text/html";

public interface OnSuccessCallback {
void onSuccess();
}
Expand Down Expand Up @@ -63,6 +67,7 @@ private BackgroundMail(Builder builder) {
mailto = builder.mailto;
subject = builder.subject;
body = builder.body;
type = builder.type;
setSendingMessage(builder.sendingMessage);
setSendingMessageSuccess(builder.sendingMessageSuccess);
setSendingMessageError(builder.sendingMessageError);
Expand Down Expand Up @@ -105,6 +110,19 @@ public String getGmailPassword() {
return password;
}

public void setType(@NonNull String string) {
this.type = string;
}

public void setType(@StringRes int strRes) {
this.type = mContext.getResources().getString(strRes);
}

@NonNull
public String getType() {
return type;
}

public void showVisibleProgress(boolean state) {
this.processVisibility = state;
}
Expand Down Expand Up @@ -269,7 +287,7 @@ protected Boolean doInBackground(String... arg0) {
}
}
}
sender.sendMail(subject, body, username, mailto);
sender.sendMail(subject, body, username, mailto, type);
} catch (Exception e) {
e.printStackTrace();
return false;
Expand Down Expand Up @@ -308,6 +326,7 @@ public static final class Builder {
private String mailto;
private String subject;
private String body;
private String type = BackgroundMail.TYPE_PLAIN;
private ArrayList<String> attachments = new ArrayList<>();
private String sendingMessage;
private String sendingMessageSuccess;
Expand Down Expand Up @@ -363,6 +382,17 @@ public Builder withSubject(@StringRes int subjectRes) {
return this;
}

//set email mime type
public Builder withType(@NonNull String type) {
this.type = type;
return this;
}

public Builder withType(@StringRes int typeRes) {
this.type = context.getResources().getString(typeRes);
return this;
}

public Builder withBody(@NonNull String body) {
this.body = body;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}

public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
public synchronized void sendMail(String subject, String body, String sender, String recipients, String type) throws Exception {
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), type));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sat Nov 14 07:52:44 COT 2015
#Wed Dec 07 14:18:03 BOT 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip

0 comments on commit bea54e5

Please sign in to comment.