问题
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){}
回答1:
You have incorrectly added javax.mail to the compile configuration. Since this is needed at build time you will need to add it to the buildscript dependencies.
Eg:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'javax.mail:javax.mail-api:1.5.1'
}
}
More info here
回答2:
Reading the docs for the mail task it says
Note: This task may depend on external libraries that are not included in the Ant distribution. See Library Dependencies for more information.
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.taskdefto define another mail task (egmail2) with all the required jars in the classpath. This would be my preferred approach.
Eg:
configurations { antMail }
dependencies {
antMail 'ant:ant-javamail:1.6.5'
antMail 'javax.activation:activation:1.1.1'
antMail 'javax.mail:mail:1.4.7'
}
ant.taskdef(
name: 'mail2',
classname: 'org.apache.tools.ant.taskdefs.email.EmailTask',
classpath: configurations.antMail.asPath
)
ant.mail2(mailParams) { ... }
回答3:
It seems groupId change by the time (javax >> javax.mail): You need to replace by:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'javax.mail:javax.mail-api:1.5.6'
}
}
回答4:
Looks like you need to put
classpath 'javax.mail:javax.mail-api:1.5.5'
in your buildscript dependencies
来源:https://stackoverflow.com/questions/37293194/sending-email-using-gradle