Friday, 5 October 2012

SendingMail using Java

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

public class GmailSendEmailSSL {
    public static final String USERNAME = "xxxxxxxxxxxxxxxx";
    public static final String PASSWORD = "xxxxxxxxxxxxxxxx";

    public static void main(String[] args) throws Exception {
        //
        // Email information such as from, to, subject and contents.
        //
        String mailFrom = "xxxxxxxxxxxxxxxxxxxxxx";
        String mailTo = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
        String mailSubject = "TestReport";
        String mailText = "Please check enclosed TestReport";

        GmailSendEmailSSL gmail = new GmailSendEmailSSL();
        gmail.sendMail(mailFrom, mailTo, mailSubject, mailText);
    }

    private void sendMail(String mailFrom, String mailTo,
                          String mailSubject, String mailText)
            throws Exception {

        Properties config = createConfiguration();

        //
        // Creates a mail session. We need to supply username and
        // password for Gmail authentication.
        //
        Session session = Session.getInstance(config, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(
                        GmailSendEmailSSL.USERNAME,
                        GmailSendEmailSSL.PASSWORD
                );
            }
        });

        //
        // Creates email message
        //
        Message message = new MimeMessage(session);
        message.setSentDate(new Date());
        message.setFrom(new InternetAddress(mailFrom));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
        message.setSubject(mailSubject);
        message.setText(mailText);

        //
        // Send a message
        //
        Transport.send(message);
    }

    private Properties createConfiguration() {
        return new Properties() {{
            put("mail.smtp.host", "smtp.gmail.com");
            put("mail.smtp.auth", "true");
            put("mail.smtp.port", "465");
            put("mail.smtp.socketFactory.port", "465");
            put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        }};
    }
}

12 comments:

  1. It’s really amazing that we can record what our visitors do on our site. Thanks for sharing this awesome guide. I’m happy that I came across with your site this article is on point,thanks again and have a great day. Keep update more information..

    CRO Agency in Chennai

    ReplyDelete
  2. Informative post.I got more knowledge from this blog.Thanks for sharing.... Warehouse Audit
    Stock Audit
    Customer Reconciliation

    ReplyDelete
  3. I really enjoyed your blog Thanks for sharing such an informative post.Looking For Some More Stuff.

    shuttering works

    ReplyDelete
  4. Wonderful write-up! I learned several new things from this blog.workday hr training

    ReplyDelete
  5. After comparing several options for the best powerbi training online, I found OnlineITGuru to be a strong choice because of its experienced trainers, live projects, and certification support.best power bi training online

    ReplyDelete
  6. Online Tableau Training makes it easy to master dashboard creation, data analysis, and reporting without attending physical classes. A structured curriculum, live instructor support, and project-based learning help learners understand Tableau concepts more effectively. It's a valuable investment for anyone planning a career in analytics or business intelligence.online tableau training

    ReplyDelete
  7. The article provides some valuable insights into learning data visualization tools. A structured tableau online course can be particularly helpful for learners who want to progress from basic charts to more advanced dashboards. Thanks for the useful information.tableau online course

    ReplyDelete
  8. Oracle Service Bus training helps professionals understand how to integrate applications and manage enterprise services efficiently. This oracle service bus training covers proxy services, business services, pipelines, adapters, message transformation, and service monitoring. Students strengthen their skills through hands-on exercises and real-world scenarios.

    ReplyDelete