Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Image to ASCII for submission via SMTP

807580Apr 16 2010 — edited Apr 16 2010
Hi all,

I have been trying to write my own mailing class for a little tool of mine.
It's a rather simple implementation but it does the trick.
Now i'm trying to add inline images (html) into the mail body.
I'm using the Base64 encoder from Apache to encode my data.

When i'm sending the email via mozilla thunderbird and the encoding of the image looks like this (first line):
/9j/4AAQSkZJRgABAQEAYABgAAD/4QBoRXhpZgAASUkqAAgAAAAEABoBBQABAAAAPgAAABsB

When i send the same email via my own class the encoding of the image looks like this (first line):
77+977+977+977+9ABBKRklGAAEBAQBgAGAAAO+/ve+/vQBoRXhpZgAASUkqAAgAAAAEABoBBQAB

If i send the same mail but convert the binary data from the image to ASCII encoding first , the encoding looks like this (first line):
Pz8/PwAQSkZJRgABAQEAYABgAAA/PwBoRXhpZgAASUkqAAgAAAAEABoBBQABAAAAPgAAABsBBQAB

The result: i don't see the image if the mail has been sent via my own library.

Here's some code.
/**
     *
     * @param data
     * @param filename
     * @param positioning
     * @return
     */
    public String addAttachment(String data, String filename, int positioning) {
        String attachment = "";
        String imgid = SMTP.generateID(this);

        if (positioning == ATTACHMENT_POSITIONING_ATTACHMENT) {
            //TODO: attachment code schrijven
        } else if (positioning == ATTACHMENT_POSITIONING_INLINE) {
            attachment = SMTP.KEYWORD_CONTENT_TYPE + ": " + SMTP.getAttachmentContentType(filename) + ";\r\n";
            attachment += " name=\"" + filename + "\"\r\n";
            attachment += SMTP.KEYWORD_CONTENT_TRANSFER_ENCODING + ": base64" + "\r\n";
            attachment += SMTP.KEYWORD_CONTENT_ID + ": <" + imgid + ">\r\n";
            attachment += SMTP.KEYWORD_CONTENT_DISPOSITION + ": " + "attachment;" + "\r\n";
            attachment += " filename=\"" + filename + "\"\r\n\r\n";
            attachment += Base64.encodeBase64String(data.getBytes());
        }

        this.message.add(attachment);
        return imgid;
    }
private void sendMailWithAttachment(String subject, String message, String data) {
        String id = "";
        EmailMessage emessage = Engine.getInstance().getMailer().prepareMessage(subject, message, SMTP.CONTENT_TYPE_MULTIPART_RELATED, SMTP.CONTENT_TYPE_TEXT_HTML);
        id = emessage.addAttachment(data, "screenshot.jpg", EmailMessage.ATTACHMENT_POSITIONING_INLINE);
        String temp = emessage.getMessage().split("</body>")[0];
        temp += "<br><img src=\"cid:" + id + "\" alt=\"\"><br>";
        temp += "</body>" + emessage.getMessage().split("</body>")[1];
        emessage.setMessage(temp, SMTP.CONTENT_TYPE_TEXT_HTML);
        Engine.getInstance().getMailer().sendMessageAsynchronous(emessage);
    }
Edited by: NightWalker007 on Apr 16, 2010 9:36 AM

Edited by: NightWalker007 on Apr 16, 2010 9:37 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 14 2010
Added on Apr 16 2010
5 comments
427 views