Java Email Send sendMail(String toEmailId, String subject, String msgText, String from, String smtpServer, String userName, String password)

Here you can find the source of sendMail(String toEmailId, String subject, String msgText, String from, String smtpServer, String userName, String password)

Description

send Mail

License

Open Source License

Declaration

public static void sendMail(String toEmailId, String subject, String msgText, String from, String smtpServer,
            String userName, String password) throws MessagingException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *  This file is part of free utilities created by Joga Singh <joga.singh@gmail.com>.
 *  You are free to copy/modify/distribute the files to use it in any way you like.
 *  However as a credit, author's name should be mentioned in the file header.
 *  //from  www  .j  av  a 2s .c o m
 *  See the complete license terms (MIT License) in LICENSE.TXT included in the package.
 *  
 *******************************************************************************/

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

public class Main {
    public static void sendMail(String toEmailId, String subject, String msgText, String from, String smtpServer,
            String userName, String password) throws MessagingException {

        Properties properties = new Properties();
        properties.setProperty("mail.smtp.host", smtpServer);
        Session session = Session.getDefaultInstance(properties);
        MimeMessage message = new MimeMessage(session);

        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmailId));
        message.setSubject(subject);
        message.setContent(msgText, "text/html; charset=utf-8");
        message.setSentDate(new Date());
        Transport.send(message);
    }
}

Related

  1. sendMail(Session session, Message message)
  2. sendMail(String filePathName, InternetAddress[] addresses, List> md5cellRows, String today, byte[] report1bytes, byte[] report2bytes, byte[] report3bytes)
  3. sendMail(String host, int port, String username, String password, String recipients, String subject, String content, String from)
  4. sendMail(String smtpServer, String to, String from, String subject, String body)
  5. sendMail(String to, String from, String subject, String body, boolean bodyIsHTML)
  6. sendMail(String vmName, String property, String emailId, boolean usageHigh)
  7. sendMessage(Message message)
  8. sendMessage(Message msg, Transport transport)
  9. sendMimeMessage(MimeMessage mimeMessage)