How to embed a link in email message for file attached in email using JavaMail API?

StackOverflow https://stackoverflow.com//questions/24036058

  •  21-12-2019
  •  | 
  •  

Question

I want to embed a link in email for file already attached in email message using JavaMail API.

For example, I am sending an email with some attachments. Now I want to embed link for all files which are available in email message.

Could you please help me on this?

I am using below code to attach a file in email message:

MimeBodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachFile);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(attachFileName);
Was it helpful?

Solution

Setup attachment as following code:

MimeBodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachFile);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setHeader("Content-ID","file");
messageBodyPart.setFileName(attachFileName);

In the above code Content-ID is defined as file which can used as reference in href of anchor tag as:

<a href='cid:html'>link text</a>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top