
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to send a email with attachment using a JSP page?
Following is an example to send an email with attachment from your machine −
Example
<%@ 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>
Let us now run the above JSP to send a file as an attachment along with a message on a given email ID.
User Authentication Part
If it is required to provide user ID and Password to the email server for authentication purpose, then you can set these properties as follows −
Output
props.setProperty("mail.user", "myuser"); props.setProperty("mail.password", "mypwd");
- Related Questions & Answers
- How to send a html based email using a JSP page?
- How to send a simple text based email using a JSP page?
- How to send an email with a file attachment in Android?
- How to send an email with a file attachment in Android using Kotlin?
- How to send an attachment in email using Swift?
- How to send an attachment in email using Swift(ios)?
- How to send a file as an email attachment using the Linux command line?
- Sending an Attachment with email using Perl
- How to send email using PHPMailer?
- How to send email using PowerShell?
- How to send a report through email using Selenium Webdriver?
- Send email using Java Program
- Send mail with attachment from your Gmail account using Python
- How to create a link to send email with a subject in HTML?
- How to create a common error page using JSP?
Advertisements