Getting java.lang.VerifyError while sending mail using javax.mail - java

I am trying to send a mail with attachment using javax.mail functionality, below is my code:
// create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
// fill message
messageBodyPart.setText(strmessage);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
AdcsipLogger.adcsipLog("begin exportFile...........");
ExportFile exportFile = buildQuestionnaireReport(questionnaire);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new ExportFileDataSource(exportFile);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(questionnaire.getName() + ".pdf");
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
// Send the message
Transport.send(message);
I am getting an exception at line:
messageBodyPart.setDataHandler(new DataHandler(source));
Below is the extract from stacktrace:
[10/30/13 14:44:38:089 CET] 00000062 ServletWrappe E com.ibm.ws.webcontainer.servlet.ServletWrapper service SRVE0068E: Uncaught exception created in one of the service methods of the servlet QuestionnaireControl in application A430-CADCSIP-V8.0_Run4. Exception created : java.lang.VerifyError: javax/mail/internet/MimeBodyPart.setDataHandler(Ljavax/activation/DataHandler;)V
Please help me.
Thanks.

You're running in WebSphere, right?
Do you have the JavaMail jar file included in your application? If so, it may be conflicting with the version included in WebSphere.

Related

Send Email with image attachment using JBoss server from eclipse

I am trying to send email with image attachment from my JBOSS server. I am using eclipse Ide. Where should I put my image resource that is to be sent at email attachment.
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<H1>Hello</H1><img src=\"cid:image\">";
messageBodyPart.setContent(htmlText, "text/html");
// add it
multipart.addBodyPart(messageBodyPart);
// second part (the image)
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource("Logo.png");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<image>");
// add image to the multipart
multipart.addBodyPart(messageBodyPart);
// put everything together
msg.setContent(multipart);
// Send message
Transport.send(msg);
In the above code I am providing Logo.png, where should I put that file in the folder hierarchy.
The folder structure is:
project-backend
-DeployedResources
-src
--main
---java
----com
-----abc
------utils
-------EmailUtil.java -> File with the email sending logic
---resources
----META-INF
-----persistence.xml
---webapp
----WEB_INF
-----beans.xml
-----web.xml
I tried placing that file at multiple locations by everytime I am get the error in line:
// Send message
Transport.send(msg);
The error is:
java.lang.NullPointerException
javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:226)
javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:299)
javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1375)
javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1021)
javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:419)
javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1354)
javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2107)
javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2075)
com.abc.utils.EmailUtil.sendEmail(EmailUtil.java:69)
javax.mail.Transport.send(Transport.java:123)
I was able to solve the problem by placing the resources in the bin folder of jboss server.

javax.mail.Transport.send() causing a "java.awt.Component is a restricted class." when attaching a jpg

Here is the exception I get sometimes when trying to send mail with attachments on Appengine :
java.lang.NoClassDefFoundError: java.awt.Component is a restricted class. Please see the Google App Engine developer's guide for more details.
at com.google.apphosting.runtime.security.shared.stub.java.awt.Component.<clinit>(Component.java)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at java.lang.Class.newInstance(Class.java:375)
at javax.activation.MailcapCommandMap.getDataContentHandler(MailcapCommandMap.java:588)
at javax.activation.MailcapCommandMap.createDataContentHandler(MailcapCommandMap.java:542)
at javax.activation.DataHandler.getDataContentHandler(DataHandler.java:617)
at javax.activation.DataHandler.getInputStream(DataHandler.java:240)
at javax.activation.DataHandlerDataSource.getInputStream(DataHandler.java:708)
at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:764)
at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:718)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:548)
at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:133)
at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:1393)
at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:1366)
at javax.mail.Transport.send(Transport.java:76)
at javax.mail.Transport.send(Transport.java:48)
I use this code to send a mail :
Properties props = new Properties();
Session mailSession = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(mailSession);
MimeMultipart multipart = new MimeMultipart();
// first part (the html)
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(htmlText, "text/html");
multipart.addBodyPart(htmlPart);
// second part (the image)
BodyPart attachmentPart = new MimeBodyPart();
attachmentPart.setFileName("screenshot.jpg");
DataSource imageSrc = new ByteArrayDataSource(image, "image/jpeg");
attachmentPart.setDataHandler(new DataHandler(imageSrc));
multipart.addBodyPart(attachmentPart);
message.setContent(multipart);
message.saveChanges();
Transport.send(message);
When this exception occurs on an instance, it becomes unstable and is no longer able to send emails with attachment. The only solution I found is to kill the instance and start a new one.
Is "image" really a byte array?
The stack trace suggests that you've set the attachment as an Image object, so JAF is trying to find a DataContentHandler to convert it to a byte array, and that DataContentHandler is using an AWT class that's not allowed. There's no such DataCOntentHandler configured by default so I don't know where it's coming from. Maybe GAE has a different configuration?
Also, what version of JavaMail are you using?

Include Image in an email without attachments using java

