无法与Android的附件发送邮件 [英] Unable to send Mail with Attachments Android

查看:282
本文介绍了无法与Android的附件发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友

我试图发送邮件形成我的申请
我收到此错误

I am trying to send mail form my application i received this error

这是我的错误:

02-07 15:28:06.852: W/System.err(1051): javax.mail.MessagingException: IOException while sending message;
02-07 15:28:06.852: W/System.err(1051):   nested exception is:
02-07 15:28:06.852: W/System.err(1051):     javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; 

02-07 15:28:06.852: W/System.err(1051):     boundary="----=_Part_0_1156236192.1328608681807"
02-07 15:28:06.882: W/System.err(1051):     at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1141)
02-07 15:28:06.882: W/System.err(1051):     at javax.mail.Transport.send0(Transport.java:195)
02-07 15:28:06.882: W/System.err(1051):     at javax.mail.Transport.send(Transport.java:124)
02-07 15:28:06.882: W/System.err(1051):     at com.controltest.activities.GMailSender.sendMail(GMailSender.java:115)
02-07 15:28:06.892: W/System.err(1051):     at com.controltest.activities.SmtActivity$1.onClick(SmtActivity.java:62)
02-07 15:28:06.892: W/System.err(1051):     at android.view.View.performClick(View.java:2408)
02-07 15:28:06.892: W/System.err(1051):     at android.view.View$PerformClick.run(View.java:8816)
02-07 15:28:06.892: W/System.err(1051):     at android.os.Handler.handleCallback(Handler.java:587)
02-07 15:28:06.892: W/System.err(1051):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-07 15:28:06.892: W/System.err(1051):     at android.os.Looper.loop(Looper.java:123)
02-07 15:28:06.892: W/System.err(1051):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-07 15:28:06.892: W/System.err(1051):     at java.lang.reflect.Method.invokeNative(Native Method)
02-07 15:28:06.892: W/System.err(1051):     at java.lang.reflect.Method.invoke(Method.java:521)
02-07 15:28:06.892: W/System.err(1051):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-07 15:28:06.892: W/System.err(1051):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-07 15:28:06.892: W/System.err(1051):     at dalvik.system.NativeStart.main(Native Method)
02-07 15:28:06.892: W/System.err(1051): Caused by: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; 

02-07 15:28:06.902: W/System.err(1051):     boundary="----=_Part_0_1156236192.1328608681807"
02-07 15:28:06.902: W/System.err(1051):     at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:905)
02-07 15:28:06.902: W/System.err(1051):     at javax.activation.DataHandler.writeTo(DataHandler.java:330)
02-07 15:28:06.902: W/System.err(1051):     at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1476)
02-07 15:28:06.902: W/System.err(1051):     at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1772)
02-07 15:28:06.902: W/System.err(1051):     at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1099)
02-07 15:28:06.902: W/System.err(1051):     ... 15 more

我的code:

senderactivity.java

senderactivity.java

try{
File file1 = new File(path);

sender.sendMail("Test File", message,"sendermailaddress", "receivermailaddress",file1);

}
catch(Exception e){
e.printStackTrace();
}

.............

.....................................

GmailSender.java

GmailSender.java

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import android.util.Log;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Provider;
import java.security.Security;
import java.util.Properties;

public class GMailSender extends javax.mail.Authenticator {
    private String mailhost = "smtp.gmail.com";
    private String user;
    private String password;
    private Session session;

    static {
        Security.addProvider(new com.controltest.activities.JSSEProvider());
    }

    public GMailSender(String user, String password) {
        this.user = user;
        this.password = password;

        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", mailhost);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.quitwait", "false");

        session = Session.getDefaultInstance(props, this);

    }

    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, password);
    }

    public synchronized void sendMail(String subject, String body,
            String sender, String recipients) throws Exception {
        try {
            MimeMessage message = new MimeMessage(session);
            DataHandler handler = new DataHandler(new ByteArrayDataSource(
                    body.getBytes(), "text/plain"));
            message.setSender(new InternetAddress(sender));
            message.setSubject(subject);
            message.setDataHandler(handler);
            if (recipients.indexOf(',') > 0)
                message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse(recipients));
            else
                message.setRecipient(Message.RecipientType.TO,
                        new InternetAddress(recipients));
            Transport.send(message);
        } catch (Exception e) {
         e.printStackTrace();
        }
    }

    public synchronized void sendMail(String subject, String body,
            String sender, String recipients, File attachment) throws Exception {
        try {
            MimeMessage message = new MimeMessage(session);
            message.setSender(new InternetAddress(sender));
            message.setSubject(subject);

            MimeBodyPart mbp1 = new MimeBodyPart();
            mbp1.setText(body);

//          FileInputStream fis = new FileInputStream(attachment);
            MimeBodyPart mbp2 = new MimeBodyPart();
            mbp2.attachFile(attachment);
//          mbp2.setHeader("Content-Type", "text/plain; charset=\"us-ascii\"; name=\"\"");
//          FileDataSource fds = new FileDataSource(attachment);
//          mbp2.setDataHandler(new DataHandler(fds));
            Log.d("File Name",""+attachment.getName());
             mbp2.setFileName(attachment.getName());

            Multipart mp = new MimeMultipart();
            mp.addBodyPart(mbp1);
            mp.addBodyPart(mbp2);

            message.setContent(mp);

            if (recipients.indexOf(',') > 0)
                message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse(recipients));
            else
                message.setRecipient(Message.RecipientType.TO,
                        new InternetAddress(recipients));
            Transport.send(message);
        } catch (Exception e) {
               e.printStackTrace();
        }
    }

    public class ByteArrayDataSource implements DataSource {
        private byte[] data;
        private String type;

        public ByteArrayDataSource(byte[] data, String type) {
            super();
            this.data = data;
            this.type = type;
        }

        public ByteArrayDataSource(byte[] data) {
            super();
            this.data = data;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getContentType() {
            if (type == null)
                return "application/octet-stream";
            else
                return type;
        }

        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(data);
        }

        public String getName() {
            return "ByteArrayDataSource";
        }

        public OutputStream getOutputStream() throws IOException {
            throw new IOException("Not Supported");
        }
    }
}

.............................. ....................................

.................................................................................

JSSEProvider.java

JSSEProvider.java

import java.security.AccessController;
import java.security.Provider;

public final class JSSEProvider extends Provider {

    public JSSEProvider() {
        super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
        AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
            public Void run() {
                put("SSLContext.TLS",
                        "org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
                put("Alg.Alias.SSLContext.TLSv1", "TLS");
                put("KeyManagerFactory.X509",
                        "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
                put("TrustManagerFactory.X509",
                        "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
                return null;
            }
        });
    }
}

...............

......................

我添加了所有jar文件:
 的mail.jar
 的activati​​on.jar
additionnal.jar

i have added all jars : mail.jar, activation.jar additionnal.jar

我想这环节的解决方案,但它不工作。
<一href=\"http://stackoverflow.com/questions/4612673/messagingexceptionioexception-while-sending-message-in-java\">MessagingExceptionIOException而在Java中发送邮件?

i tried this link's solution but its not working. MessagingExceptionIOException while sending message in java?

这仅仅是带附件不带附件的做工精细

任何帮助将AP preciate。

any help will be appreciate.

谢谢...

推荐答案

我可以发送电子邮件邮件使用以下code附件。在这里, ouputFileUri 是附件的URI。

I could send e mail with attachment using following code. Here ouputFileUri is the Uri of the attachment.

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"test mail");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"test");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(outputFileUri.toString()));
emailIntent.setType("message/rfc822");
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

这篇关于无法与Android的附件发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