我正在尝试按照此处找到的教程进行操作: Sending Email in Android using JavaMail API without using the default/built-in app
但是我遇到以下错误:
Cannot resolve symbol MimeMessage
Cannot resolve symbol DataHandler
Cannot resolve symbol InternetAddress
Cannot resolve symbol setSender
Cannot resolve symbol SetDataHandler
Cannot resolve symbol Transport
然后在ByteArrayDataSource
方法上,我收到一个错误,说明如何必须将其声明为抽象或实现抽象方法getConnection()
。
这是针对GMailSender.java
文件的。我将所有外部jar添加到项目中,并将它们添加到库中。我想知道是否其他人可以构建该项目并告诉我他们是否遇到相同的错误,以及需要导入哪些库来解决它们。
谢谢
生成文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.homework16v2"
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation group: 'javax.mail', name: 'javax.mail-api', version: '1.6.2'
implementation group: 'com.sun.mail', name: 'javax.mail', version: '1.6.2'
}
GMailSender错误:
public class GMailSender extends javax.mail.Authenticator {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
static {
Security.addProvider(new com.example.homework16v2.JSSEProvider());
}
public GMailSender(String user, String password) {
this.user = user;
this.password = password;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
//cannot getDefaultInstace
session = Session.getDefaultInstance(props, this);
}
//getPassWordAuthentication clashes with getPasswordAuthentication in javax.mail.Authenticator; attempting to use incompatible return type
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
try{
//cannot resolve constructor MimeMessage(android.se.omapi.Session)
MimeMessage message = new MimeMessage(session);
//cannot resolve constructor DataHandler()
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
//Cannot resolve symbol RecipientType
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
//Cannot resolve symbol RecipientType
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}catch(Exception e){
}
}
//ByteArrayDataSource must either be declared abstract or implement getConnection() in 'DataSource'
public class ByteArrayDataSource implements DataSource {
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type) {
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data) {
super();
this.data = data;
}
public void setType(String type) {
this.type = type;
}
public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
public String getName() {
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not Supported");
}
}
}