Code:
java
package
com.kartik.mail.send; 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.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.MimeMultipart; public class
SendInlineImagesInEmails { public static void sendMail(String
htmlContent, String imagePath) { // Recipient's email ID String to =
"kmandal@gmail.com"; // Sender's email ID String from =
"kmandal@kmical.com"; final String username =
"kmandal"; // change
accordingly final String password =
"XXXXXXX"; // change
accordingly String host =
"XXXXXXXXXX"; // change
accordingly // Set up the SMTP server properties Properties props = new Properties(); props.put("mail.smtp.auth",
"true");
props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host",
host); props.put("mail.smtp.port",
"25"); // Create a session with
authentication Session session =
Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication
getPasswordAuthentication() { return new
PasswordAuthentication(username, password); } }); try { // Create a default MimeMessage
object MimeMessage message = new
MimeMessage(session); message.setFrom(new
InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new
InternetAddress(to)); message.setSubject("Embedded
Image Email"); // Create a multipart message for
the email content Multipart multipart = new
MimeMultipart("related"); // First part: HTML content BodyPart messageBodyPart = new
MimeBodyPart();
messageBodyPart.setContent(htmlContent, "text/html");
messageBodyPart.setDisposition(BodyPart.INLINE);
multipart.addBodyPart(messageBodyPart); // Second part: Image attachment messageBodyPart = new
MimeBodyPart(); File emailImage = new
File(imagePath); if (emailImage.exists()) { DataSource fds = new
FileDataSource(emailImage);
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID",
"<image>");
multipart.addBodyPart(messageBodyPart); } else {
System.out.println("Image file not found: " + imagePath); return; } // Combine both parts into the
message message.setContent(multipart); // Send the message Transport.send(message); System.out.println("Sent
message successfully...."); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } public static void main(String[] args) { // HTML content with image reference String htmlContent =
"<html><body><h1>Hello</h1>" + "<img
src=\"cid:image\" style=\"display: block; margin-left: auto;
margin-right: auto;\" width=\"200\" height=\"200\"
/>" +
"</body></html>"; // Call the sendMail method with the
HTML content and image path sendMail(htmlContent,
"d:\\chemical\\_Electron.png"); } } |
The provided Java program is designed to send an email with
an inline image. The email is composed using JavaMail API, where the HTML
content and the image are embedded directly into the email.
Code Walkthrough:
- Setting
up email properties: The email configuration, including authentication
and SMTP server details, is set up in this section:
java
Properties
props = new Properties(); props.put("mail.smtp.auth",
"true"); props.put("mail.smtp.starttls.enable",
"true"); props.put("mail.smtp.host",
host); props.put("mail.smtp.port",
"25"); |
Here, the host, username, and password need to be replaced
with actual SMTP server credentials.
- Creating
the email: A MimeMessage is created and filled with content (HTML and
image) using a multipart MIME structure.
Java
MimeMessage
message = new MimeMessage(session); message.setFrom(new
InternetAddress(from)); message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to)); message.setSubject("DDDD");
Multipart
multipart = new MimeMultipart("related"); |
- Adding
the HTML content: The HTML part of the email is added as a BodyPart.
This part references the inline image with the cid:image identifier.
Java
BodyPart
messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent(data,
"text/html"); messageBodyPart.setDisposition(BodyPart.INLINE); |
- Embedding
the image: The image is attached as another BodyPart. It is identified
in the HTML by the cid:image header.
java
messageBodyPart
= new MimeBodyPart(); File
emailImage = new File("d:\\chemical\\_Electron.png"); DataSource
fds = new FileDataSource(emailImage); messageBodyPart.setDataHandler(new
DataHandler(fds)); messageBodyPart.setHeader("Content-ID","<image>"); |
- Sending
the email: The message is sent using the Transport.send(message)
method.
Changes and Improvements:
- Dynamic
Image Path: The sendMail method now accepts the image path as a
parameter, making the program more flexible.
- File
Existence Check: Added a check to ensure that the file exists before
attempting to send it.
- Cleaner
Main Method: The main method now uses the dynamic htmlContent and
passes it to the sendMail method. You can easily adjust the content as
needed.
0 Comments