亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JAVA數(shù)據(jù)寫(xiě)入生成excel文件和發(fā)送郵件

 更新時(shí)間:2024年06月01日 11:44:31   作者:我就吃最后一口  
這篇文章主要介紹了JAVA數(shù)據(jù)寫(xiě)入生成excel文件和發(fā)送郵件,流程:先導(dǎo)包 => 郵箱開(kāi)啟配置 => java寫(xiě)好配置類 => 測(cè)試發(fā)送 => 數(shù)據(jù)寫(xiě)入excel => 郵件帶附件發(fā)送

JAVA數(shù)據(jù)寫(xiě)入生成excel文件和發(fā)送郵件流程:

? 先導(dǎo)包 => 郵箱開(kāi)啟配置 => java寫(xiě)好配置類 => 測(cè)試發(fā)送 => 數(shù)據(jù)寫(xiě)入excel => 郵件帶附件發(fā)送

郵箱jar包

<dependencies>
    <dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
    </dependency>
</dependencies>

郵箱開(kāi)啟配置

我這個(gè)是163的,開(kāi)啟之后會(huì)生成授權(quán)碼,記得復(fù)制粘貼到自己的項(xiàng)目配置文件里面去

郵箱配置類

public final class JavaMailUtil {
    //文件地址 上面是linux地址,下面是我自己本地測(cè)試用的
    //public final static String FILEPATH = "/wenjie/javaProject/bomexcelfiles";
    public final static String FILEPATH = "e:/Users/liuwenj/Desktop/";
    
    private JavaMailUtil() {
    }
    public static Session createSession() {
        //賬號(hào)信息
        String username = "";//郵箱發(fā)送賬號(hào)
        String password = "";//郵箱授權(quán)碼
        //創(chuàng)建一個(gè)配置文件,并保存
        Properties props = new Properties();
        //SMTP服務(wù)器連接信息
        //126——smtp.126.com
        //163——smtp.163.com
        props.put("mail.smtp.host", "smtp.126.net");//SMTP主機(jī)名
        //126——25
        //163——645 如果645一直連不上,可以換成25試一試
        props.put("mail.smtp.port", "25");// 主機(jī)端口號(hào)
        props.put("mail.smtp.auth", "true");// 是否需要用戶認(rèn)證
        props.put("mail.smtp.starttls.enable", "true");// 啟用TlS加密
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // TODO Auto-generated method stub
                return new PasswordAuthentication(username, password);
            }
        });
        //控制臺(tái)打印調(diào)試信息
        session.setDebug(true);
        return session;
    }
}

測(cè)試發(fā)送純文本

//創(chuàng)建Session會(huì)話
Session session = JavaMailUtil.createSession();

//創(chuàng)建郵件對(duì)象
MimeMessage message = new MimeMessage(session);
message.setSubject("主題");
message.setFrom(new InternetAddress("發(fā)送者郵箱"));
//發(fā)送給一個(gè)人
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("接收者郵箱"));
//發(fā)送給多個(gè)人
//message.setRecipients(MimeMessage.RecipientType.CC, new InternetAddress[] {new InternetAddress("接收者郵箱")});

//純文本信息
message.setText("文本信息:來(lái)自于公司PLM系統(tǒng)測(cè)試");
//發(fā)送
Transport.send(message);

數(shù)據(jù)寫(xiě)入excel,并生成文件

先導(dǎo)jar

<!-- Apache POI -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.2.3</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.3</version>
</dependency>

寫(xiě)入數(shù)據(jù)到excel

Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Bom");
//第一行的數(shù)據(jù)名
List<String> labelList = Arrays.asList("物料編碼", "描述");
Row row = sheet.createRow(0);
for (int i = 0; i < labelList.size(); i++) {
    row.createCell(i).setCellValue(labelList.get(i));
}
int rowNo = 1;
//獲取數(shù)據(jù)然后放入Excel
List<Map<String, Object>> bomList = plmDataService.getBomNotPerfect();
for (Map<String, Object> bom : bomList) {
    String no = (String) bom.get("ITEM_NUMBER");
    String desc = (String) bom.get("DESCRIPTION");
    Row tempRow = sheet.createRow(rowNo);
    rowNo++;

    tempRow.createCell(0).setCellValue(no);
    tempRow.createCell(1).setCellValue(desc);
}
// 寫(xiě)入數(shù)據(jù)到Excel
try (FileOutputStream outputStream = new FileOutputStream(JavaMailUtil.FILEPATH + fileName)) {
    workbook.write(outputStream);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        workbook.close(); //關(guān)閉工作簿
    } catch (IOException e) {
        e.printStackTrace();
    }
}

一切準(zhǔn)備就緒之后就可以發(fā)送郵件了

發(fā)送郵件(帶附件)

//創(chuàng)建會(huì)話
Session session = JavaMailUtil.createSession();

//創(chuàng)建郵件對(duì)象
MimeMessage message = new MimeMessage(session);
message.setSubject("主題");
message.setFrom(new InternetAddress("發(fā)送者郵箱"));
//發(fā)送給一個(gè)人
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("接收者郵箱"));
//發(fā)送給多個(gè)人
//message.setRecipients(MimeMessage.RecipientType.CC, new InternetAddress[] {new InternetAddress("接收者郵箱")});

//郵件主體
BodyPart textPart = new MimeBodyPart();
textPart.setContent("文本信息:來(lái)自于公司PLM系統(tǒng)測(cè)試", "text/html;charset=utf-8");

//郵件附件
BodyPart filePart = new MimeBodyPart();
filePart.setFileName(fileName);

//提交附件文件
filePart.setDataHandler(new DataHandler(new ByteArrayDataSource(Files.readAllBytes(Paths.get(JavaMailUtil.FILEPATH + fileName)), "application/octet-stream")));

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(textPart);
multipart.addBodyPart(filePart);

//將郵件裝入信封
message.setContent(multipart);
//發(fā)送
Transport.send(message);

如果有發(fā)送一些特殊需求,比如需要內(nèi)嵌圖片HTML

發(fā)送郵件(內(nèi)嵌圖片HTML)

PS: 沒(méi)有試過(guò),copy的代碼,貼上去以后如果有需求了再驗(yàn)證

Session session = JavaMailUtils.createSession(); //創(chuàng)建session對(duì)象
MimeMessage message = new MimeMessage(session); //創(chuàng)建message對(duì)象
message.setSubject("測(cè)試郵件"); //設(shè)置郵件標(biāo)題
message.setFrom(new InternetAddress("xxxxxx@163.com")); //設(shè)置發(fā)送方地址
message.setRecipient(RecipientType.TO, new InternetAddress("xxxxxx@qq.com")); //設(shè)置接收方地址
message.setRecipients(RecipientType.CC, new InternetAddress[] {new InternetAddress("xxxxxx@qq.com"),new InternetAddress("xxxxxx@qq.com")}); //群發(fā)(抄送多人)
//正文
BodyPart textPart=new MimeBodyPart();
StringBuilder contentText=new StringBuilder();
contentText.append("<h3>網(wǎng)易郵箱/h3>");
contentText.append("<p>給QQ郵箱發(fā)消息了!</p>");
contentText.append("<img src=\"cid:xxx\"/>");  
textPart.setContent(contentText.toString(),"text/html;charset=utf-8");
//附件
BodyPart imagePart=new MimeBodyPart();  
imagePart.setDataHandler(new DataHandler(
    new ByteArrayDataSource(Files.readAllBytes(Paths.get("D://test//1.jpg")), "application/octet-stream"))); //
imagePart.setHeader("Content-ID", "xxx"); //圖片的內(nèi)容ID
Multipart multipart=new MimeMultipart(); //創(chuàng)建multipart對(duì)象
multipart.addBodyPart(textPart); //將textPart對(duì)象放入multipart
multipart.addBodyPart(filePart); //將filePartt對(duì)象放入multipart
message.setContent(multipart); //將multipart對(duì)象放入郵件
Transport.send(message); //發(fā)送郵件

 

到此這篇關(guān)于JAVA數(shù)據(jù)寫(xiě)入生成excel文件和發(fā)送郵件的文章就介紹到這了,更多相關(guān)JAVA生成excel和發(fā)送郵件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論