【发布时间】:2014-09-28 03:01:22
【问题描述】:
您好,我正在尝试“附加文件夹中的图像”,该图像将作为附件发送,但无法发送邮件。
如果我删除了附件的多部分部分,邮件就会通过。谁能帮我解决这个问题?
@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,"
+ "\n\n New job with job no" + " " + jobNo +" "+ "has arrived from Team"+ " "+ teamNo
+ "\n\n\n\n Please check the attached images."
+ "\n\n\n\n\n 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;
}
}
}
【问题讨论】:
-
您是否收到任何错误消息?
-
没有错误。它完成了它的活动
标签: android email smtp email-attachments