Java mail MimeMessageメール送信

13326 ワード

import com.derbysoft.common.config.MailConfig;
import com.derbysoft.common.utils.string.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.activation.DataHandler;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.util.ByteArrayDataSource;
import java.io.IOException;
import java.util.Properties;

/**
 * Created by Harry on 2017/5/18.
 */
@Component
public class MailUtil {

    @Autowired
    MailConfig mailConfig;

    /**
     *     
     *       html  
     *
     * @param senderName sender name default dplatform
     * @param to         if exist more than one receiver using , splite
     * @param cc         if exist more than one receiver using , splite
     * @param subject
     * @param content
     * @param bytes      annex array
     * @param names      annex names
     * @throws MessagingException
     * @throws IOException
     */
    public void sendMail(String senderName, String to, String cc, String subject, String content, byte[][] bytes, String[] names) throws MessagingException, IOException {

        //     
        Properties properties = properties();
        //     
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                //  session       ,Transport        W
                return new PasswordAuthentication(mailConfig.getUserName(), mailConfig.getPassWord());
            }
        });

        //  
        MimeMessage msg = new MimeMessage(session);
        //    
        msg.setSubject(subject);
        //   ,       
        msg.setFrom(new InternetAddress("\"" + MimeUtility.encodeText(senderName) + "\"+ mailConfig.getUserName() + ">"));
        //       
//        msg.setReplyTo(new Address[]{new InternetAddress("[email protected]")});

        msg.setRecipients(Message.RecipientType.TO, to);
        if (!StringUtil.isEmpty(cc)) {
            msg.setRecipients(Message.RecipientType.CC, cc);
        }
        //     MINE   
        MimeMultipart msgMultipart = new MimeMultipart("mixed");//       
        //     MINE   
        msg.setContent(msgMultipart);

        //     
        if (bytes != null && names != null) {
            for (int i = 0; i < bytes.length; i++) {
                MimeBodyPart attch = new MimeBodyPart(); //   
                msgMultipart.addBodyPart(attch);         //       MIME    
                ByteArrayDataSource dataSource = new ByteArrayDataSource(bytes[i], "text/data"); //   
                attch.setDataHandler(new DataHandler(dataSource));
                attch.setFileName(names[i]);
            }
        }

        //html    
        MimeBodyPart htmlPart = new MimeBodyPart();
        msgMultipart.addBodyPart(htmlPart);
        //html  
        htmlPart.setContent(content, "text/html;charset=utf-8");
        //    
        Transport.send(msg, msg.getAllRecipients());

    }

    protected Properties properties() {
        //     
        Properties properties = new Properties();
        //   debug   ,    
        properties.setProperty("mail.debug", "false");
        //            
        properties.setProperty("mail.smtp.auth", mailConfig.getAuth());
        //        ,     ,   25
        properties.setProperty("mail.smtp.port", mailConfig.getPort());
        //         
        properties.setProperty("mail.transport.protocol", mailConfig.getProtocol());
        //           
        properties.setProperty("mail.host", mailConfig.getHost());

        return properties;
    }

}