ozimov / spring-boot-email-tools

A set of services and tools for sending emails in a Spring Boot 1.5.x application using a Template Engine

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add attachement to my email

Zeuzif opened this issue · comments

please can u tell me how can i add a pdf file to my email ?

What have you tried? Please show me the code.

@component
public class SendMail {
@Autowired
public EmailService emailService;

public void sendEmail(ByteArrayInputStream out,String enseignantMail) throws UnsupportedEncodingException {
	DefaultEmailAttachment attachment = null;
	try {
		attachment = DefaultEmailAttachment.builder().attachmentName("Nouvelle_Demande.pdf")
				.attachmentData(IOUtils.toByteArray(out)).build();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	final Email email = DefaultEmail.builder()
			.from(new InternetAddress(enseignantMail, enseignantMail))
			.to(Lists.newArrayList(new InternetAddress("username@gmail.com", "password")))
			.subject("Laelius de amicitia")
			.body("Firmamentum autem stabilitatis constantiaeque eius, quam in amicitia quaerimus, fides est.").attachment(attachment)
			.encoding("UTF-8").build();
	
	
	emailService.send(email);
}

}

it's ok thank u very much

Well, when you use the builder returned from DefaultEmail.builder(), don't you have any method called attachment that accepts an object of type EmailAttachment?

Anyway, all the examples in the example folder which use a template have an attachment. E.g. for Thymeleaf https://github.com/ozimov/spring-boot-email-tools/blob/master/examples/sending-mime-email-with-thymeleaf-example/src/main/java/com/test/TestService.java

I'll consider this question closed :)