Computer >> Máy Tính >  >> Lập trình >> Lập trình

Làm cách nào để gửi email có tệp đính kèm bằng trang JSP?

Sau đây là một ví dụ để gửi một email có tệp đính kèm từ máy của bạn -

Ví dụ

<%@ page import = "java.io.*,java.util.*,javax.mail.*"%>
<%@ page import = "javax.mail.internet.*,javax.activation.*"%>
<%@ page import = "javax.servlet.http.*,javax.servlet.*" %>
<%
   String result;

   // Recipient's email ID needs to be mentioned.
   String to = "abcd@gmail.com";

   // Sender's email ID needs to be mentioned
   String from = "mcmohd@gmail.com";

   // Assuming you are sending email from localhost
   String host = "localhost";

   // Get system properties object
   Properties properties = System.getProperties();

   // Setup mail server
   properties.setProperty("mail.smtp.host", host);

   // Get the default Session object.
   Session mailSession = Session.getDefaultInstance(properties);

   try {
      // Create a default MimeMessage object.
      MimeMessage message = new MimeMessage(mailSession);

      // Set From: header field of the header.
      message.setFrom(new InternetAddress(from));

      // Set To: header field of the header.
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

      // Set Subject: header field
      message.setSubject("This is the Subject Line!");

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

      // Fill the message
      messageBodyPart.setText("This is message body");

      // Create a multipart message
      Multipart multipart = new MimeMultipart();

      // Set text message part
      multipart.addBodyPart(messageBodyPart);

      // Part two is attachment
      messageBodyPart = new MimeBodyPart();

      String filename = "file.txt";
      DataSource source = new FileDataSource(filename);
      messageBodyPart.setDataHandler(new DataHandler(source));
      messageBodyPart.setFileName(filename);
      multipart.addBodyPart(messageBodyPart);

      // Send the complete message parts
      message.setContent(multipart );

      // Send message
      Transport.send(message);
      String title = "Send Email";
      result = "Sent message successfully....";
   } catch (MessagingException mex) {
      mex.printStackTrace();
      result = "Error: unable to send message....";
   }
%>
<html>
   <head>
      <title>Send Attachment Email using JSP</title>
   </head>
   <body>
      <center>
         <h1>Send Attachment Email using JSP</h1>
      </center>
      <p align = "center">
         <%out.println("Result: " + result + "\n");%>
      </p>
   </body>
</html>

Bây giờ chúng ta hãy chạy JSP ở trên để gửi một tệp dưới dạng tệp đính kèm cùng với thư trên một ID email nhất định.

Phần xác thực người dùng

Nếu được yêu cầu cung cấp ID người dùng và Mật khẩu cho máy chủ email nhằm mục đích xác thực, thì bạn có thể đặt các thuộc tính này như sau -

Đầu ra

props.setProperty("mail.user", "myuser");
props.setProperty("mail.password", "mypwd");