Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

android - Sending email with attachment through GMailSender?

I have been reading the blogs and have tried numerous implementations but have still failed to get an image attached to an email that I'm sending through GMail using java. I downloaded all the jars and added GMailSender.java, GMailAuthenticator.java, and JSSEProvider.java and I'm able to send regular e-mails just fine. The way I've tried doing it is shown below, with the middle part that's commented out being the part I had hoped would add the image. Below that is the output on the logcat when I try to execute this. Surely I am missing something pretty simple. Could someone point it out to me please? Thanks in advance.

    public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {  
        try {  
            Thread.currentThread().setContextClassLoader(getClass().getClassLoader());  
        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);  

        /*  
        // Create your new message part  
        BodyPart imgPart = new MimeBodyPart();  

        // Create a related multi-part to combine the parts  
        MimeMultipart multipart = new MimeMultipart("related");  
        multipart.addBodyPart(imgPart);

        String fileName = "http://.../sampleBarcode.png";  

        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();  
        if (classLoader == null) {  
                classLoader = this.getClass().getClassLoader();  
            if (classLoader == null) {  
                System.out.println("IT IS NULL AGAIN!!!!");  
            }  
        }  

        DataSource ds = new URLDataSource(classLoader.getResource(fileName));  
        imgPart.setDataHandler(new DataHandler(ds));  
        imgPart.setHeader("Content-ID", "<logoimg_cid>");  

        multipart.addBodyPart(imgPart);  

        message.setContent(multipart);  
        */  

        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){Log.e("EMAIL_ERROR",e.getMessage(), e);}  
    }   

null  
java.lang.NullPointerException  
    at javax.activation.URLDataSource.getContentType(URLDataSource.java:91)  
    at javax.activation.DataHandler.getContentType(DataHandler.java:218)  
    ...  
    ...  
    (plus some more)  
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I modified the function to accept a File parameter and attach it to the email, here it is

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

    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);
    }catch(Exception e){

    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...