<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="https://stackoverflow.com/questions/46663/how-do-you-send-email-from-a-java-app-using-gmail">How do you send email from a Java app using Gmail?</a>  </p>
</blockquote>



<p>How do I send an SMTP Message from Java?</p>

[{'answer_id': 73596, 'author': 'Mason', 'author_id': 8973, 'author_profile': 'https://Stackoverflow.com/users/8973', 'pm_score': 2, 'selected': False, 'text': '<p>See the <a href="http://www.oracle.com/technetwork/java/javamail/index.html" rel="nofollow noreferrer">JavaMail API</a> and associated javadocs.</p>\n'}, {'answer_id': 73599, 'author': 'Ryan Lanciaux', 'author_id': 1385358, 'author_profile': 'https://Stackoverflow.com/users/1385358', 'pm_score': 2, 'selected': False, 'text': '<p>Please see this post </p>\n\n<p><a href="https://stackoverflow.com/questions/46663/how-do-you-send-email-from-a-java-app-using-gmail">How can I send an email by Java application using GMail, Yahoo, or Hotmail?</a></p>\n\n<p>It is specific to gmail but you can substitute your smtp credentials.</p>\n'}, {'answer_id': 73600, 'author': 'Jorge Ferreira', 'author_id': 6508, 'author_profile': 'https://Stackoverflow.com/users/6508', 'pm_score': 2, 'selected': False, 'text': '<p>See the following tutorial at Java Practices.</p>\n\n<p><a href="http://www.javapractices.com/topic/TopicAction.do?Id=144" rel="nofollow noreferrer">http://www.javapractices.com/topic/TopicAction.do?Id=144</a></p>\n'}, {'answer_id': 73649, 'author': 'tovare', 'author_id': 12677, 'author_profile': 'https://Stackoverflow.com/users/12677', 'pm_score': 6, 'selected': True, 'text': '<p>Here\'s an example for Gmail smtp:</p>\n\n<pre><code>import java.io.*;\nimport java.net.InetAddress;\nimport java.util.Properties;\nimport java.util.Date;\n\nimport javax.mail.*;\n\nimport javax.mail.internet.*;\n\nimport com.sun.mail.smtp.*;\n\n\npublic class Distribution {\n\n    public static void main(String args[]) throws Exception {\n        Properties props = System.getProperties();\n        props.put("mail.smtps.host","smtp.gmail.com");\n        props.put("mail.smtps.auth","true");\n        Session session = Session.getInstance(props, null);\n        Message msg = new MimeMessage(session);\n        msg.setFrom(new InternetAddress("mail@tovare.com"));;\n        msg.setRecipients(Message.RecipientType.TO,\n        InternetAddress.parse("tov.are.jacobsen@iss.no", false));\n        msg.setSubject("Heisann "+System.currentTimeMillis());\n        msg.setText("Med vennlig hilsennTov Are Jacobsen");\n        msg.setHeader("X-Mailer", "Tov Are\'s program");\n        msg.setSentDate(new Date());\n        SMTPTransport t =\n            (SMTPTransport)session.getTransport("smtps");\n        t.connect("smtp.gmail.com", "admin@tovare.com", "&lt;insert password here&gt;");\n        t.sendMessage(msg, msg.getAllRecipients());\n        System.out.println("Response: " + t.getLastServerResponse());\n        t.close();\n    }\n}\n</code></pre>\n\n<p>Now, do it this way only if you would like to keep your project dependencies to a minimum, otherwise i can warmly recommend using classes from apache</p>\n\n<p><a href="http://commons.apache.org/email/" rel="noreferrer">http://commons.apache.org/email/</a> </p>\n\n<p>Regards</p>\n\n<p>Tov Are Jacobsen</p>\n'}, {'answer_id': 90001, 'author': 'Brad at Kademi', 'author_id': 17025, 'author_profile': 'https://Stackoverflow.com/users/17025', 'pm_score': 3, 'selected': False, 'text': '<p>Another way is to use aspirin (<a href="https://github.com/masukomi/aspirin" rel="nofollow noreferrer">https://github.com/masukomi/aspirin</a>) like this:</p>\n\n<pre><code>MailQue.queMail(MimeMessage message)\n</code></pre>\n\n<p>..after having constructed your mimemessage as above.</p>\n\n<p>Aspirin <strong>is</strong> an smtp \'server\' so you don\'t have to configure it. But note that sending email to a broad set of recipients isnt as simple as it appears because of the many different spam filtering rules receiving mail servers and client applications apply. </p>\n'}, {'answer_id': 4578893, 'author': 'user527619', 'author_id': 527619, 'author_profile': 'https://Stackoverflow.com/users/527619', 'pm_score': 1, 'selected': False, 'text': '<pre><code>import javax.mail.*;\nimport javax.mail.internet.*;\nimport java.util.*; \n\npublic void postMail(String recipients[], String subject,\n    String message , String from) throws MessagingException {\n\n    //Set the host smtp address\n    Properties props = new Properties();\n    props.put("mail.smtp.host", "smtp.jcom.net");\n\n    // create some properties and get the default Session\n    Session session = Session.getDefaultInstance(props, null);\n    session.setDebug(false);\n\n    // create a message\n    Message msg = new MimeMessage(session);\n\n    // set the from and to address\n    InternetAddress addressFrom = new InternetAddress(from);\n    msg.setFrom(addressFrom);\n\n    InternetAddress[] addressTo = new InternetAddress[recipients.length]; \n    for (int i = 0; i &lt; recipients.length; i++) {\n        addressTo[i] = new InternetAddress(recipients[i]);\n    }\n    msg.setRecipients(Message.RecipientType.TO, addressTo);\n\n    // Optional : You can also set your custom headers in the Email if you Want\n    msg.addHeader("MyHeaderName", "myHeaderValue");\n\n    // Setting the Subject and Content Type\n    msg.setSubject(subject);\n    msg.setContent(message, "text/plain");\n    Transport.send(msg);\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/73580', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2443/']