Java Email Send sendEmail(String toAddress, String subject, String message)

Here you can find the source of sendEmail(String toAddress, String subject, String message)

Description

send Email

License

Apache License

Declaration

public static void sendEmail(String toAddress, String subject, String message)
            throws AddressException, MessagingException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static void sendEmail(String toAddress, String subject, String message)
            throws AddressException, MessagingException {

        final String userName = "idesaniv@gmail.com";
        final String password = "26577141";

        String host = "smtp.gmail.com", port = "25";

        // sets SMTP server properties
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", "smtp");
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", port);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");

        // creates a new session with an authenticator
        Authenticator auth = new Authenticator() {

            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
            }//  w w w  .j  a v  a 2  s .com
        };

        Session session = Session.getInstance(properties, auth);

        // creates a new e-mail message
        Message msg = new MimeMessage(session);

        msg.setFrom(new InternetAddress(userName));
        InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
        msg.setRecipients(Message.RecipientType.TO, toAddresses);
        msg.setSubject(subject);
        msg.setSentDate(new Date());
        msg.setText(message);

        // sends the e-mail
        Transport.send(msg);

    }
}

Related

  1. sendBulkUpdateFailureNotice(final String msgBody)
  2. sendEmail(final String aFromEmailAddr, final String aToEmailAddr, final String aSubject, final String aBody)
  3. sendEmail(Session session, String fromEmail, String toEmail, String subject, String body)
  4. sendEmail(String subject, String text, String receiverEmail)
  5. sendEmail(String to, String from, String subject, String text)
  6. sendMail(Properties props, String recipients[], String subject, String message, String from)
  7. sendMail(Session session, Message message)
  8. sendMail(String filePathName, InternetAddress[] addresses, List> md5cellRows, String today, byte[] report1bytes, byte[] report2bytes, byte[] report3bytes)
  9. sendMail(String host, int port, String username, String password, String recipients, String subject, String content, String from)