File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes Adding a css template to java mail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Adding a css template to java mail" Watch "Adding a css template to java mail" New topic
Author

Adding a css template to java mail

Prasath Premkumar
Ranch Hand

Joined: Nov 13, 2008
Posts: 43

Hi,

I'm trying to send mail from our application using java mail api. There is a .css file which i need to use for it. How can i do it???

The problems i faced are :
I tried to get the .css file through Datasource and add it to the body part with a content id and tried to call it from the html code using cid. And i found that the .css file is attached to the mail, not templated. Also inside the .css file i used cid
to access an image and it also came as an attachment. I need to know how to add a .css template to the mail???

I can send the code if needed.

Thanks in advance.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

You need it to be embedded. Check out Apache Common's Email with its HtmlEmail class. It's Javadoc page shows how to embed an image; a CSS file should work the same.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18906
    
    8

Are you expecting the mail client which displays your mail to use your CSS file? Don't be too disappointed if it doesn't do that, because I don't think it will. Do you have a reason to believe that mail clients will use CSS from the message? Have you seen messages which work that way?

(If you have, just take a look at how they are structured and then construct yours in the same way.)
Prasath Premkumar
Ranch Hand

Joined: Nov 13, 2008
Posts: 43

Thanks for the replies.

It is possible to send Html mails through java mail api, and i have seen few tutorials on embedding images too. What i'm trying to do is write a html message and in <link> tag to embed style sheet. If the email client is capable of displaying Html content, it should be able to render the <link> tag as well, isnt it?? (since it is a html component). And within the style sheet i have reference to images too.

Normally to embed images, we create a bodypart, construct a datasource and set datahandler to the bodypart with the datasource and add the bodypart to the message setting up a content id which can be used to reference the image through tag. But it doesn't work as i thought, instead of embedding the .css file , it sends it as attachment. So i want to know whether are there any possible ways to do this?? I have seen templated mails sent as notifications from few sites like American express Credit card statements, but cannot get the source of those stuff. Also the tutorials says that in this way we can embed images, but still i get them as attachments too.

Are there any other ways to send templated mails using java mail api with embedded images???

Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18906
    
    8

There's no guarantee that an e-mail client will implement all of HTML. It's just an assumption on your part that they might be able to use your CSS files to prettify the HTML. It's quite possible that they don't. Or that many of them don't.

But if you've actually seen that in action, then just look at the messages which use the feature. I don't understand what you mean by "cannot get the source". E-mail messages don't have "source". One of the example programs which comes with the JavaMail download can be used to break a message into its parts and dump them to the console.

I'm not sure what you mean by "templated" mails either. That isn't an e-mail concept that I'm aware of. You can certainly use a template to generate data for an e-mail message but the mail clients don't know that you did that and wouldn't care anyway.

Rajagopal Krishnan
Greenhorn

Joined: Aug 27, 2010
Posts: 1
Outlook allows you to receive emails with html content. I don't know what difference it makes, but if you open an html file in internet explorer (i used IE 7), then do a select all of the page, copy it, paste it into outlook and send the mail, the recipient will have colors properly displayed.

If the recipient rightclick on the email body and view source, you can see it's html and has style sheet contents.

(I used this html from "view source" to send the emails afterward and it worked )
Awinash Kumar
Greenhorn

Joined: Feb 27, 2012
Posts: 2
Its been around a week.I have been working on this java mail .even I tried a lot to use css file in my html file in every possible way.I found Two possible way of doing it.
1) you can attach the CSS file as an attachement and in HTML you can reference it using "cid:style.css".
2)you can include the whole CSS script in body section of HTML file.

In both cases,for some clients it will work fine and for some it wont.I tried to use 2nd case.HTML page got displayed fine in yahoo mail,but in gmail it didn't work.
If your CSS is not much complicated then it may work(in gmail too).but it will be headache after all.

Later I came to know that some clients like gmail avoid the attached css file .It also avoids the content between head section in HTML file. so the best thing is try to avoid using css file.use inline style.
By the way, its just matter of one html page.so you can avoid..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Awinash Kumar wrote:2)you can include the whole CSS script in body section of HTML file.

It should go in the head section, not the body.
Awinash Kumar
Greenhorn

Joined: Feb 27, 2012
Posts: 2
@ Rob Spoor :: Yeah!! My mistake......it should be "head section". By the way Thank You for pointing it out..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding a css template to java mail