Java Email Send sendEmail(String subject, String text, String receiverEmail)

Here you can find the source of sendEmail(String subject, String text, String receiverEmail)

Description

send Email

License

Open Source License

Declaration

public static Boolean sendEmail(String subject, String text, String receiverEmail) 

Method Source Code


//package com.java2s;
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;
import java.util.Properties;

public class Main {
    private static final String smtpServerAddress = "smtp.126.com";
    private static final String senderEmail = "panzhangbao@126.com";
    private static final String senderAuthorizationCode = "";

    public static Boolean sendEmail(String subject, String text, String receiverEmail) {
        Properties props = new Properties();
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.transport.protocol", "smtp");
        props.put("mail.smtp.host", smtpServerAddress);

        Session session = Session.getInstance(props);
        session.setDebug(false);//from ww  w  .ja v  a  2s  .c  om

        Message msg = new MimeMessage(session);
        try {
            msg.setSubject(subject);
            msg.setText(text);
            msg.setFrom(new InternetAddress(senderEmail));
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(receiverEmail));
            msg.saveChanges();

            Transport transport = session.getTransport();
            transport.connect(senderEmail, senderAuthorizationCode);
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
        }

        return false;
    }
}

Related

  1. send(String from, String to, String bcc, String subject, String content)
  2. send(String to, String from, String subject, String text, Properties mailProps)
  3. sendBulkUpdateFailureNotice(final String msgBody)
  4. sendEmail(final String aFromEmailAddr, final String aToEmailAddr, final String aSubject, final String aBody)
  5. sendEmail(Session session, String fromEmail, String toEmail, String subject, String body)
  6. sendEmail(String to, String from, String subject, String text)
  7. sendEmail(String toAddress, String subject, String message)
  8. sendMail(Properties props, String recipients[], String subject, String message, String from)
  9. sendMail(Session session, Message message)