Getting mail from GMail into Java application using IMAP

Using imaps was a great suggestion. Neither of the answers provided just worked for me, so I googled some more and found something that worked. Here’s how my code looks now. Properties props = System.getProperties(); props.setProperty(“mail.store.protocol”, “imaps”); try { Session session = Session.getDefaultInstance(props, null); Store store = session.getStore(“imaps”); store.connect(“imap.gmail.com”, “<username>@gmail.com”, “<password>”); … } catch (NoSuchProviderException … 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(“abc@abc.example”)); message.addRecipient(Message.RecipientType.CC, InternetAddress.parse(“abc@def.example”)); message.addRecipient(Message.RecipientType.CC, InternetAddress.parse(“ghi@abc.example”)); It will add the three addresses to CC. If you wish to add all addresses at once, you should use setRecipients or … Read more

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

How do I send an HTML email?

As per the Javadoc, the MimeMessage#setText() sets a default mime type of text/plain, while you need text/html. Rather use MimeMessage#setContent() instead. message.setContent(someHtmlMessage, “text/html; charset=utf-8”); For additional details, see: GMail Media Queries GMail CSS Design CSS support in mail clients

java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger for JUnit test case for Java mail

The javax.mail-api artifact is only good for compiling against. You actually need to run code, so you need a complete implementation of JavaMail API. Use this: <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency> NOTE: The version number will probably differ. Check the latest version here.

How can I send an email by Java application using GMail, Yahoo, or Hotmail?

First download the JavaMail API and make sure the relevant jar files are in your classpath. Here’s a full working example using GMail. import java.util.*; import javax.mail.*; import javax.mail.internet.*; public class Main { private static String USER_NAME = “*****”; // GMail user name (just the part before “@gmail.com”) private static String PASSWORD = “********”; // … Read more

Sending Email in Android using JavaMail API without using the default/built-in app

Send e-mail in Android using the JavaMail API using Gmail authentication. Steps to create a sample Project: MailSenderActivity.java: public class MailSenderActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button send = (Button) this.findViewById(R.id.send); send.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { try { GMailSender sender = new GMailSender(“username@gmail.com”, “password”); sender.sendMail(“This … Read more

tech