【发布时间】:2015-04-27 16:47:33
【问题描述】:
我正在尝试在后台发送带有附件的电子邮件。
我无法使用/不使用附件。
不知道我哪里出错了。 有人可以帮我解决这个问题吗? 谢谢。
错误日志:
javax.mail.NoSuchProviderException:无效协议:null javax.mail.Session.getProvider(Session.java:441) javax.mail.Session.getTransport(Session.java:660) javax.mail.Session.getTransport(Session.java:641) javax.mail.Session.getTransport(Session.java:627) com.test.www.test.MailClass.doInBackground(MailClass.java:43) com.test.www.test.DelAddress$1.run(DelAddress.java:82) java.lang.Thread.run(Thread.java:818)
代码片段:
class MailClass extends AsyncTask<String, Void, Void> {
MimeMessage email;
String delAddress, pathsList, user, password;
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart attachPart = new MimeBodyPart();
DataSource source;
Session session;
protected Void doInBackground( ArrayList<String> imagePaths, String address) throws MessagingException, UnsupportedEncodingException, EmailException {
setupEmailConfig();
deliveryAddress = address;
Log.i("doInBackground, Count:", String.valueOf(imagePaths.size()));
createEmail(imagePaths);
Log.i("doInBackground:", "Email Created Successfully.");
try {
Transport transport = session.getTransport();
transport.connect();
transport.sendMessage(email, email.getRecipients(Message.RecipientType.TO));
transport.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
}
Log.i("doInBackground:", "Email Sent.");
return null;
}
private void setupEmailConfig() {
user = "abc@gmail.com";
password = "abc";
// sets SMTP server properties
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.googlemail.com");
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.user", user);
properties.put("mail.password", password);
session = Session.getDefaultInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
});
}
private void createEmail(ArrayList<String> imagePaths) throws EmailException,
MessagingException, IOException {
String recip = "xyz@gmail.com";
email = new MimeMessage(session);
email.setFrom(new InternetAddress(user));
email.addRecipient(Message.RecipientType.TO, new InternetAddress(recip));
email.setSubject("Test Mail");
email.setSentDate(new Date());
pathsList = "";
for(int i=0; i<imagePaths.size(); i++) {
pathsList += "\r\n" + String.valueOf(i+1) + ") " + imagePaths.get(i);
// attachPart.attachFile(imagePaths.get(i));
// source = new FileDataSource(imagePaths.get(i));
// attachPart.setDataHandler(new DataHandler(source));
// attachPart.setFileName(new File(imagePaths.get(i)).getName());
// multipart.addBodyPart(attachPart);
}
BodyPart messageBody = new MimeBodyPart();
messageBody.setText("Text Body");
multipart.addBodyPart(messageBody);
email.setContent(multipart);
}
@Override
protected Void doInBackground(String... params) {
return null;
}
}
【问题讨论】:
-
你得到什么错误/输出?如果您尝试发送带附件的电子邮件与不带附件的电子邮件,该输出会有所不同吗?
-
我没有收到任何错误。有/没有附件,输出没有区别。
-
我怀疑这是您唯一的问题,但是您为什么使用
imagePaths.get(0)而不是imagePaths.get(i)?此外,在尝试期间记录您正在添加的路径并检查 logcat(针对整个设备,而不仅仅是您的应用程序)。 -
只有 imagePaths.get(i)。我做了这个改变。对这个有什么建议吗?仍然坚持这一点。谢谢。
-
有什么建议吗?
标签: java android android-studio email-attachments