Java Email Send sendEmail(final String aFromEmailAddr, final String aToEmailAddr, final String aSubject, final String aBody)

Here you can find the source of sendEmail(final String aFromEmailAddr, final String aToEmailAddr, final String aSubject, final String aBody)

Description

send Email

License

Open Source License

Declaration

public static void sendEmail(final String aFromEmailAddr, final String aToEmailAddr, final String aSubject,
            final String aBody) 

Method Source Code


//package com.java2s;
/*/*from w w  w .jav a 2 s . co  m*/
 * SwingTech Software - http://cooksarm.sourceforge.net/
 * 
 * Copyright (C) 2011 Joe Rice All rights reserved.
 * 
 * SwingTech Cooks Arm is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option) any
 * later version.
 * 
 * SwingTech Cooks Arm is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * SwingTech Cooks Arm; If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Main {
    static Properties props = new Properties();

    public static void sendEmail(final String aFromEmailAddr, final String aToEmailAddr, final String aSubject,
            final String aBody) {
        Session session = null;
        session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("jizzoerice@gmail.com", "!amdaman01");
            }
        });

        try {
            final Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(aFromEmailAddr));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(aToEmailAddr));
            message.setSubject(aSubject);
            message.setText(aBody);

            Transport.send(message);

            System.out.println("Done");
        } catch (final MessagingException e) {
            System.out.println("WARNING!!!!  tried unsucessfully to send email.  Details:  from:  " + aFromEmailAddr
                    + ".  to:  " + aToEmailAddr + ".  subject:  " + aSubject + ".  body:  " + aBody);
        }
    }
}

Related

  1. getSenderEmail(MimeMessage msg)
  2. send(final String username, final String password, final String toEmail, final String subject, final String content)
  3. send(String from, String to, String bcc, String subject, String content)
  4. send(String to, String from, String subject, String text, Properties mailProps)
  5. sendBulkUpdateFailureNotice(final String msgBody)
  6. sendEmail(Session session, String fromEmail, String toEmail, String subject, String body)
  7. sendEmail(String subject, String text, String receiverEmail)
  8. sendEmail(String to, String from, String subject, String text)
  9. sendEmail(String toAddress, String subject, String message)