Java Email Send sendMail(String smtpServer, String to, String from, String subject, String body)

Here you can find the source of sendMail(String smtpServer, String to, String from, String subject, String body)

Description

send Mail

License

Open Source License

Declaration

public static void sendMail(String smtpServer, String to, String from, String subject, String body) 

Method Source Code


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

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

public class Main {
    public static void sendMail(String smtpServer, String to, String from, String subject, String body) {
        try {//from  w ww  .j ava2  s.  c  o m
            Properties props = System.getProperties();
            // -- Attaching to default Session, or we could start a new one --
            props.put("mail.smtp.host", smtpServer);
            Session session = Session.getDefaultInstance(props, null);
            // -- Create a new message --
            // Message msg = new javax.mail.Message(session);
            Message msg = new MimeMessage(session);
            // -- Set the FROM and TO fields --
            msg.setFrom(new InternetAddress(from));
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
            // -- We could include CC recipients too --
            // if (cc != null)
            // msg.setRecipients(Message.RecipientType.CC
            // ,InternetAddress.parse(cc, false));
            // -- Set the subject and body text --
            msg.setSubject(subject);
            msg.setText(body);
            // -- Set some other header information --
            msg.setHeader("X-Mailer", "LOTONtechEmail");
            msg.setSentDate(new Date());
            // -- Send the message --
            Transport.send(msg);
            System.out.println("Message sent OK.");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Related

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