简体   繁体   English

没有使用Android Mail API的意图的Android邮件

[英]Android mail without intents using Android Mail API

I am using an function to attach image to an email. 我正在使用将图像附加到电子邮件的功能。

 public void addAttachment(String filename) throws Exception { 
    BodyPart messageBodyPart = new MimeBodyPart(); 
    DataSource source = new FileDataSource(filename); 
    messageBodyPart.setDataHandler(new DataHandler(source)); 
    messageBodyPart.setFileName(filename); 

    _multipart.addBodyPart(messageBodyPart); 

  } 

I call this function when attaching images to email. 将图像附加到电子邮件时,我调用此功能。 When I try to remove the attachment it is not possible. 当我尝试删除附件时,这是不可能的。

 public void removeAttachment(String filename) throws Exception { 
    BodyPart messageBodyPart = new MimeBodyPart(); 
    DataSource source = new FileDataSource(filename); 
    messageBodyPart.setDataHandler(new DataHandler(source)); 
    messageBodyPart.setFileName(filename); 

    _multipart.removeBodyPart(messageBodyPart); 

  } 

Tried with this function but not working. 尝试使用此功能,但不起作用。

 public void AddAndRemoveAttachment(String filename, Boolean yesorno)throws Exception{
      BodyPart messageBodyPart = new MimeBodyPart(); 
      DataSource source = new FileDataSource(filename); 
      if(yesorno == true ){

        source = new FileDataSource(filename); 
        messageBodyPart.setDataHandler(new DataHandler(source)); 
        messageBodyPart.setFileName(filename); 

        _multipart.addBodyPart(messageBodyPart); 
      }
      else{

          _multipart.removeBodyPart(messageBodyPart);
      }
  }

But the removeBodyPart is not removing the attached image. 但是removeBodyPart不会删除附加的图像。 Looking forward to your reply. 期待你的回复。 thanks. 谢谢。

sorry, I was a little bit conufsed the last time I posted this answer and I took you absolutely wrong! 抱歉,我上次发布此答案时有点不满意,我认为您绝对错了! it is indeed correct as you tried it to implement. 在您尝试实施时确实是正确的。 According to the link I posted in my comment, did you read the documentation for Multipart ? 根据我在评论中发布的链接,您是否阅读了Multipart的文档? it says that the removeBodyPart(BodyPart part) method throws at least a MessagingException if no such BodyPart exists. 它说,如果不存在这样的BodyPart ,则removeBodyPart(BodyPart part)方法至少会引发MessagingException have you tried to catch this Exception and write to Log ? 您是否尝试捕获此Exception并写入Log

public void removeAttachment(String filename) {
    BodyPart part = new MimeBodyPart();
    DataSource src = new FileDataSource(filename);
    part.setDataHandler(new DataHandler(src));
    part.setFileName(filename);

    try {
        _multipart.removeBodyPart(part);
    } catch(MessagingException e) {
        Log.e(TAG, "Got Exception while removing BodyPart: " + e.toString(), e)
    }
}

your second guess won't remove anything from your _multipart because your just adding the DataHandler and setting a filename if your boolean is true . 您的第二个猜测不会从_multipart删除任何内容,因为如果您的boolean值为true则只需添加DataHandler并设置文件名。 if it's false you're just removing an empty BodyPart object. 如果为false ,则只是删除一个空的BodyPart对象。

otherwise, if you want to write your own mail client with user interaction, why don't you add the files which should be attached in an array and do updates on this array? 否则,如果您想通过用户交互来编写自己的邮件客户端,为什么不添加应附加在数组中的文件并在该数组上进行更新呢? so you can add and remove files from this array and attach the files all at once when the message is going to be sent. 因此,您可以在此数组中添加和删除文件,并在发送邮件时一次附加所有文件。

I doubt that creating a new bodypart and using that as handle to call the removebodypart will work. 我怀疑创建一个新的bodypart并将其用作调用removebodypart的句柄会起作用。 Isn't there a function to retrieve the existing bodypart and then remove it? 是否没有功能来检索现有的身体部位然后将其删除?

Looks like you are using the wrong argument for it. 似乎您使用了错误的参数。

are you sure you're adding something to your MultiPart or does it throw an Exception too? 您确定要在MultiPart添加某些内容,还是也会引发Exception

I searched for javax.mail tutorials a bit and found the following: http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_(no_Intents)_in_Android 我搜索了一下javax.mail教程,发现了以下内容: http : //www.jondev.net/articles/Sending_Emails_without_User_Intervention_( no_Intents)_in_Android

the author of this tutorial mentions in his Mail constructor, that 本教程的作者在他的Mail构造函数中提到,

There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added. MailCap出了点问题,javamail找不到多部分/混合部分的处理程序,因此需要添加此位。

and adds the following: 并添加以下内容:

MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap(); 
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"); 
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"); 
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); 
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); 
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"); 
CommandMap.setDefaultCommandMap(mc); 

with this it should work as the tutorial did work "out of the box" for me... hope this helps a little bit 有了它,它应该可以正常工作,因为本教程对我来说是“开箱即用”的。希望这对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM