Implementation ideas
First, set the relevant attributes of the mail server, including whether authentication is required, the mail protocol used, the server address, the port, etc.
Create a session object, use the method, and provide the attributes and authentication information of the mail server.
Create a MimeMessage object as a mail message, and set sender, recipient, email subject and other information.
To send HTML content, use the setContent method and specify the content type as text/html.
For attachments, create a MimeMultipart object, add the HTML content as a MimeBodyPart, and add the attachment as a MimeBodyPart, and use the attachFile method to add the attachment.
Finally, use the method to send the email.
Complete code
Here is a complete Java code example:
import .*; import .*; import ; import ; public class HtmlAndAttachmentEmailSender { public static void main(String[] args) { // Mail server properties settings Properties properties = new Properties(); ("", "true"); ("", "true"); ("", ""); ("", "587"); // The sender's email account and password String senderEmail = "your_email@"; String senderPassword = "your_password"; // Create a session object Session session = (properties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(senderEmail, senderPassword); } }); try { // Create a mail message object MimeMessage message = new MimeMessage(session); (new InternetAddress(senderEmail)); (, ("recipient_email@")); ("HTML content with attachments"); // Create a multi-part email content object Multipart multipart = new MimeMultipart(); // Part 1: HTML content BodyPart htmlPart = new MimeBodyPart(); String htmlContent = "<html><body><h1>Hey, this is an HTML email with attachments!</h1><p>Isn't it great?</p></body></html>"; (htmlContent, "text/html; charset=utf-8"); (htmlPart); // Part 2: Attachment BodyPart attachmentPart = new MimeBodyPart(); File file = new File("path/to/your/"); // Replace it with the actual path of the attachment you want to add (file); (attachmentPart); // Set multi-part content into email messages (multipart); // Send email (message); ("HTML content with attachments was sent successfully!"); } catch (MessagingException | e) { (); ("Email sent failed!"); } } }
Code explanation
How about it, friends, isn’t it very simple? Use this code in your project quickly to make your email richer and more powerful. However, during use, remember to handle exceptions to prevent accidents. Ask me any questions at any time, and I will help you solve them!
Properties class: Used to store attributes of the mail server, such as whether authentication is required, whether TLS secure connection is enabled, it is the host name of the mail server and the port number.
Session class: Represents a mail session, generated using methods based on properties and Authenticator, which provides the sender's authentication information.
MimeMessage class: represents a mail message, created with session, set the sender through setFrom, set the recipient by setRecipients, and setSubject to set the topic.
Multipart class: represents the multipart content of the email, which is implemented using MimeMultipart.
MimeBodyPart class: represents part of the message, which can be HTML text or attachment. For the HTML part, use the setContent method to add HTML content and specify the content type as text/html; for the attachment part, use the attachFile method to add attachments.
Method: Finally send an email.
Instructions for use
Make sure you replace your_email@ in the code with the sender's real email address and your_password with the sender's email password.
Replace recipient_email@ with the recipient's real email address.
Replace path/to/your/ with the actual path of the attachment you want to add.
When running the code, if an exception is encountered, the exception message will be printed and the "Email sent failed!" will be output; if the sending is successful, the "HTML content and email sent with attachments will be successfully sent!".
This is the article about Java implementing sending HTML content and emails with attachments. For more related Java sending emails, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!