【问题标题】:Attachment issue in android using SMTP mail system [closed]android中使用SMTP邮件系统的附件问题[关闭]
【发布时间】: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


【解决方案1】:

您的代码看起来不错。 (见http://www.jguru.com/faq/view.jsp?EID=30251

您可以尝试更改 MessageBodyPart 和 MessageBodyPart2 的顺序吗?所以,先添加邮件正文,再添加附件。

【讨论】:

  • 嗨不得不重新排列顺序。现在可以了,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-11
  • 2011-09-07
  • 2010-09-08
  • 2015-03-16
  • 2012-05-24
  • 1970-01-01
相关资源
最近更新 更多