Java Utililty Methods Email Send

List of utility methods to do Email Send

Description

The list of methods to do Email Send are organized into topic(s).

Method

StringgetMailSender(MimeMessage message)
Returns Mail Sender from the message
InternetAddress sender = (InternetAddress) message.getSender();
if (sender == null) {
    InternetAddress[] fromAddress = (InternetAddress[]) message.getFrom();
    if (fromAddress != null & fromAddress.length > 1) {
        return (fromAddress[0]).getAddress();
    return null;
return sender.getAddress();
StringgetSenderEmail(MimeMessage msg)
Gets the senderEmail attribute of the MimeMessageUtil class
String senderEmail = null;
try {
    InternetAddress[] from = (InternetAddress[]) msg.getFrom();
    if (from != null && from.length > 0) {
        senderEmail = from[0].getAddress();
} catch (AddressException e) {
    senderEmail = SENDER_NOT_AVAILABLE;
...
voidsend(final String username, final String password, final String toEmail, final String subject, final String content)
send
Properties properties = System.getProperties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
...
voidsend(String from, String to, String bcc, String subject, String content)
Send email
Properties props = new Properties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", smtpPort);
Session session = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
if (bcc != null) {
...
voidsend(String to, String from, String subject, String text, Properties mailProps)
Send an email to a recipient
Session mailSession = Session.getDefaultInstance(mailProps);
Message simpleMessage = new MimeMessage(mailSession);
InternetAddress fromAddress = new InternetAddress(from);
InternetAddress toAddress = new InternetAddress(to);
simpleMessage.setFrom(fromAddress);
simpleMessage.setRecipient(Message.RecipientType.TO, toAddress);
simpleMessage.setSubject(subject);
simpleMessage.setText(text);
...
booleansendBulkUpdateFailureNotice(final String msgBody)
send Bulk Update Failure Notice
Session session = Session.getInstance(emailProps, new javax.mail.Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("", "");
});
try {
    Message message = new MimeMessage(session);
...
voidsendEmail(final String aFromEmailAddr, final String aToEmailAddr, final String aSubject, final String aBody)
send Email
Session session = null;
session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("jizzoerice@gmail.com", "!amdaman01");
});
try {
...
voidsendEmail(Session session, String fromEmail, String toEmail, String subject, String body)
Utility method to send simple HTML email
try {
    MimeMessage msg = new MimeMessage(session);
    msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
    msg.addHeader("format", "flowed");
    msg.addHeader("Content-Transfer-Encoding", "8bit");
    msg.setFrom(new InternetAddress(fromEmail, "NoReply-JD"));
    msg.setReplyTo(InternetAddress.parse("no_reply@journaldev.com", false));
    msg.setSubject(subject, "UTF-8");
...
BooleansendEmail(String subject, String text, String receiverEmail)
send Email
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);
Message msg = new MimeMessage(session);
try {
...
voidsendEmail(String to, String from, String subject, String text)
send Email
Properties props = new Properties();
props.put("mail.smtp.host", "localhost");
props.put("mail.smtp.port", "35");
Session session = Session.getDefaultInstance(props);
try {
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
...