简体   繁体   中英

Send an email as attachment using servlets and attachment should have just file name

In this example Im trying to send an attachment via mail using gmail smtp server.

Mailer.java

package com.servlet.mail;

import java.io.File;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;

public class Mailer {
    public static  void send(String to ,String subject ,String msg)
    {
        System.out.println("in Mailer class");
        final String user="sup.ni@gmail.com";
        final String psw="XXXXXX";//changes to be made accoordingly


        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");



        Session session = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(user,psw);
                    }
                });


        try
        {
            System.out.println(to+""+subject+""+msg);

            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(user));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);
            //message.setText(msg);


            //Mail sending with attachement



             BodyPart msgBodyPart1 = new MimeBodyPart();  
             msgBodyPart1.setText("This is message body");

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

            MimeBodyPart msgBodyPart2 = new MimeBodyPart();


            String path = "D:/Self_Learning/Servlets/Servlet_Examples/WebContent/CSS/";
            String filename= path+"Note.txt";

            System.out.println(path);


            message.setHeader("Content-Disposition", "attachement ; filename= \""+filename+"\"");
            DataSource src = new FileDataSource(filename);
            msgBodyPart2.setDataHandler(new DataHandler(src));
            msgBodyPart2.setFileName(path);
            multipart.addBodyPart(msgBodyPart2);

            message.setContent(multipart);

            //SEND MSG
            Transport.send(message);
            System.out.println("Mail sent successfully");

        }
        catch(MessagingException e)
        {
         System.out.println(e);
        }
    }
}
  1. The requirement is that I want the attachment name to be just the file name.
  2. Here in this example when I check the mail the attachment will have absolute path.
  3. This is the file-name I receive in the attached file "D:/Self_Learning/Servlets/Servlet_Examples/WebContent/CSS/Note.txt"
  4. But I want the attached file to be just Note.txt
  5. Please let me know what is changes has to be done in order to get my required output

You're setting the filename in the header using the filename= \\".....\\" key-value pair. Just change this value to the name you want to set.

For example:

 String path = "D:/Self_Learning/Servlets/Servlet_Examples/WebContent/CSS/";
 String name = "Note.txt";
 String filename= path+name;
 message.setHeader("Content-Disposition", "attachement ; filename= \""+name+"\"");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM