0% found this document useful (0 votes)
6 views2 pages

Email

The document outlines a Spring Boot application structure that includes a main class for sending emails with embedded images. It specifies the necessary dependencies for email functionality and demonstrates how to configure and send an email using JavaMailSender. The application includes an image located in the resources directory that is embedded in the email content.

Uploaded by

pankajid7541
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Email

The document outlines a Spring Boot application structure that includes a main class for sending emails with embedded images. It specifies the necessary dependencies for email functionality and demonstrates how to configure and send an email using JavaMailSender. The application includes an image located in the resources directory that is embedded in the email content.

Uploaded by

pankajid7541
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

src/

├── main/
│ ├── java/
│ │ └── com/
│ │ └── yourpackage/
│ │ └── YourSpringBootApplication.java
│ └── resources/
│ └── static/
│ └── images/
│ └── your_image.png
└── resources/
└── application.properties

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.CommandLineRunner;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@SpringBootApplication
public class YourSpringBootApplication implements CommandLineRunner {

@Autowired
private JavaMailSender emailSender;

public static void main(String[] args) {


SpringApplication.run(YourSpringBootApplication.class, args);
}

@Override
public void run(String... args) {
sendEmailWithImage();
}

public void sendEmailWithImage() {


MimeMessage message = emailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom("[email protected]");
helper.setTo("[email protected]");
helper.setSubject("Email with Embedded Image");

// Inline image, add 'cid' content ID


helper.setText("<html><body><img src='cid:your_image' alt='Your
Image'></body></html>", true);

// Get the image path


File file = new
File("src/main/resources/static/images/your_image.png");
helper.addInline("your_image", file);

emailSender.send(message);
System.out.println("Email with embedded image sent.");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}

You might also like