我用Idea来写Java,想发邮件去Gmail。 在网上找了一些例子, 出现问题后就直接照抄下来。 但是还是有同样的问题。
提示的错误如下 (两个):
java.lang.reflect.InvocationTargetException
java.lang.VerifyError: (class: com/sun/mail/handlers/handler_base, method: getTransferData signature: (Ljavax/activation/ActivationDataFlavor;Ljavax/activation/DataSource;)Ljava/lang/Object;) Incompatible argument to function
发送邮件的代码如下:
package seedu.address.logic;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EmailSender {
private String from;
private String to;
private String subject;
private String content;
private String password;
/**
* Create a new Email Sender
* @param from is sender email address
* @param to is receiver email address
* @param subject is the email subject
* @param content is the email content
* @param password is the password of sender email
*/
public EmailSender(String from,
String to,
String subject,
String content,
String password) {
this.from = "tic4003tp5@gmail.com";
this.to = to;
this.password = "nusyear4";
this.subject = subject;
this.content = content;
}
/**
* Send a email out
*/
public void sendEmail() {
// Create a instance for Email
Properties props = new Properties();
gmailTls(props);
// Set MimeMessage
Message msg = getMimeMessage(props);
try {
Transport.send(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Prepare email MimeMessage
* @param props email server properties
* @return MimeMessage
*/
private Message getMimeMessage(Properties props) {
/* Session session = Session.getDefaultInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, password);
}
});*/
Session session = Session.getInstance(props);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
try {
// Set sender email address
msg.setFrom(new InternetAddress(from));
// Set receiver email address
msg.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
// Set email subject
msg.setSubject(subject);
// Set email content
msg.setContent(content, "text/html;charset=UTF-8");
} catch (MessagingException e) {
e.printStackTrace();
}
return msg;
}
private static void gmailTls(Properties props) {
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.ssl.enable", "true");
props.setProperty("mail.smtp.starttls.enable", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.setProperty("mail.smtp.port", "587");
}
}
Build.gradle代码如下:
plugins {
id 'java'
id 'checkstyle'
id 'com.github.johnrengelman.shadow' version '4.0.4'
id 'application'
id 'jacoco'
}
mainClassName = 'seedu.address.Main'
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}
checkstyle {
toolVersion = '8.29'
}
test {
useJUnitPlatform()
finalizedBy jacocoTestReport
}
task coverage(type: JacocoReport) {
sourceDirectories.from files(sourceSets.main.allSource.srcDirs)
classDirectories.from files(sourceSets.main.output)
executionData.from files(jacocoTestReport.executionData)
afterEvaluate {
classDirectories.from files(classDirectories.files.collect {
fileTree(dir: it, exclude: ['**/*.jar'])
})
}
reports {
html.enabled = true
xml.enabled = true
}
}
dependencies {
String jUnitVersion = '5.4.0'
String javaFxVersion = '11'
implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'win'
implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'mac'
implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'linux'
implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'win'
implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'mac'
implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'linux'
implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'win'
implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'mac'
implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'linux'
implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'win'
implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'mac'
implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'linux'
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.7.0'
implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.7.4'
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: jUnitVersion
testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: jUnitVersion
compile 'com.sun.mail:android-mail:1.5.5'
compile 'com.sun.mail:android-activation:1.5.5'
compile 'javax.mail:javax.mail-api:1.6.2'
compile 'com.sun.mail:javax.mail:1.6.2'
testCompile 'junit:junit:4.12'
}
shadowJar {
archiveBaseName = 'teamcontact24-7-v1.4'
}
defaultTasks 'clean', 'test'
run {
enableAssertions = true
}