I have written a task (actually copied from Internet), it sends email to given email. But when I run it, I get java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage
exception. I have included compile group: 'javax.mail', name: 'javax.mail-api', version: '1.5.1'
in dependencies but still getting this. Here is the task
apply plugin: 'com.android.application'
class MailSender extends DefaultTask {
@TaskAction
def sendMail(){
def mailParams = [
mailhost: "smtp.gmail.com",
mailport:"465",
subject: "Email Recieved",
messagemimetype: "text/plain",
user: "allaudinqazi@gmail.com",
password:"", //
enableStartTLS:"true",
ssl:"true"
]
ant.mail (mailParams) {
from (address:'allaudinqazi@gmail.com')
to (address:'allaudinqazi@gmail.com')
}
}
}
android {
compileSdkVersion 23
buildToolsVersion '23.0.1'
defaultConfig {
applicationId "uk.org.sportscotland.app"
minSdkVersion 16
targetSdkVersion 21
versionCode 3
versionName "1.1.1"
}
dexOptions {
javaMaxHeapSize "2g"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.2.0'
compile files('libs/org.apache.http.legacy.jar')
compile fileTree(dir: 'libs', include: 'Parse-1.7.0.jar')
compile 'com.koushikdutta.ion:ion:2.+'
compile group: 'javax.mail', name: 'javax.mail-api', version: '1.5.1'
}
ext {
fileName = "SSV01"
}
task copyToDropbox(type: Copy){
from "build/outputs/apk/app-debug.apk"
into "F:/folder/Dropbox/Builds/SS"
rename {
fileName + ".apk"
}
}
task mail(type: MailSender){}
Reading the docs for the mail task it says
I seem to remember that ant was a bit wierd like this and expected you to drop jar files into %ANT_HOME%/lib. So, I think the mail task will need to be loaded by the same classloader as the mail/activation jars. To me that means two options:
Add jars to ant's classloader as specified here. This approach feels very hacky to me and will likely break in Java 9 when URLClassloader is NOT guaranteed.
Use
ant.taskdef
to define another mail task (egmail2
) with all the required jars in the classpath. This would be my preferred approach.Eg:
Looks like you need to put
in your buildscript dependencies
You have incorrectly added
javax.mail
to thecompile
configuration. Since this is needed at build time you will need to add it to thebuildscript
dependencies.Eg:
More info here
It seems groupId change by the time (javax >> javax.mail): You need to replace by: