{"id":1917,"date":"2011-04-06T15:30:31","date_gmt":"2011-04-06T10:00:31","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1917"},"modified":"2011-04-06T15:30:31","modified_gmt":"2011-04-06T10:00:31","slug":"a-simple-program-to-send-email-in-java","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/a-simple-program-to-send-email-in-java\/","title":{"rendered":"A simple program to send email in JAVA"},"content":{"rendered":"<p>We will see a very simple program to send the email in JAVA using <a title=\"JAVA Email API\" href=\"http:\/\/www.oracle.com\/technetwork\/java\/javamail\/index.html\" target=\"_blank\">JAVAMail API<\/a>. Please download all the jar files and include in the library.<\/p>\n<p>Code to send email :<!--more--><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\nimport javax.mail.*;\nimport javax.mail.internet.*;\nimport java.util.*;\nimport java.io.*;\n\npublic class SendMailUsingAuthentication\n{\n\n  private static final String SMTP_HOST_NAME = &quot;SMTP_HOST_NAME&quot;;\n  private static final String SMTP_PORT = &quot;465&quot;;\n\n  private static final String SMTP_AUTH_USER = &quot;SENDER_EMAIL&quot;;\n  private static final String SMTP_AUTH_PWD  = &quot;SENDER_PASS&quot;;\n\n  private static final String emailMsgTxt      = &quot;-----------Hello------------------ &quot;;\n  private static final String emailSubjectTxt  = &quot;Test Mail from JAVA&quot;;\n\tprivate static final String emailFromAddress = &quot;FROM_EMAIL&quot;;\n\n  \/\/ Add List of Email address to who email needs to be sent to\n\tprivate static final String&#x5B;] emailList = { &quot;TO_EMAIL_USER1&quot;,&quot;TO_EMAIL_USER2&quot; };\n\n  public static void main(String args&#x5B;]) throws Exception\n  {\n    SendMailUsingAuthentication mailSender = new SendMailUsingAuthentication();\n\tmailSender.postMail(emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);\n    System.out.println(&quot;Sucessfully Sent mail to All Users&quot;);\n  }\n\n  public void postMail( String recipients&#x5B; ], String subject,\n                            String message , String from) throws MessagingException\n  {\n    boolean debug = false;\n            \/\/Set the host smtp address\n\tProperties props = new Properties();\n\tprops.put(&quot;mail.smtp.host&quot;, SMTP_HOST_NAME);\n\tprops.put(&quot;mail.smtp.socketFactory.port&quot;, SMTP_PORT);\n            \/\/IF SSL required by Host\n\tprops.put(&quot;mail.smtp.socketFactory.class&quot; , &quot;javax.net.ssl.SSLSocketFactory&quot;);\n\tprops.put(&quot;mail.smtp.auth&quot;, &quot;true&quot;);\n\tprops.put(&quot;mail.debug&quot;, &quot;true&quot;);\n\tprops.put(&quot;mail.smtp.port&quot;, SMTP_PORT);\n\n            \/\/Authenticate the Username and password for the SMTP\n\tAuthenticator auth = new PasswordAuthentication(SMTP_AUTH_USER, SMTP_AUTH_PWD);\n\n    Session session = Session.getDefaultInstance(props, auth);\n\n    session.setDebug(debug);\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&#x5B;] addressTo = new InternetAddress&#x5B;recipients.length];\n    for (int i = 0; i &lt; recipients.length; i++)\n    {\n        addressTo&#x5B;i] = new InternetAddress(recipients&#x5B;i]);\n    }\n    msg.setRecipients(Message.RecipientType.TO, addressTo);\n\n    \/\/ Setting the Subject and Content Type\n    msg.setSubject(subject);\n    msg.setContent(message, &quot;text\/plain&quot;);\n    System.out.println(&quot;Sending Email...........&quot;);\n    Transport.send(msg);\n\n    System.out.println(&quot;Email Sent.........&quot;);\n\n }\n\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A Simple SMTP Email Program in JAVA<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[89,182],"class_list":["post-1917","post","type-post","status-publish","format-standard","hentry","category-java","tag-email","tag-smtp"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":2061,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/explain-serialversionuid-in-java\/","url_meta":{"origin":1917,"position":0},"title":"Explain serialVersionUID in Java","author":"Jitendra","date":"April 20, 2011","format":false,"excerpt":"What is serialVersionUID and what is its need ?","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1612,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/serialization-marshalling-deflating-in-java\/","url_meta":{"origin":1917,"position":1},"title":"Serialization \/ Marshalling \/ Deflating in JAVA","author":"Jitendra","date":"March 9, 2011","format":false,"excerpt":"Concept of Serialization \/ Marshalling \/ Deflating in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Serialization ,Marshalling ,deflating, DeSerialization ,UnMarshalling ,inflating","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Serialization-Marshalling-deflating-DeSerialization-UnMarshalling-inflating.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3150,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/merge-pdf-in-salesforce-using-java-itextpdf-and-oauth-2\/","url_meta":{"origin":1917,"position":2},"title":"Merge PDF in Salesforce Using Java, ITextPDF and OAuth 2","author":"Jitendra","date":"December 9, 2012","format":false,"excerpt":"Its long time, since i wrote any article because of my busy schedule However this time i came with advance one. In this article we are going to use the J2EE (Servlet) to Merge PDF attachment inside salesforce with the help of OAuth and ITextPDF jar file. The reason of\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Create Remote Access in Salesforce.com for OAuth 2","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/12\/Create-Remote-Access-in-Salesforce.com_.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/12\/Create-Remote-Access-in-Salesforce.com_.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/12\/Create-Remote-Access-in-Salesforce.com_.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2347,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/hibernate\/step-by-step-hibernate-tutorial-using-eclipse-wtp\/","url_meta":{"origin":1917,"position":3},"title":"Step By Step Hibernate Tutorial Using eclipse WTP","author":"Jitendra","date":"August 8, 2011","format":false,"excerpt":"Step By Step Hibernate (ORM Tool) Tutorial Using eclipse WTP","rel":"","context":"In &quot;Hibernate&quot;","block_context":{"text":"Hibernate","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/hibernate\/"},"img":{"alt_text":"Eclipse Install New Software - Hibernate","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Eclipse-Install-New-Software-Hibernate.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1582,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/snmp\/generating-trap-in-snmp-using-snmp4j\/","url_meta":{"origin":1917,"position":4},"title":"Generating TRAP in SNMP using SNMP4j","author":"Jitendra","date":"February 24, 2011","format":false,"excerpt":"Generating TRAP in SNMP using SNMP4j","rel":"","context":"In &quot;SNMP&quot;","block_context":{"text":"SNMP","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/snmp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2700,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/how-to-create-immutable-class-in-java\/","url_meta":{"origin":1917,"position":5},"title":"How to create Immutable Class in Java","author":"Jitendra","date":"February 3, 2012","format":false,"excerpt":"creating the Immutable class in Java with simple source code example","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1917","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1917"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1917\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}