Using java api to create excel content and send email

Posted by phpMover on Tue, 10 Dec 2019 21:57:59 +0100

Copyright notice: This is the original article of the blogger. It can't be reproduced without the permission of the blogger. https://blog.csdn.net/qq_26654727/article/details/83473968

Article directory


Sketch:
Recently, use github to classify the address of the tool class github you usually use:
https://github.com/auguszero/javaToolRepsitory

Using java api to create excel content and send email

Main functions:

1. Send mail to sender, receiver and CC through profile settings 
2. At present, the user-defined sending content is implemented
 3. Realize the content of excel email 

Core code (SendMailManager):

package com.javatool.email.proxy;
import com.javatool.configer.DefaultConfigerContext;
import com.javatool.email.model.MailAuthenticator;
import com.javatool.email.model.SendMailConfigModel;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * @author haisong
 * @create 2018/09/13 14:21
 */
public class SendMailManager {


    /**
     * Send mail
     * @param subject Mail theme
     * @param content Mail content
     * @return success Send success failure send failure
     * @throws Exception
     */
    public static String sendMail( String subject, String content)
            throws Exception {
        SendMailConfigModel sendMailConfigModel  = DefaultConfigerContext.getInstance().getModelFromProperties(SendMailConfigModel.class);
        String to = sendMailConfigModel.getTo();
        if (to != null){
              Properties props = System.getProperties();
            props.put("mail.smtp.host", sendMailConfigModel.getMail_smtp_host());
            props.put("mail.smtp.auth", "true");
            props.put("mail.transport.protocol", "smtp");
            MailAuthenticator auth = new MailAuthenticator();
            Session session = Session.getInstance(props, auth);
            session.setDebug(true);
            try {
                MimeMessage message = new MimeMessage(session);
                message.setFrom(new InternetAddress(sendMailConfigModel.getFrom()));
                if (!to.trim().equals("")) {
                    message.addRecipient(Message.RecipientType.TO,
                            new InternetAddress(to.trim()));
                }
                List<String> copyToList = sendMailConfigModel.getCopyToList();
                if(copyToList!=null&&copyToList.size()>0) {
                    Address[] addresses = new Address[copyToList.size()];
                    int i = 0;
                    for (String copy : copyToList) {
                        addresses[i] = new InternetAddress(copy);
                        i++;
                    }
                    message.addRecipients(Message.RecipientType.CC, addresses);
                }
                message.setSubject(subject);
                MimeBodyPart mbp1 = new MimeBodyPart(); // text
                mbp1.setContent(content, "text/html;charset=utf-8");
                Multipart mp = new MimeMultipart(); // Whole message: body + attachment
                mp.addBodyPart(mbp1);
                message.setContent(mp);
                message.setSentDate(new Date());
                message.saveChanges();
                Transport trans = session.getTransport("smtp");
//                trans.connect("smtp.163.com", sendMailConfigModel.getUser(), sendMailConfigModel.getPassword());
                trans.send(message);
                System.out.println(message.toString());
            } catch (Exception e){
                e.printStackTrace();
                return "failure";
            }
            return "success";
        }else{
            return "failure";
        }
    }



    public static String SendExcelMail(String  subject,List<List<String>> conteList) throws Exception {
        String htmlStr ="";
        for(int i=0;i<conteList.size();i++){
            htmlStr = createHTML(htmlStr,conteList.get(i),i==0?true:false,i==conteList.size()-1?true:false);
        }
       String result =  sendMail(subject,htmlStr);
        return result;
    }



    private static String createHTML(String originHtml, List<String> data,boolean headFlage,boolean endFlage) {
        String html_msg="";
        if(headFlage){
            html_msg = "<table border=\"1\" width='80%' height='80'>";
            html_msg = html_msg+"<tr bgcolor='#B6DDE6'>";
            for(int column=0;column<data.size();column++){
                html_msg = html_msg +"<td width='12%'>"+data.get(column)+"</td>";
            }
            html_msg = html_msg+"</tr>";
        }else{
            html_msg = html_msg+"<tr>";
            for(int column=0;column<data.size();column++){
                html_msg = html_msg +"<td>"+data.get(column)+"</td>";
            }
            html_msg = html_msg+"</tr>";
        }
        if(endFlage){
            html_msg = html_msg + "</table>";
        }
        return originHtml+html_msg;
    }
}

Profile content:

sendMail.mail_smtp_host=smtp.163.com
sendMail.mail_smtp_auth=login
sendMail.user=***@163.com
sendMail.password=******
sendMail.from=*****@163.com
sendMail.to=auguszero@163.com

excel email sending example:

If you think you can package the project directly in github and introduce it as a tool class.

At the same time, welcome to improve the github project.
Welcome to exchange:

Topics: Java Session Excel github