{"id":3683,"date":"2025-09-22T10:00:47","date_gmt":"2025-09-22T14:00:47","guid":{"rendered":"https:\/\/www.mymiller.name\/wordpress\/?p=3683"},"modified":"2025-09-22T10:00:47","modified_gmt":"2025-09-22T14:00:47","slug":"sending-emails-in-java-spring-with-attachments-using-javamail","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/spring\/sending-emails-in-java-spring-with-attachments-using-javamail\/","title":{"rendered":"Sending Emails in Java Spring with Attachments using JavaMail"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><p>In this blog post, we\u2019ll delve into the process of creating a Java Spring service that leverages JavaMail to send emails, both with and without attachments. This capability is crucial for many web applications, from sending user registration confirmations to delivering reports and notifications.<\/p>\n<p>Let\u2019s get started.<\/p>\n<p><strong>1. Set up Your Project<\/strong><\/p>\n<ul>\n<li>Make sure you have a Spring Boot project ready.<\/li>\n<li>Include the following dependency in your <code>pom.xml<\/code> (for Maven) or <code>build.gradle<\/code> (for Gradle):<\/li>\n<\/ul>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-mail&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n<\/code><\/pre>\n<p><strong>2. Configure Email Properties<\/strong><\/p>\n<ul>\n<li>In your <code>application.properties<\/code> or <code>application.yml<\/code> file, configure the necessary email properties. Here\u2019s an example for <code>application.properties<\/code>:<\/li>\n<\/ul>\n<pre><code class=\"language-properties\">spring.mail.host=smtp.gmail.com \nspring.mail.port=587\nspring.mail.username=your-email@gmail.com\nspring.mail.password=your-password\nspring.mail.properties.mail.smtp.auth=true\nspring.mail.properties.mail.smtp.starttls.enable=true\n<\/code><\/pre>\n<ul>\n<li>Replace the placeholders with your actual email settings. If you\u2019re using a different email provider, adjust the host and port accordingly.<\/li>\n<\/ul>\n<p><strong>3. Create the Email Service<\/strong><\/p>\n<ul>\n<li>Now let\u2019s create a Spring service to handle email sending.<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.core.io.FileSystemResource;\nimport org.springframework.mail.SimpleMailMessage;\nimport org.springframework.mail.javamail.JavaMailSender;\nimport org.springframework.mail.javamail.MimeMessageHelper;\nimport org.springframework.stereotype.Service;\n\nimport javax.mail.MessagingException;\nimport javax.mail.internet.MimeMessage;\nimport java.io.File;\n\n@Service\npublic class EmailService {\n\n    @Autowired\n    private JavaMailSender javaMailSender;\n\n    public void sendSimpleEmail(String to, String subject, String text) {\n        SimpleMailMessage message = new SimpleMailMessage();\n        message.setTo(to);\n        message.setSubject(subject);\n        message.setText(text);\n        javaMailSender.send(message);\n    }\n\n    public void sendEmailWithAttachment(String to, String subject, String text, String attachmentPath) throws MessagingException {\n        MimeMessage message = javaMailSender.createMimeMessage();\n        MimeMessageHelper helper = new MimeMessageHelper(message, true);\n\n        helper.setTo(to);\n        helper.setSubject(subject);\n        helper.setText(text);\n\n        FileSystemResource file = new FileSystemResource(new File(attachmentPath));\n        helper.addAttachment(file.getFilename(), file);\n\n        javaMailSender.send(message);\n    }\n}\n<\/code><\/pre>\n<ul>\n<li>We inject the <code>JavaMailSender<\/code>, which Spring Boot auto-configures based on our properties.<\/li>\n<li>The <code>sendSimpleEmail<\/code> method handles sending plain text emails.<\/li>\n<li>The <code>sendEmailWithAttachment<\/code> method allows sending emails with file attachments.<\/li>\n<\/ul>\n<p><strong>4. Use the Email Service<\/strong><\/p>\n<ul>\n<li>You can now autowire this <code>EmailService<\/code> into any of your Spring components (controllers, services, etc.) and use it to send emails.<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Autowired\nprivate EmailService emailService;\n\n\/\/ ...\n\nemailService.sendSimpleEmail(&quot;recipient@example.com&quot;, &quot;Hello&quot;, &quot;This is a test email.&quot;);\n\n\/\/ ...\n\ntry {\n    emailService.sendEmailWithAttachment(&quot;recipient@example.com&quot;, &quot;Report&quot;, &quot;Please find the attached report.&quot;, &quot;\/path\/to\/report.pdf&quot;);\n} catch (MessagingException e) {\n    \/\/ Handle exception\n}\n<\/code><\/pre>\n<p><strong>Important Notes:<\/strong><\/p>\n<ul>\n<li>Ensure that you have the necessary permissions to send emails from your email provider.<\/li>\n<li>For production environments, consider using a dedicated email service provider for better deliverability and scalability.<\/li>\n<li>Handle exceptions gracefully, especially when dealing with attachments or network issues.<\/li>\n<li>Always test your email sending functionality thoroughly.<\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":3684,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[318],"tags":[69,319],"series":[],"class_list":["post-3683","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring","tag-java-2","tag-spring"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/e-mail-3597088_1280-jpg.avif","jetpack-related-posts":[{"id":3686,"url":"https:\/\/www.mymiller.name\/wordpress\/aws\/sending-emails-with-aws-simple-email-service-ses-and-attachments\/","url_meta":{"origin":3683,"position":0},"title":"Sending Emails with AWS Simple Email Service (SES) and Attachments","author":"Jeffery Miller","date":"November 17, 2025","format":false,"excerpt":"In this blog article, we\u2019ll delve into using Amazon Simple Email Service (SES) to send emails, both with and without attachments, directly from your applications. SES is a cost-effective, flexible, and scalable email service offered by AWS. Let\u2019s explore how to leverage its capabilities. 1. Set up AWS SES If\u2026","rel":"","context":"In &quot;AWS&quot;","block_context":{"text":"AWS","link":"https:\/\/www.mymiller.name\/wordpress\/category\/aws\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/e-mail-4487083_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/e-mail-4487083_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/e-mail-4487083_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/e-mail-4487083_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/e-mail-4487083_1280-jpg.avif 3x"},"classes":[]},{"id":3680,"url":"https:\/\/www.mymiller.name\/wordpress\/aws\/sending-sms-and-mms-messages-with-aws-sns-and-java-spring-integration\/","url_meta":{"origin":3683,"position":1},"title":"Sending SMS and MMS Messages with AWS SNS and Java (Spring Integration)","author":"Jeffery Miller","date":"September 22, 2025","format":false,"excerpt":"AWS Simple Notification Service (SNS) provides a robust platform for sending notifications across various channels, including SMS and MMS. Let\u2019s explore how to implement this functionality using Java within a Spring framework. Prerequisites AWS Account: An active AWS account is necessary. AWS SDK for Java: Make sure you have the\u2026","rel":"","context":"In &quot;AWS&quot;","block_context":{"text":"AWS","link":"https:\/\/www.mymiller.name\/wordpress\/category\/aws\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/smartphone-3152679_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/smartphone-3152679_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/smartphone-3152679_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/smartphone-3152679_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/smartphone-3152679_1280-jpg.avif 3x"},"classes":[]},{"id":3569,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_ai\/integrating-prolog-with-spring-boot\/","url_meta":{"origin":3683,"position":2},"title":"Integrating Prolog with Spring Boot","author":"Jeffery Miller","date":"September 22, 2025","format":false,"excerpt":"Prolog, a declarative logic programming language, shines in solving specific types of problems that require knowledge representation and logical inference. Integrating Prolog with Spring Boot can bring the power of logic programming to your Java applications. 1. Setting Up Your Environment Add JPL Dependency: Include the Java Prolog Interface (JPL)\u2026","rel":"","context":"In &quot;Spring AI&quot;","block_context":{"text":"Spring AI","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_ai\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/Gemini_Generated_Image_avkkoeavkkoeavkk.jpg?fit=1200%2C1200&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/Gemini_Generated_Image_avkkoeavkkoeavkk.jpg?fit=1200%2C1200&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/Gemini_Generated_Image_avkkoeavkkoeavkk.jpg?fit=1200%2C1200&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/Gemini_Generated_Image_avkkoeavkkoeavkk.jpg?fit=1200%2C1200&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/Gemini_Generated_Image_avkkoeavkkoeavkk.jpg?fit=1200%2C1200&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3374,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_databases\/spring-data-with-java-records\/","url_meta":{"origin":3683,"position":3},"title":"Spring Data with Java Records","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Java records are a new feature introduced in Java 14 that provides a concise way to declare classes that are meant to hold immutable data. Spring Data is a popular framework for building data access layers in Java applications. In this answer, we will explain how to use Java records\u2026","rel":"","context":"In &quot;Spring Databases&quot;","block_context":{"text":"Spring Databases","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_databases\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/office-work-gc63fa3774_640.jpg?fit=640%2C430&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/office-work-gc63fa3774_640.jpg?fit=640%2C430&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/office-work-gc63fa3774_640.jpg?fit=640%2C430&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3951,"url":"https:\/\/www.mymiller.name\/wordpress\/java\/scaling-streams-mastering-virtual-threads-in-spring-boot-4-and-java-25\/","url_meta":{"origin":3683,"position":4},"title":"Scaling Streams: Mastering Virtual Threads in Spring Boot 4 and Java 25","author":"Jeffery Miller","date":"December 22, 2025","format":false,"excerpt":"As a software architect, I\u2019ve seen the industry shift from heavy platform threads to reactive streams, and finally to the \"best of both worlds\": Virtual Threads. With the recent release of Spring Boot 4.0 and Java 25 (LTS), Project Loom's innovations have officially become the bedrock of high-concurrency enterprise Java.\u2026","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/12\/Gemini_Generated_Image_wqijejwqijejwqij-scaled.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/12\/Gemini_Generated_Image_wqijejwqijejwqij-scaled.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/12\/Gemini_Generated_Image_wqijejwqijejwqij-scaled.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/12\/Gemini_Generated_Image_wqijejwqijejwqij-scaled.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/12\/Gemini_Generated_Image_wqijejwqijejwqij-scaled.avif 3x"},"classes":[]},{"id":3961,"url":"https:\/\/www.mymiller.name\/wordpress\/spring\/spring4\/architecting-spring-boot-4-with-official-spring-grpc-support\/","url_meta":{"origin":3683,"position":5},"title":"Architecting Spring Boot 4 with Official Spring gRPC Support","author":"Jeffery Miller","date":"January 15, 2026","format":false,"excerpt":"For years, the Spring community relied on excellent third-party starters (like net.devh) to bridge the gap between Spring Boot and gRPC. With the evolution of Spring Boot 4 and the official Spring gRPC project, we now have native support that aligns perfectly with Spring's dependency injection, observability, and configuration models.\u2026","rel":"","context":"In &quot;Spring4&quot;","block_context":{"text":"Spring4","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring\/spring4\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2026\/01\/Gemini_Generated_Image_3yqio33yqio33yqi.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2026\/01\/Gemini_Generated_Image_3yqio33yqio33yqi.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2026\/01\/Gemini_Generated_Image_3yqio33yqio33yqi.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2026\/01\/Gemini_Generated_Image_3yqio33yqio33yqi.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2026\/01\/Gemini_Generated_Image_3yqio33yqio33yqi.avif 3x"},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3683","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/comments?post=3683"}],"version-history":[{"count":1,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3683\/revisions"}],"predecessor-version":[{"id":3685,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3683\/revisions\/3685"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/3684"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=3683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=3683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=3683"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=3683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}