Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is it possible to send an email from my Java application using a GMail account? I have configured my company mail server with Java app to send email, but that's not going to cut it when I distribute the application. Answers with any of using Hotmail, Yahoo or GMail are acceptable.

like image 284
Bill the Lizard Avatar asked Sep 05 '08 19:09

Bill the Lizard


People also ask

Can you send Java files over email?

For sending email with attachment, JavaMail API provides some useful classes like BodyPart, MimeBodyPart etc. For better understanding of this example, learn the steps of sending email using JavaMail API first. For sending the email using JavaMail API, you need to load the two jar files: mail.

What is mail SMTP host in Java?

Advertisements. SMTP is an acronym for Simple Mail Transfer Protocol. It is an Internet standard for electronic mail (e-mail) transmission across Internet Protocol (IP) networks.

How do I send an email from Java to Gmail?

Send Email in Java using Gmail SMTP with SSL [Secure Socket Layer] You just need to set the properties “mail.smtp.socketFactory.port”, “mail.smtp.socketFactory.class” to the properties instance using the put() method to send email in Java using Gmail with SSL authentication.

How to send an email to multiple recipients in Java?

import javax.activation.*; Sending Email to multiple recipients is same as that for single recipient. The difference is that to send a mail to multiple recipients you should add multiple recipients. To add multiple recipients we have to invoke the following method and pass the type of recipient and list of email addresses as arguments:

How to install JavaMail API and JAF?

You may download the latest version of both JavaMail API and JAF from the official website of Java. After successfully downloading these two, extract them. Add mail.jar , smtp.jar and activation.jar file in your classpath to be eligible to send emails.

Can I Use my Yahoo Mail account to spam someone else?

If you can send an email from and to the same account, you can use it to send any addresses. After you learned how to send an email from your Yahoo! Mail account, please do not use it to spam someone else. Especially not to mass spam many people at once. If you do, I am sure you can get into trouble.


2 Answers

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 = "********"; // GMail password     private static String RECIPIENT = "[email protected]";      public static void main(String[] args) {         String from = USER_NAME;         String pass = PASSWORD;         String[] to = { RECIPIENT }; // list of recipient email addresses         String subject = "Java send mail example";         String body = "Welcome to JavaMail!";          sendFromGMail(from, pass, to, subject, body);     }      private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {         Properties props = System.getProperties();         String host = "smtp.gmail.com";         props.put("mail.smtp.starttls.enable", "true");         props.put("mail.smtp.host", host);         props.put("mail.smtp.user", from);         props.put("mail.smtp.password", pass);         props.put("mail.smtp.port", "587");         props.put("mail.smtp.auth", "true");          Session session = Session.getDefaultInstance(props);         MimeMessage message = new MimeMessage(session);          try {             message.setFrom(new InternetAddress(from));             InternetAddress[] toAddress = new InternetAddress[to.length];              // To get the array of addresses             for( int i = 0; i < to.length; i++ ) {                 toAddress[i] = new InternetAddress(to[i]);             }              for( int i = 0; i < toAddress.length; i++) {                 message.addRecipient(Message.RecipientType.TO, toAddress[i]);             }              message.setSubject(subject);             message.setText(body);             Transport transport = session.getTransport("smtp");             transport.connect(host, from, pass);             transport.sendMessage(message, message.getAllRecipients());             transport.close();         }         catch (AddressException ae) {             ae.printStackTrace();         }         catch (MessagingException me) {             me.printStackTrace();         }     } } 

Naturally, you'll want to do more in the catch blocks than print the stack trace as I did in the example code above. (Remove the catch blocks to see which method calls from the JavaMail API throw exceptions so you can better see how to properly handle them.)


Thanks to @jodonnel and everyone else who answered. I'm giving him a bounty because his answer led me about 95% of the way to a complete answer.

like image 87
Bill the Lizard Avatar answered Oct 05 '22 12:10

Bill the Lizard


Something like this (sounds like you just need to change your SMTP server):

String host = "smtp.gmail.com"; String from = "user name"; Properties props = System.getProperties(); props.put("mail.smtp.host", host); props.put("mail.smtp.user", from); props.put("mail.smtp.password", "asdfgh"); props.put("mail.smtp.port", "587"); // 587 is the port number of yahoo mail props.put("mail.smtp.auth", "true");  Session session = Session.getDefaultInstance(props, null); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from));  InternetAddress[] to_address = new InternetAddress[to.length]; int i = 0; // To get the array of addresses while (to[i] != null) {     to_address[i] = new InternetAddress(to[i]);     i++; } System.out.println(Message.RecipientType.TO); i = 0; while (to_address[i] != null) {      message.addRecipient(Message.RecipientType.TO, to_address[i]);     i++; } message.setSubject("sending in a group"); message.setText("Welcome to JavaMail"); // alternately, to send HTML mail: // message.setContent("<p>Welcome to JavaMail</p>", "text/html"); Transport transport = session.getTransport("smtp"); transport.connect("smtp.mail.yahoo.co.in", "user name", "asdfgh"); transport.sendMessage(message, message.getAllRecipients()); transport.close(); 
like image 23
jodonnell Avatar answered Oct 05 '22 10:10

jodonnell