Wednesday, July 16, 2014

Send attachment in java mail

How can i send attachment in java mail ? You can use java mail API, if you don't have got yet you can download from this link java mail API and add your project library.

public static void main(String[] args) throws MessagingException, IOException {

        String filename = "fatura.txt";

//        dosya oluşturma ve içerisine bilgi girme
        String text = "fatura bilgileri dosya içi";
        try {
            File file = new File(filename);
            BufferedWriter output = new BufferedWriter(new FileWriter(file));
            output.write(text);
            output.close();
        } catch ( IOException e ) {
            LOGGER.error("dosya oluşturulamadı");
        }


        Properties props = System.getProperties();
        javax.mail.Session session = javax.mail.Session.getDefaultInstance(props, null);


        MimeMessage message = new MimeMessage(session);
        message.setSubject("Fatura Bilgileri");
        message.setSentDate(new Date());

        // E-mail body kısmı

        MimeBodyPart messagePart = new MimeBodyPart();
        messagePart.setText("Tarih :" + new Date());

        // E-mail attachment kısmı

        MimeBodyPart attachmentPart = new MimeBodyPart();
        FileDataSource fileDataSource = new FileDataSource(filename) {
            @Override
            public String getContentType() {
                return "application/octet-stream";
            }
        };
        attachmentPart.setDataHandler(new DataHandler(fileDataSource));
        attachmentPart.setFileName(fileDataSource.getName());

        // bütün partları ekle

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messagePart);
        multipart.addBodyPart(attachmentPart);
        message.setContent(multipart);

        sendMail(message);

    }

Send mail method



public static void sendMail(MimeMessage mail) throws MessagingException {

        String host = "yourhostname";
        String username = "ibrahim.kiraz";
        Properties props = new Properties();

        props.setProperty("mail.smtp.host", host);
        props.put("mail.smtp.starttls.enable", "false");
        props.setProperty("mail.smtp.auth", "true"); // s
        javax.mail.Session session = javax.mail.Session.getInstance(props);
       
 // mesaj content kısmı

        Transport t = session.getTransport("smtp");
        Address fromUser = new InternetAddress("\"İbrahim Kiraz\"ibrahimkiraz@abc.com");
        Address toUser = new InternetAddress("ibrahim.kiraz@xxx.com.tr");
        mail.setFrom(fromUser);
        mail.setRecipient(Message.RecipientType.TO, toUser);


        try {
            t.connect(host, username);
            t.sendMessage(mail, mail.getAllRecipients());
            LOGGER.info("Mail başarıyla gönderildi");
        } catch (MessagingException e) {
            LOGGER.error("Mail gönderilemedi", e);
        } finally {
            t.close();
        }

    }

No comments:

Post a Comment

How can we use svg file as a icon of v-btn (vuetify button) ?

<template>   <v-btn>     <CustomIcon />     Click me   </v-btn> </template> <script> // Import your SVG ...