通过 GMailSender 发送带有附件的电子邮件?

发布于 2024-11-07 07:05:35 字数 2378 浏览 3 评论 0原文

我一直在阅读博客并尝试了多种实现,但仍然无法将图像附加到我使用 java 通过 GMail 发送的电子邮件中。我下载了所有 jar 并添加了 GMailSender.java、GMailAuthenticator.java 和 JSSEProvider.java,并且我能够正常发送常规电子邮件。我尝试这样做的方式如下所示,中间被注释掉的部分是我希望添加图像的部分。下面是我尝试执行此命令时 logcat 上的输出。当然我错过了一些非常简单的东西。有人可以向我指出吗?提前致谢。

    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)  

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)  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦魇绽荼蘼 2024-11-14 07:05:35

我修改了该函数以接受文件参数并将其附加到电子邮件中,如下所示

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

    }
}

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

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文