Java Email Send getSenderEmail(MimeMessage msg)

Here you can find the source of getSenderEmail(MimeMessage msg)

Description

Gets the senderEmail attribute of the MimeMessageUtil class

License

Apache License

Parameter

Parameter Description
msg Description of the Parameter

Return

The senderEmail value

Declaration

public static String getSenderEmail(MimeMessage msg) 

Method Source Code

//package com.java2s;
/*//w  w  w .  j a v  a  2 s  .  c o  m
 * Copyright 1999-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Main {
    /**
     *  Description of the Field
     */
    public final static String SENDER_NOT_AVAILABLE = "-not available-";

    /**
     *  Gets the senderEmail attribute of the MimeMessageUtil class
     *
     *@param  msg  Description of the Parameter
     *@return      The senderEmail value
     */
    public static String getSenderEmail(MimeMessage msg) {
        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;
        } catch (MessagingException e) {
            senderEmail = SENDER_NOT_AVAILABLE;
        }
        if (senderEmail == null || senderEmail.trim().length() == 0) {
            senderEmail = SENDER_NOT_AVAILABLE;
        }
        return senderEmail;
    }
}

Related

  1. getMailSender(MimeMessage message)
  2. send(final String username, final String password, final String toEmail, final String subject, final String content)
  3. send(String from, String to, String bcc, String subject, String content)
  4. send(String to, String from, String subject, String text, Properties mailProps)
  5. sendBulkUpdateFailureNotice(final String msgBody)