我正在使用此代码(如下所示)通过我的Android应用程序的电子邮件发送图像,收到电子邮件但它没有图像。请告诉我的代码中有什么问题?
收到邮件: 电子邮件正文
代码:
package com.example.appdeveloper.appname;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import java.io.File;
import java.util.Properties;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EmailHandler extends AsyncTask<Void, Void, Boolean> {
private static String to = "reciever@example.com";
private static String from = "sender@example.com";
private static String subject = "Subject";
private static String sender = "Android App";
private static String mail;
private static String username = "sender";
private static String password = "password";
EmailHandler(Context context) {
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/test.png";
mail ="<!DOCTYPE html><html><body><img src="+path+"></body></html>";
}
@Override
protected Boolean doInBackground(Void... nothing) {
try {
Authenticator auth = new EmailAutherticator();
Properties properties = new Properties();
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.starttls.enable", "true");
properties.setProperty("mail.smtp.host", "smtp.gmail.com");
properties.setProperty("mail.smtp.port", "587");
properties.setProperty("mail.smtp.user", username);
properties.setProperty("mail.smtp.password", password);
Session session = Session.getDefaultInstance(properties,auth);
MimeMessage message = new MimeMessage(session);
message.setSubject(subject);
message.setContent(mail, "text/html; charset=utf-8");
Address address = new InternetAddress(from,sender);
message.setFrom(address);
InternetAddress ad[] = new InternetAddress[2];
ad[0] = new InternetAddress(to);
ad[1] = new InternetAddress(from);
message.addRecipients( Message.RecipientType.TO, ad );
Transport.send(message);
return true;
}
catch(Exception exp) {
exp.printStackTrace();
return false;
}
}
}
class EmailAutherticator extends Authenticator {
private String username = "sender";
private String password = "password";
public EmailAutherticator() {
super();
}
public EmailAutherticator(String user,String pwd){
super();
username = user;
password = pwd;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,password);
}
}
答案 0 :(得分:0)
所以你的电子邮件就像这样(有点):
<!DOCTYPE html><html><body><img src="\myPhone\DCIM\image.png"></body></html>
除非您在手机上阅读电子邮件,否则您的电子邮件无法到达该位置。
编辑1
您可以使用Android文档here中所述的Intent.EXTRA_STREAM
为您的意图附加额外数据