Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

sending a excel in an attachment in email java

    // Define message
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.addRecipient(Message.RecipientType.TO,
      new InternetAddress(to));
    message.setSubject("Hello JavaMail Attachment");

    // Create the message part
    BodyPart messageBodyPart = new MimeBodyPart();

    // Fill the message
    messageBodyPart.setText("Pardon Ideas");

    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);

    // Part two is attachment
    messageBodyPart = new MimeBodyPart();
    DataSource source = new FileDataSource(filename);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(filename);
    multipart.addBodyPart(messageBodyPart);

    // Put parts in message
    message.setContent(multipart);

    // Send the message
    Transport.send(message);
Comment

sending a excel in an attachment in email java

ByteArrayOutputStream bos = new ByteArrayOutputStream();
xlsFile.write(bos); // write excel data to a byte array
fos.close();

// Now use your ByteArrayDataSource as
DataSource fds = new ByteArrayDataSource(bos.toByteArray(), "application/vnd.ms-excel");
Comment

sending a excel in an attachment in email java

Workbook xlsFile = new HSSFWorkbook(); // create a workbook
CreationHelper helper = xlsFile.getCreationHelper();
Sheet sheet1 = xlsFile.createSheet("Sheet #1"); // add a sheet to your workbook

while(rs.next())
{
 Row row = sheet1.createRow((short)0); // create a new row in your sheet
 for(int i = 0; i < 12; i++)
 {
   row.createCell(i).setCellValue(
     helper.createRichTextString(exceldata)); // add cells to the row
 }
} 

// Write the output to a temporary excel file
FileOutputStream fos = new FileOutputStream("temp.xls");
xlsFile.write(fos);
fos.close();

// Switch to using a `FileDataSource` (instead of ByteArrayDataSource)
DataSource fds = new FileDataSource("temp.xls");
Comment

PREVIOUS NEXT
Code Example
Java :: how to get filename without extension in java 
Java :: java.lang.classcastexception: java.lang.string cannot be cast to java.util.arraylist 
Java :: replace everything before a character in java 
Java :: Java Creating an EnumMap 
Java :: get distinct values from list of objects java 
Java :: string vs new string 
Java :: random suffling java 
Java :: Java Remove ArrayList Elements 
Java :: mergesort java 
Java :: remove duplicates from arraylist in android 
Java :: stringbuilder in java 
Java :: string into ascii in java 
Java :: convert from integer to character java 
Java :: java how to create subclass 
Java :: interface vs abstract class java 
Java :: stream reduce 
Java :: format specifier in java 
Java :: run specific test case junit 
Java :: hash map 
Java :: method overriding in java 
Java :: spring boot basic authentication 
Java :: abstract class java 
Java :: Service vs IntentService. 
Java :: @Async how it works @EnableAsync 
Java :: assign a random number in a set without replacement javva 
Java :: Java Create a WeakHashMap 
Java :: worldedit api copy schematic 
Java :: what is java.io example 
Java :: how to remove all components from layeredPane java 
Java :: calculate tip and sales tax function 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =