0

我正在尝试在 Android 中使用 GmailSender 附加 SD 文件。

我有这个:通过 GMailSender 发送带有附件的电子邮件?

我检查了文件是否存在,但邮件无法正确发送。没有附加文件的相同方法运行正常。

可能是什么问题?

谢谢。

File imageFile = new File("/sdcard/prueba.jpg");
if (imageFile.exists()){
    Toast.makeText(getApplicationContext(),"Existe",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"NO Existe", Toast.LENGTH_LONG).show();
}
gmail.sendMail("Datos", c.getMailBody(),Constants.getAddress(), udb.getMail(), imageFile);

我的 GmailSender 类:

    public class GMailSender extends javax.mail.Authenticator{

    private final static String defaultUserName = "particle";
    private final static String defaultPassword = "particle123";

    private String mailhost = "smtp.gmail.com";   
    private String user;   
    private String password;   
    private Session session;   

    static {   
        Security.addProvider(new JSSEProvider());   
    }  

    public GMailSender() {   
        new GMailSender(defaultUserName, defaultPassword);
    }

    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);   
    }   

    protected 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){

        }
    }  

    public synchronized void sendMail(String subject, String body, String sender, String recipients, File attachment) throws Exception { 
        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(attachment!=null){
            MimeBodyPart mbp1 = new MimeBodyPart();
            mbp1.setText(body);
            MimeBodyPart mbp2 = new MimeBodyPart();
            FileDataSource fds = new FileDataSource(attachment);
            mbp2.setDataHandler(new DataHandler(fds));
            mbp2.setFileName(fds.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);
    }


    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");   
        }   
    }   
} 

我的例外是:

javax.mail.MessagingException: IOException while sending message;

嵌套异常是:javax.activation.UnsupportedDataTypeException:MIME 类型 multipart/mixed 没有对象 DCH;边界=“----=_Part_1_1079440400.1348225974758”在 com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1177) 在 javax.mail.Transport.send0(Transport.java:195) 在 javax.mail .Transport.send(Transport.java:124) 在 teru.SimDetect.TFC.GMailSender.sendMail(GMailSender.java:105) 在 teru.SimDetect.TFC.SimDetectActivity.onCreate(SimDetectActivity.java:56) 在 android.app。 Instrumentation.callActivityOnCreate(Instrumentation.java:1072) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893) 在 android.app.ActivityThread.access$1500( ActivityThread.java:135) 在 android.app。ActivityThread$H.handleMessage(ActivityThread.java:1054) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:150) 在 android.app.ActivityThread.main (ActivityThread.java:4385) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:507) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller .run(ZygoteInit.java:849) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) at dalvik.system.NativeStart.main(Native Method) 原因:javax.activation.UnsupportedDataTypeException:否MIME 类型多部分/混合的对象 DCH;边界=“----=_Part_1_1079440400.1348225974758”在 javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:905) 在 javax.activation.DataHandler.writeTo(DataHandler.java:

4

2 回答 2

0

也许这可以帮助你:

String image = "prueba.jpg";
File file = new File(Environment.getExternalStorageDirectory(), image);
GMailSender sender = new GMailSender("user@gmail.com", "password");
sender.sendMail (head , body ,"user@gmail.com", "sendto@mail.com", file);

GmailSender 修改:

public synchronized void sendMail(String subject, String body, String sender, String recipients, File attachment) throws Exception { 
    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(attachment!=null){
        MimeBodyPart mbp1 = new MimeBodyPart();
        mbp1.setText(body);
        MimeBodyPart mbp2 = new MimeBodyPart();
        FileDataSource fds = new FileDataSource(attachment);
        mbp2.setDataHandler(new DataHandler(fds));
        mbp2.setFileName(fds.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);
}   
于 2012-09-20T18:06:21.570 回答
0

代码很好,问题是 GMailSender 库,它已经过时了。

谢谢。

于 2012-09-26T17:17:33.890 回答