My Java program stopped sending emails using my gmail Account

The key error is this: Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target See this JavaMail FAQ entry. Since you’re connecting to Gmail, this shouldn’t happen. The most likely causes are: There’s a firewall or anti-virus program intercepting your request. There’s something wrong in your JDK installation preventing it from finding … Read more

javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; boundary

JavaMail depends on some configuration files to map MIME types to Java classes (e.g., multipart/mixed to javax.mail.internet.MimeMultipart). These configuration files are loaded using the ClassLoader for the application. If the ClassLoader doesn’t function properly, these configuration files won’t be found. You can simply add below lines .. that solves the issue . MailcapCommandMap mc = … Read more

how to send HTML email

Don’t upcast your MimeMessage to Message: MimeMessage simpleMessage = new MimeMessage(mailSession); Then, when you want to set the message body, either call simpleMessage.setText(text, “utf-8”, “html”); or call simpleMessage.setContent(text, “text/html; charset=utf-8”); If you’d rather use a charset other than utf-8, substitute it in the appropriate place. JavaMail has an extra, useless layer of abstraction that often … Read more

Send mail to multiple recipients in Java

If you invoke addRecipient multiple times, it will add the given recipient to the list of recipients of the given time (TO, CC, and BCC). For example: message.addRecipient(Message.RecipientType.CC, InternetAddress.parse(“[email protected]”)); message.addRecipient(Message.RecipientType.CC, InternetAddress.parse(“[email protected]”)); message.addRecipient(Message.RecipientType.CC, InternetAddress.parse(“[email protected]”)); It will add the three addresses to CC. If you wish to add all addresses at once, you should use setRecipients or … Read more

Sending email using JSP

Well offhand I would say there is an authentication issue when trying to connect. You are not providing any username or password, unless your exchange server doesn’t require username and password. UPDATE: If using JDK 7 see the following post, it resolved this issue: Defect – JDK7 Permission Denied with Sockets when using VPN “More … Read more

DCH class error with JavaMail

Add these before you send your message: MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap(); mc.addMailcap(“text/html;; x-java-content-handler=com.sun.mail.handlers.text_html”); mc.addMailcap(“text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml”); mc.addMailcap(“text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain”); mc.addMailcap(“multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed”); mc.addMailcap(“message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822”); CommandMap.setDefaultCommandMap(mc); I got the problem in my Android app and it works.

Download attachments using Java Mail

Without exception handling, but here goes: List<File> attachments = new ArrayList<File>(); for (Message message : temp) { Multipart multipart = (Multipart) message.getContent(); for (int i = 0; i < multipart.getCount(); i++) { BodyPart bodyPart = multipart.getBodyPart(i); if(!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()) && StringUtils.isBlank(bodyPart.getFileName())) { continue; // dealing with attachments only } InputStream is = bodyPart.getInputStream(); // — EDIT — … Read more