你好,我正在尝试'从文件夹中附加图像',这是作为附件发送,但它未能发送邮件。
如果我删除附件的multipart部分,邮件将通过。有人能帮我一下吗?
@Override
protected void onStart()
{
final String username = "abc@gmail.com";
final String password = "123456";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
message = new MimeMessage(session);
message.setFrom(new InternetAddress("abc@gmail.com"));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("xyz@outlook.com"));
message.setSubject("Photo"+ jobNo);
message.setText("Hello,"
+ "nn New job with job no" + " " + jobNo +" "+ "has arrived from Team"+ " "+ teamNo
+ "nnnn Please check the attached images."
+ "nnnnn Thank You");
message.setContent(multipart);
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(Environment.getExternalStorageDirectory() + "/CLC/"+jobNo) ;
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(jobNo);
multipart.addBodyPart(messageBodyPart);
BodyPart messageBodyPart2 = new MimeBodyPart();
messageBodyPart2.setText("Hi");
multipart.addBodyPart(messageBodyPart2);
new SendMailTask().execute(message);
Toast.makeText(getApplicationContext(), "Job Sent", Toast.LENGTH_SHORT).show();
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
super.onStart();
}
public class SendMailTask extends AsyncTask<Message, Void, Void>
{
private ProgressDialog progressDialog;
@Override
protected void onPreExecute()
{
super.onPreExecute();
progressDialog = ProgressDialog.show(Email.this, "Please wait", "Sending mail", true, false);
}
@Override
protected void onPostExecute(Void aVoid)
{
super.onPostExecute(aVoid);
progressDialog.dismiss();
}
protected Void doInBackground(javax.mail.Message... messages)
{
try
{
Transport.send(message);
} catch (MessagingException e)
{
e.printStackTrace();
}
return null;
}
}
}
您的代码看起来很好。(见http://www.jguru.com/faq/view.jsp?EID=30251)
你能试着改变MessageBodyPart和MessageBodyPart2的顺序吗?因此,首先添加消息正文,然后添加附件。