Java Email Send sendMail(String host, int port, String username, String password, String recipients, String subject, String content, String from)

Here you can find the source of sendMail(String host, int port, String username, String password, String recipients, String subject, String content, String from)

Description

send Mail

License

Open Source License

Declaration

public static void sendMail(String host, int port, String username, String password, String recipients,
            String subject, String content, String from) throws AddressException, MessagingException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Main {
    public static void sendMail(String host, int port, String username, String password, String recipients,
            String subject, String content, String from) throws AddressException, MessagingException {

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);

        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }/*ww w. j a v  a 2 s.  c  o m*/
        });

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
        message.setSubject(subject);
        message.setText(content);

        Transport.send(message);
    }
}

Related

  1. sendEmail(String to, String from, String subject, String text)
  2. sendEmail(String toAddress, String subject, String message)
  3. sendMail(Properties props, String recipients[], String subject, String message, String from)
  4. sendMail(Session session, Message message)
  5. sendMail(String filePathName, InternetAddress[] addresses, List> md5cellRows, String today, byte[] report1bytes, byte[] report2bytes, byte[] report3bytes)
  6. sendMail(String smtpServer, String to, String from, String subject, String body)
  7. sendMail(String to, String from, String subject, String body, boolean bodyIsHTML)
  8. sendMail(String toEmailId, String subject, String msgText, String from, String smtpServer, String userName, String password)
  9. sendMail(String vmName, String property, String emailId, boolean usageHigh)