{"id":38,"date":"2017-08-29T19:03:46","date_gmt":"2017-08-29T17:03:46","guid":{"rendered":"http:\/\/www.tomvst.be\/blog\/?p=38"},"modified":"2017-08-29T19:03:46","modified_gmt":"2017-08-29T17:03:46","slug":"example-to-send-an-e-mail-from-your-java-application-with-or-without-tls-authentication","status":"publish","type":"post","link":"https:\/\/www.tomvst.net\/blog\/2017\/08\/example-to-send-an-e-mail-from-your-java-application-with-or-without-tls-authentication\/","title":{"rendered":"Example to send an e-mail from your Java application (with or without TLS authentication)"},"content":{"rendered":"<p>In this post I just wanted to share a small example to send an e-mail from your Java application, using TLS authentication. You&#8217;ll need to include the <strong>javax.mail.jar<\/strong> library from <a href=\"https:\/\/javaee.github.io\/javamail\/\">JavaMail<\/a> in your project as a dependency&#8230;<\/p>\n<p>&nbsp;<\/p>\n<p>I think all steps are pretty much self-explaining, like how to set the host, username and password, and how to add more recipients&#8230;<\/p>\n<p>&nbsp;<\/p>\n<pre>import java.util.*;\r\nimport javax.mail.*;\r\nimport javax.mail.internet.*;\r\n\r\npublic class SendMail {\r\n\r\n  public static void main(String[] args) {\r\n    new SendMail();\r\n  }\r\n\r\n  public SendMail() {\r\n    String toAddress = \"recipient@you.com\";\r\n    String fromAddress = \"from@me.com\";\r\n\r\n    Properties properties = System.getProperties();\r\n    properties.setProperty(\"mail.smtp.host\", \"smtp.website.net\");\r\n    properties.setProperty(\"mail.smtp.port\", \"25\");\r\n    properties.setProperty(\"mail.smtp.auth\", \"true\");\r\n    properties.setProperty(\"mail.smtp.starttls.enable\", \"true\");\r\n\r\n    \/\/ To override the Authenticator in the session\r\n    Authenticator authenticator = new Authenticator() {\r\n      @Override\r\n      protected PasswordAuthentication getPasswordAuthentication() {\r\n        return new PasswordAuthentication(\"username_or_email\", \"THE_P@SSWORD\");\r\n      }\r\n    };\r\n\r\n    \/\/ Create a session object with our above properties and the authenticator\r\n    Session session = Session.getInstance(properties, authenticator);\r\n\r\n    try {\r\n      \/\/ Create a MimeMessage object.\r\n      MimeMessage message = new MimeMessage(session);\r\n\r\n      message.setFrom(fromAddress);\r\n      message.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));\r\n      \/\/ Add TO, CC or BCC addresses as needed...\r\n\r\n      message.setSubject(\"This is a subject for my e-mail...\");\r\n      message.setText(\"This is the body of my message...\\n\\nSincerely yours,\\n\\n\\nTom\");\r\n\r\n      \/\/ Actually end the message...\r\n      Transport.send(message);\r\n      System.out.println(\"Message has been sent\");\r\n    } catch (MessagingException e) {\r\n      e.printStackTrace(System.err);\r\n    }\r\n  }\r\n}<\/pre>\n<p>If you don&#8217;t need authentication, your example becomes a lot shorter:<\/p>\n<pre>import java.util.*;\r\nimport javax.mail.*;\r\nimport javax.mail.internet.*;\r\n\r\npublic class SendMail {\r\n\r\n  public static void main(String[] args) {\r\n    new SendMail();\r\n  }\r\n\r\n  public SendMail() {\r\n    String toAddress = \"recipient@you.com\";\r\n    String fromAddress = \"from@me.com\";\r\n\r\n    Properties properties = System.getProperties();\r\n    properties.setProperty(\"mail.smtp.host\", \"smtp.website.net\");\r\n\r\n    \/\/ Create a session object with our above properties\r\n    Session session = Session.getDefaultInstance(properties);\r\n\r\n    try {\r\n      \/\/ Create a MimeMessage object.\r\n      MimeMessage message = new MimeMessage(session);\r\n\r\n      message.setFrom(fromAddress);\r\n      message.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));\r\n      \/\/ Add TO, CC or BCC addresses as needed...\r\n\r\n      message.setSubject(\"This is a subject for my e-mail...\");\r\n      message.setText(\"This is the body of my message...\\n\\nSincerely yours,\\n\\n\\nTom\");\r\n\r\n      \/\/ Actually end the message...\r\n      Transport.send(message);\r\n      System.out.println(\"Message has been sent\");\r\n    } catch (MessagingException e) {\r\n      e.printStackTrace(System.err);\r\n    }\r\n  }\r\n}<\/pre>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I just wanted to share a small example to send an e-mail from your Java application, using TLS authentication. You&#8217;ll need to&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[23,22,21],"class_list":["post-38","post","type-post","status-publish","format-standard","hentry","category-java","tag-e-mail","tag-java","tag-javamail"],"_links":{"self":[{"href":"https:\/\/www.tomvst.net\/blog\/wp-json\/wp\/v2\/posts\/38","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tomvst.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tomvst.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tomvst.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tomvst.net\/blog\/wp-json\/wp\/v2\/comments?post=38"}],"version-history":[{"count":1,"href":"https:\/\/www.tomvst.net\/blog\/wp-json\/wp\/v2\/posts\/38\/revisions"}],"predecessor-version":[{"id":39,"href":"https:\/\/www.tomvst.net\/blog\/wp-json\/wp\/v2\/posts\/38\/revisions\/39"}],"wp:attachment":[{"href":"https:\/\/www.tomvst.net\/blog\/wp-json\/wp\/v2\/media?parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tomvst.net\/blog\/wp-json\/wp\/v2\/categories?post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tomvst.net\/blog\/wp-json\/wp\/v2\/tags?post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}