Avatar of GiedriusS
GiedriusS

asked on 

Email from JSP/java

Hi,

I want to send emails from JSP. So I created a java class that I call. This is theagment that does not work:

Properties props = System.getProperties();
props.put("mail.smtp.host", "smtp.company.lt");
props.put("mail.smtp.auth", "false");
Session emlSession = Session.getInstance(props, null);
Message msg = new MimeMessage(emlSession);
msg.setFrom(new InternetAddress("my@email.lt"));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("my@email.lt", false));
msg.setSubject("subject");
msg.setText("text");
SMTPTransport t = (SMTPTransport)emlSession.getTransport("smtp");
t.connect();
 t.sendMessage(msg, msg.getAllRecipients());
 t.close();
System.out.println("\nMail was sent successfully.");

I getException:
javax.mail.MessagingException: IOException while sending message; nested exception is: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type text/plain; charset=us-ascii

Could you help me?
maybe there is another solution to sent email from JSP?

Thanks in advance.
Java

Avatar of undefined
Last Comment
GiedriusS
SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of GiedriusS
GiedriusS

ASKER

Hi,

I followed the steps in http://docs.cs.byu.edu/docs/javamail_api/:
I downloaded mail.jar, activation.jar. I added them in  ...tomcat/webapps/mail/WEB-INF/lib. I restarted tomcat, but I still have the same error.

Now my code looks like this:
Properties props = new Properties();
props.setProperty("mail.smtp.host", "smtp.company.lt");
props.setProperty("mail.subject", "Test email");
props.setProperty("mail.from", "my@email.lt");
props.setProperty("mail.to", "my@email.lt");
props.setProperty("mail.debug", "false");

javax.mail.Session emailSession = javax.mail.Session.getInstance(props);
MimeMessage msg = new MimeMessage(emailSession);
InternetAddress from = new InternetAddress(props.getProperty("mail.from"));
InternetAddress to = new InternetAddress(props.getProperty("mail.to"));
msg.setFrom(from);
msg.setRecipient(Message.RecipientType.TO, to);
msg.setSubject(props.getProperty("mail.subject"));
msg.setContent("text", "text/plain");
javax.mail.Transport.send(msg);

And the error is:
javax.mail.MessagingException: IOException while sending message; nested exception is: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type text/plain OK

Also what is mailcap? where can I appned this line?
text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain
Avatar of GiedriusS
GiedriusS

ASKER

I searched my computer for mailcap file, I found it in:
...tomcat/work/Catalina/localhost/mail/loader/META-INF

It contains the line you suggested:
#
# @(#)mailcap      1.8 05/04/20
#
# Default mailcap file for the JavaMail System.
#
# JavaMail content-handlers:
#
text/plain;;            x-java-content-handler=com.sun.mail.handlers.text_plain
text/html;;            x-java-content-handler=com.sun.mail.handlers.text_html
text/xml;;            x-java-content-handler=com.sun.mail.handlers.text_xml
multipart/*;;            x-java-content-handler=com.sun.mail.handlers.multipart_mixed; x-java-fallback-entry=true
message/rfc822;;      x-java-content-handler=com.sun.mail.handlers.message_rfc822
#
# can't support image types because java.awt.Toolkit doesn't work on servers
#
#image/gif;;            x-java-content-handler=com.sun.mail.handlers.image_gif
#image/jpeg;;            x-java-content-handler=com.sun.mail.handlers.image_jpeg
Avatar of GiedriusS
GiedriusS

ASKER

I use j2se 1.4.2_08 java.
Could there be a problem with java version?
Avatar of GiedriusS
GiedriusS

ASKER

I have the solution:

msg.setDataHandler(new DataHandler(new ByteArrayDataSource(text.toString(), "text/plain;
charset=utf-8")));
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo