Spring Boot – How to send email via SMTP

In this tutorial, we will show you how to send email via SMTP in Spring Boot. Technologies used : Spring Boot 2.1.2.RELEASE Spring 5.1.4.RELEASE Java Mail 1.6.2 Maven 3 Java 8 1. Project Directory 2. Maven To send email, declares spring-boot-starter-mail, it will pull the JavaMail dependencies. pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 …

Read more

Java – How to send Email

To send email in Java, we need JavaMail pom.xml <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency> 1. Send Email Send a normal email in text format. SendEmailSMTP.java package com.mkyong; import com.sun.mail.smtp.SMTPTransport; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Date; import java.util.Properties; public class SendEmailSMTP { // for example, smtp.mailgun.org private static final String …

Read more

PrimeFaces + JSF Email validator example

To validate email, uses JSF <f:validateRegex>, and puts following regular expression. This regex should be able to validates most of the email format, and I’m using it for few projects. Email Regular Expression ^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$ P.S For detail explanation, refer to this how to validate email address with regular expression. In this tutorial, we will show …

Read more

How to send Email in Android

In Android, you can use Intent.ACTION_SEND to call an existing email client to send an Email. See following code snippets : Intent email = new Intent(Intent.ACTION_SEND); email.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); email.putExtra(Intent.EXTRA_SUBJECT, "subject"); email.putExtra(Intent.EXTRA_TEXT, "message"); email.setType("message/rfc822"); startActivity(Intent.createChooser(email, "Choose an Email client :")); P.S This project is developed in Eclipse 3.7, and tested with Samsung Galaxy S2 (Android …

Read more

Spring – Sending e-mail with attachment

Here’s an example to use Spring to send e-mail that has attachments via Gmail SMTP server. In order to contains the attachment in your e-mail, you have to use Spring’s JavaMailSender & MimeMessage , instead of MailSender & SimpleMailMessage. 1. Project dependency Add the JavaMail and Spring’s dependency. File : pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 …

Read more

Spring – Define an E-mail template in bean configuration file

In last Spring’s email tutorial, you hard-code all the email properties and message content in the method body, it’s not practical and should be avoid. You should consider define the email message template in the Spring’s bean configuration file. 1. Project dependency Add the JavaMail and Spring’s dependency. File : pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 …

Read more

Spring – Sending E-mail via Gmail SMTP server with MailSender

Spring comes with a useful ‘org.springframework.mail.javamail.JavaMailSenderImpl‘ class to simplify the e-mail sending process via JavaMail API. Here’s a Maven build project to use Spring’s ‘JavaMailSenderImpl‘ to send an email via Gmail SMTP server. 1. Project dependency Add the JavaMail and Spring’s dependency. File : pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mkyong.common</groupId> <artifactId>SpringExample</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> …

Read more

JavaMail API – Sending email via Gmail SMTP example

In this article, we will show you how to send an email via Gmail SMTP server. To send email in Java, we need JavaMail pom.xml <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency> 1. Gmail SMTP via TLS SMTP = smtp.gmail.com Port = 587 SendEmailTLS.java package com.mkyong; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; public class SendEmailTLS …

Read more

Maven – How to download JavaMail API

JavaMail is now available in Maven Central repository. pom.xml <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency> If you want to use the core JavaMail API to send email, you have to get the JavaMail library – mail.jar and activation.jar. Unfortunately, those libraries are not available in the Maven central repository, you have to download it from java.Net …

Read more

Java email regex examples

The format of an email address is local-part@domain. Look at this email address [email protected] local-part = mkyong @ = @ domain = example.com The formal definitions of an email address are in RFC 5322 and RFC 3696. However, this article will not follow the above RFC for email validation. The official email "local-part" is too …

Read more

How to send email in Python via SMTPLIB

Here is an email example written in Python module “smtplib”. It will connect to the GMail SMTP server and do the authentication with username and password given (hardcoded in program), and use the GMail SMTP server to send email to the recipient. import smtplib to = ‘[email protected]’ gmail_user = ‘[email protected]’ gmail_pwd = ‘yourpassword’ smtpserver = …

Read more

Different between Inline and Attachment in Email

Today when i debugging a Java Mail program, i find out one of the checking is about “Part.Attachment” and “Part.Inline” in email message. What is the different between Inline attachment and attachment? Inline Attachment Inline attachment usually is an attachment that we can see direclty within the email message body. For Example, In outlook express, …

Read more