使用servlet发送电子邮件作为附件,附件应该只有文件名

时间:2015-04-22 22:39:57

标签: java

在此示例中,我尝试使用gmail smtp服务器通过邮件发送附件。

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. 要求是我希望附件名称只是文件名。
  2. 在此示例中,当我检查邮件时,附件将具有绝对路径。
  3. 这是我在附件“D:/Self_Learning/Servlets/Servlet_Examples/WebContent/CSS/Note.txt”中收到的文件名
  4. 但我希望附件只是Note.txt
  5. 请让我知道为了获得所需的输出必须进行哪些更改

1 个答案:

答案 0 :(得分:1)

您使用filename= \".....\"键值对在标题中设置文件名。只需将此值更改为您要设置的名称即可。

例如:

 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+"\"");