java 发邮件带excel附件,以流的形式发送附件,不生成excel文件

最近项目中是有个发送邮件的需求,但是需要在附件中携带excel附件,这里有两种实现方式,一种是先生成文件,然后发送附件, 一种是不借助文件直接通过流的形式发送附件,这博文主要介绍通过流的方式发送excel 附件:

1、发送邮件service

package com.jack.bid.email.service;


import java.io.ByteArrayInputStream;
import java.security.GeneralSecurityException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.util.ByteArrayDataSource;

import com.sun.mail.util.MailSSLSocketFactory;
import com.jack.common.utils.PropertiesUtils;
import org.springframework.stereotype.Service;

@Service
public class BaseMail {

    public static Properties email = PropertiesUtils.getProperties("email.properties");
    public static String send_email_account = email.getProperty("send_email_account");
    public static String send_email_pwd = email.getProperty("send_email_pwd");
    public static String email_host = email.getProperty("email_host");

    /**
     * 发送邮件
     *
     * @param to        邮件收件人地址
     * @param title     邮件标题
     * @param text      内容
     * @param text      附件标题
     * @param
     */
    public void sendMsgFileDs(String to, String title, String text,String affixName, ByteArrayInputStream inputstream) {

        Session session = assembleSession();
        Message msg = new MimeMessage(session);
        try {
            msg.setFrom(new InternetAddress(send_email_account));
            msg.setSubject(title);
            msg.setRecipients(RecipientType.TO, acceptAddressList(to));
            MimeBodyPart contentPart = (MimeBodyPart) createContent(text, inputstream,affixName);//参数为正文内容和附件流
            MimeMultipart mime = new MimeMultipart("mixed");
            mime.addBodyPart(contentPart);
            msg.setContent(mime);
            Transport.send(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Address[] acceptAddressList(String acceptAddress) {
        // 创建邮件的接收者地址,并设置到邮件消息中
        Address[] tos = null;
        try {
            tos = new InternetAddress[1];
            tos[0] = new InternetAddress(acceptAddress);
        } catch (AddressException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return tos;
    }

    public Session assembleSession() {
        Session session = null;
        Properties props = new Properties();
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.smtp.port", "465");
        props.setProperty("mail.smtp.host", email_host);//邮件服务器
        //开启安全协议
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
        } catch (GeneralSecurityException e1) {
            e1.printStackTrace();
        }
        props.put("mail.smtp.ssl.socketFactory", sf);
        props.put("mail.smtp.ssl.enable", "true");
        session = Session.getDefaultInstance(props, new MyAuthenricator(send_email_account, send_email_pwd));
        return session;
    }

    static Part createContent(String content, ByteArrayInputStream inputstream, String affixName) {
        MimeBodyPart contentPart = null;
        try {
            contentPart = new MimeBodyPart();
            MimeMultipart contentMultipart = new MimeMultipart("related");
            MimeBodyPart htmlPart = new MimeBodyPart();
            htmlPart.setContent(content, "text/html;charset=gbk");
            contentMultipart.addBodyPart(htmlPart);
            //附件部分
            MimeBodyPart excelBodyPart = new MimeBodyPart();
            DataSource dataSource = new ByteArrayDataSource(inputstream, "application/excel");
            DataHandler dataHandler = new DataHandler(dataSource);
            excelBodyPart.setDataHandler(dataHandler);
            excelBodyPart.setFileName(MimeUtility.encodeText(affixName));
            contentMultipart.addBodyPart(excelBodyPart);
            contentPart.setContent(contentMultipart);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return contentPart;
    }

    //用户名密码验证,需要实现抽象类Authenticator的抽象方法PasswordAuthentication
    static class MyAuthenricator extends Authenticator {
        String u = null;
        String p = null;

        public MyAuthenricator(String u, String p) {
            this.u = u;
            this.p = p;
        }

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(u, p);
        }
    }

}
  

2、相关配置

email_host = smtp.exmail.qq.com
send_email_account=*********
send_email_pwd=*******

利用ByteArrayOutputStream把excel文件输出到bytes[]中,然后由ByteArrayResource包装起来传递给邮件服务。;

3、我发送的内容就是我上篇博客中,导出的excel的内容,下面先写一个简单的测试类

	@Test
	public void sendMailWithExcel() throws IOException {

    @Autowired
    private BaseMail mailService;

		String[] headers = {"col1","col2","col3"};
		// 声明一个工作薄
		HSSFWorkbook wb = new HSSFWorkbook();
		// 生成一个表格
		HSSFSheet sheet = wb.createSheet();
		HSSFRow row = sheet.createRow(0);
		for (int i = 0; i < headers.length; i++) {
			HSSFCell cell = row.createCell(i);
			cell.setCellValue(headers[i]);
		}
		int rowIndex = 1;
 
		for(int j=0; j<3; j++){
			row = sheet.createRow(rowIndex);
			rowIndex++;
			HSSFCell cell1 = row.createCell(0);
			cell1.setCellValue(j);
			cell1 = row.createCell(1);
			cell1.setCellValue(j+1);
			cell1 = row.createCell(2);
			cell1.setCellValue(j+2);
		}
		for (int i = 0; i < headers.length; i++) {
			sheet.autoSizeColumn(i);
		}
 
		ByteArrayOutputStream os = new ByteArrayOutputStream(1000);
		wb.write(os);
		wb.close();
 
		ByteArrayInputStream iss = new ByteArrayInputStream(os.toByteArray());
		os.close();
 
		mailService.sendMsgFileDs("xjj@qq.com",
				"attachmentMail subject",
				"I have an attachment",
                "attachment name",
				iss);
 注意:附件需要带后缀,例如上面例子,附件名称可以写 测试.xlsx
	}
}

4、这样就可以 不生成中间文件直接 发送附件

  • 9
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 22
    评论
您好!要在Java后台以附件的方式发送Excel邮件而不生成实体文件,您可以使用Apache POI库来生成Excel文件,并使用JavaMail发送邮件。以下是一个示例代码: ```java import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; public class ExcelEmailSender { public static void sendExcelEmail(Session session, String from, String to, String subject, Workbook workbook) throws MessagingException, IOException { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject(subject); Multipart multipart = new MimeMultipart(); // 创建Excel文件的字节数组输出流 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); workbook.write(outputStream); // 创建BodyPart并将Excel文件字节数组添加到BodyPart中 BodyPart bodyPart = new MimeBodyPart(); DataSource dataSource = new ByteArrayDataSource(outputStream.toByteArray(), "application/vnd.ms-excel"); bodyPart.setDataHandler(new DataHandler(dataSource)); bodyPart.setFileName("excel.xlsx"); // 将BodyPart添加到Multipart中 multipart.addBodyPart(bodyPart); // 设置邮件内容 message.setContent(multipart); // 发送邮件 Transport.send(message); } public static void main(String[] args) { String from = "your_email@example.com"; String to = "recipient_email@example.com"; String subject = "Excel Email"; // 使用Apache POI库创建Excel文件 Workbook workbook = WorkbookFactory.create(); // 在这里进行Excel文件的操作,例如创建工作表、添加数据等 // 设置JavaMail会话 Session session = Session.getInstance(System.getProperties()); try { sendExcelEmail(session, from, to, subject, workbook); System.out.println("Excel email sent successfully."); } catch (MessagingException | IOException e) { e.printStackTrace(); } finally { // 关闭工作簿 workbook.close(); } } } ``` 请确保您已将正确的发件人和收件人电子邮件地址替换到代码中的 `from` 和 `to` 变量中。此外,确保已添加Apache POI和JavaMail库到您的项目依赖中。 此代码将在Java后台创建一个Excel文件,并将其作为附件添加到电子邮件中,而不会生成实体文件。然后,使用JavaMail发送邮件。 希望对您有所帮助!如有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

g-Jack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值