I want to add an image to an email along with some text without having to attach the image in the email. Would this be possible ?
I have managed to do the following so far but it is sent with an attachment and also the image in the mail. I want it without an attachment but as a part of the mail
`Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(emailID));
message.setSubject("Password Reset");
Multipart multipart = new MimeMultipart("related");
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent("<html><body>Hi<img src=\"cid:the-img-1\"/></body></html>", "text/html");
multipart.addBodyPart(htmlPart);
BodyPart imgPart=new MimeBodyPart();
// Loading the image
DataSource ds=new FileDataSource("C:\\Users\\XYZ\\Desktop\\images.jpg");
imgPart.setDataHandler(new DataHandler(ds));
//Setting the header
imgPart.setHeader("Content-ID","the-img-1");
multipart.addBodyPart(imgPart);
// attaching the multi-part to the message
message.setContent(multipart);
Transport.send(message);`
You need to construct a multipart/related message.
You will be need to construct an html based e-mail for that purpose.
There are plenty of examples that you can get on google for that.
One example is look at here.
Note that image you want to send should be accessible over http to recipient. May be you can upload it on google drive or some image server

Exception while sending an email in Java with javax.mail

I'm trying to do an Android app that sends an image automatically without any intervention from the user.
I'm using javax.mail jar (and the activation.jar and additional.jar) in order to do so.
I'm doing the following
Properties props = new Properties();
props.put("mail.smtp.auth", true);
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.ssl.enable",true);
props.put("mail.smtp.port", "465");
Session session = Session.getInstance(props,new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USER,PASS);
}
});
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("*******#gmail.com"));
message.setSubject("Email Subject");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("******#gmail.com"));
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent("<img src=\"image.gif\"/>","text/html");
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
FileDataSource source = new FileDataSource(new File(ruta));
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("picture.gif");
messageBodyPart.setDisposition("inline");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
When I execute this code, I get the following error
W/System.err(24907): javax.mail.MessagingException: IOException while sending message;
W/System.err(24907): nested exception is:
W/System.err(24907): javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed;
W/System.err(24907): boundary="----=_Part_0_1095625208.1397499270313"
W/System.err(24907): at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1182)
W/System.err(24907): at javax.mail.Transport.send0(Transport.java:254)
W/System.err(24907): at javax.mail.Transport.send(Transport.java:124)
Any idea on how can I solve this?
I'm using a Debian 64-bits computer and Eclipse Java EE IDE for Web Developers - Kepler. if it's needed for the solution...
DCH is Data Content Handler. MimeType handlers seem to be not configured correctly.
If you can download and use latest 1.5.* version of java mail api, this error should be getting resolved.
Otherwise, within the code you can execute some statements to set the MimeType handlers.
Add following code snippet to your mail module and is available for the mail Session.
MailcapCommandMap mailcapCommandMap = new MailcapCommandMap();
mailcapCommandMap.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed; x-java-fallback-entry=true");
CommandMap.setDefaultCommandMap(mailcapCommandMap);
You can extend this for additional MimeTypes
mailcapCommandMap.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mailcapCommandMap.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mailcapCommandMap.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mailcapCommandMap.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
Refer To:
javax.activation.MailcapCommandMap.addMailcap(java.lang.String)
Add entries to the registry. Programmatically added entries are
searched before other entries.
Apart from this method, read entire documentation on this class.
I had this same problem for my release mode android app, it works as expected for the debug mode.
Adding the following lines in the proguard file will fix the problem:
-keep class com.sun.mail.handlers.**
-dontwarn com.sun.mail.handlers.handler_base

Adding image to email via Java mail

Here is my code:
MimeMessage mail = new MimeMessage(session);
mail.setFrom(from);
MimeMultipart multipart = new MimeMultipart("related");
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(bodyText, "text/html");
multipart.addBodyPart(htmlPart);
MimeBodyPart imgPart=new MimeBodyPart();
String path = "/ivr/imagelogos/accenture.jpg";
DataSource ds=new FileDataSource(path);
imgPart.setDataHandler(new DataHandler(ds));
imgPart.setHeader("Content-ID","the-img-1");
multipart.addBodyPart(imgPart);
mail.setContent(multipart);
mail.setSentDate(new Date());
mail.setHeader("X-Mailer", "ALS Notifier Build 1.0.0.10");
// send the message
Transport.send(mail);
The code is being ran on a unix box - image path is based on unix file paths.
After running the code I receive this error:
IOException while sending message
javax.mail.MessagingException: IOException while sending message;
nested exception is:
java.io.FileNotFoundException: /ivr/imagelogos/accenture.jpg (No such file or directory)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:676)
Sounds like the /ivr/imagelogos/accenture.jpg file doesn't exist. Are you sure that's the right path? Maybe it's supposed to be relative to some other path? If it does exist, does the user running the Java app have read permissions on it?
Img src= is the most efficient way to insert just a few images, otherwise you may find it useful/helpful to define an array for multiple images.

Categories

Resources