| 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
Saloon Keeper
Joined: Oct 27, 2005
Posts: 17259
|
|
|
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
How To Ask Questions How To Answer Questions
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 12871
|
|
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: 12871
|
|
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 )
|
 |
 |
|
|
subject: Adding a css template to java mail
|
|
|