如何使用Java将文件附加到电子邮件?

时间:2016-03-05 23:49:53

标签: java email javamail

大家好,我创建了一个简单的表单,其中包含了所有电子邮件信息 此代码完美无缺,但我想附上一条带有消息的文件 我该怎么做才能添加到此代码中,以便我可以附加我的文件? 这是代码:

public class UserMailDispatcher extends HttpServlet {

    @EJB
    private MailSenderBean mailSender;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, MessagingException {
        response.setContentType("text/html;charset=UTF-8");


        String toEmail = request.getParameter("email");
        String subject = request.getParameter("subject");
        String message = request.getParameter("message");

        String fromEmail = "example.gmtt@gmail.com";
        String username = "example.gmtt";
        String password = "example";


        try (PrintWriter out = response.getWriter()) {


            mailSender.sendEmail(fromEmail, username, password, toEmail, subject, message);
}
}
} 

@Stateless
public class MailSenderBean {

    public void sendEmail(String fromEmail, String username, String password, String toEmail, String subject, String message) throws MessagingException{

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


        Session mailSession = Session.getDefaultInstance(props, null);
        mailSession.setDebug(true);

        Message mailMessage = new MimeMessage(mailSession);

        mailMessage.setFrom(new InternetAddress(fromEmail));
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
        mailMessage.setContent(message, "text/html");       
        mailMessage.setSubject(subject);

        Transport transport = mailSession.getTransport("smtp");
        transport.connect("smtp.gmail.com", username, password);

        transport.sendMessage(mailMessage, mailMessage.getAllRecipients());


    }
}

1 个答案:

答案 0 :(得分:1)

尝试此更新功能:

@Stateless
public class MailSenderBean {

public void sendEmail(String fromEmail, String username, String password, String toEmail, String subject, String message) throws MessagingException{

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


    Session mailSession = Session.getDefaultInstance(props, null);
    mailSession.setDebug(true);

    Message mailMessage = new MimeMessage(mailSession);

    mailMessage.setFrom(new InternetAddress(fromEmail));
    mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
    mailMessage.setContent(message, "text/html");       
    mailMessage.setSubject(subject);

    Transport transport = mailSession.getTransport("smtp");
    transport.connect("smtp.gmail.com", username, password);

    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText("File attached:");
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    messageBodyPart = new MimeBodyPart();

    DataSource source = new FileDataSource("location\of\file");
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(filename);
    multipart.addBodyPart(messageBodyPart);
    mailMessage.setContent(multipart);

    transport.sendMessage(mailMessage, mailMessage.getAllRecipients());


}
}