我设法在我的Android应用中发送了一封电子邮件;但是,我不知道如何在电子邮件中附加图像。我该怎么办?
public MailSender(String user, String password, final Context context) {
this.user = user;
this.password = password;
this.context = context;
this.bckMng = BackgroundManager.getInstance(context);
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");
session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
String userName = context.getResources().getString(R.string.source_username);
String password = context.getResources().getString(R.string.source_password);
return new PasswordAuthentication(userName, password);
}
});
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
在this.bckmng.getimages()我可以获得图像...
使用以下方法发送图像。
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("png/image");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {
"//email-id" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
Uri uri = Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "/saved_images/MyImage.png")); // Image path
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
emailIntent.setType("text/plain");
startActivity(emailIntent);
这是我现在发送消息的方式:
protected String doInBackground(String... params) {
String immediatlyMessage = params[0];
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(context.getResources().getString(R.string.source_username)));
EmailAddressDataSource eads = new EmailAddressDataSource(context);
eads.open();
List<EmailAddress> emailsList = eads.getAllEmails();
eads.close();
String emailAdress = "";
for(EmailAddress address: emailsList){
emailAdress += address.getAddress();
emailAdress+=",";
}
//message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailAdress));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("yoyoyo@yoyo.com"));
message.setSubject("Hey");
message.setContent(immediatlyMessage, "text/html; charset=utf-8");
Transport.send(message);
}
catch (MessagingException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
我在这里使用意图吗?以何种方式?
尝试一下,但它可能对模拟器不起作用,但在真实电话上起作用
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
emailIntent.setType("image/png");
ArrayList<Uri> uris = new ArrayList<Uri>();
uris.add(Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.file1));
uris.add(Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.file2));
emailIntent.putExtra(Intent.EXTRA_STREAM, uris));
startActivity(emailIntent);