Posted By:
Ivo_Limmen
Posted On:
Thursday, May 31, 2001 03:45 AM
The setContent() method can only be used for a bodypart. The bodypart can be filled using the setText() method or setting a data handler. Furthermore, the setText() can not be used for adding HTML to your code. As soon as you set the content-id to "text/html" it expects a certain data handler; hence the error message.
Here is a simple HTMLDataSource so you can add HTML to a bodypart...
package email;
import javax.activation.DataSource;
import java.io.*;
/**
* HTMLDataSource is a data source made to supply HTML to an email message.
*/
public class HTMLDataSource implements DataSource {
private String htmlText;
/**
* Constructor.
* Creates a HTMLDataSource with all the HTML inside.
* @param newHtmlText String.
*/
public HTMLDataSource(String newHtmlText) {
super();
this.htmlText = newHtmlText;
}
/**
* Returns the content-type for the email message. It always returns text/html.
* @return String.
*/
public String getContentType() {
return "text/html";
}
public InputStream getInputStream() throws java.io.IOException {
return new ByteArrayInputStream(this.htmlText.getBytes());
}
/**
* Returns the name of this datasource.
* @return Sting.
*/
public String getName() {
return "html";
}
public OutputStream getOutputStream() throws java.io.IOException {
throw new java.lang.UnsupportedOperationException("Method getOutputStream() is not supported.");
}
}
To use this HTMLDataSource:
...
String text = "Hi!
";
body = new MimeBodyPart();
body.setDataHandler(new DataHandler(new HTMLDataSource(text)));
...
For more information about the use of datasources and datahandlers see the
JavaMail tutorial.