Home Java javaTutorial Java Email Sending Guide: Easy Getting Started and Practical Demonstrations

Java Email Sending Guide: Easy Getting Started and Practical Demonstrations

Dec 27, 2023 am 09:17 AM
Example demonstration Getting Started Tutorial java email sending

Java Email Sending Guide: Easy Getting Started and Practical Demonstrations

Java Email Sending Tutorial: Quick Start and Example Demonstration

In recent years, with the popularity and development of the Internet, email has become an indispensable part of people's daily life and work. A missing part. Sending emails through the Java programming language can not only achieve fast and efficient email sending, but also greatly improve work efficiency through automation. This article will introduce how to use the JavaMail library to send emails in Java and demonstrate it through specific code examples.

Step One: Import and Configuration of JavaMail Library
First, you need to import the JavaMail library. Importing can be achieved by adding the following dependencies to the project's Maven configuration file (pom.xml):

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>javax.mail-api</artifactId>
    <version>1.6.2</version>
</dependency>

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>
Copy after login

Step 2: Create a JavaMail Session instance
Email sending needs to be configured through a JavaMail Session instance and management, you can create a Session instance by using the username and password of the SMTP server. The sample code is as follows:

import java.util.Properties;
import javax.mail.Session;

Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", "smtp.example.com");
properties.setProperty("mail.smtp.port", "587");
properties.setProperty("mail.smtp.auth", "true");

Session session = Session.getDefaultInstance(properties);
Copy after login

In the above code, you need to replace smtp.example.com with the real SMTP server address, and you can also set the port number of the SMTP server (the default is 25 ) and whether authentication is required.

Step 3: Create an email message
In JavaMail, you can use the javax.mail.internet.MimeMessage class to create an email message. The sample code is as follows:

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

Message message = new MimeMessage(session);

try {
    message.setFrom(new InternetAddress("sender@example.com"));
    message.setRecipient(Message.RecipientType.TO, new InternetAddress("recipient@example.com"));
    message.setSubject("Hello from JavaMail");
    message.setText("This is a test email sent from JavaMail.");
} catch (AddressException e) {
    e.printStackTrace();
} catch (MessagingException e) {
    e.printStackTrace();
}
Copy after login

In the above code, sender@example.com and recipient@example.com need to be replaced with the real sender and recipient The sender’s email address.

Step 4: Send email
The last step is to send email through the created JavaMail Session instance. The sample code is as follows:

import javax.mail.Transport;

try {
    Transport.send(message);
    System.out.println("Email sent successfully!");
} catch (MessagingException e) {
    e.printStackTrace();
}
Copy after login

Run the above code. If everything goes well, the console will print out the message "Email sent successfully!", indicating that the email was sent successfully.

In summary, through the above steps and sample code, we can quickly get started and implement using the JavaMail library to send emails. Of course, in addition to basic email sending, JavaMail also supports more advanced functions, such as attachment sending, HTML format emails, etc. In practical applications, we can further expand and optimize the email sending function according to our own needs.

I hope this article can be helpful to beginners and guide everyone into the magical world of Java email sending!

The above is the detailed content of Java Email Sending Guide: Easy Getting Started and Practical Demonstrations. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP implementation framework: CakePHP introductory tutorial PHP implementation framework: CakePHP introductory tutorial Jun 18, 2023 am 09:04 AM

With the continuous development of Internet technology, Web development technology is also constantly updated and iterated. As an open source programming language, PHP is widely used in web development. As one of the commonly used tools in PHP development, the PHP framework can improve development efficiency and code quality. This article will introduce you to a PHP framework - CakePHP, and provide some simple tutorials to get started. 1. What is CakePHP? CakePHP is a model based on MVC (Model-View-Control

Beginner's Guide: Start from scratch and learn MyBatis step by step Beginner's Guide: Start from scratch and learn MyBatis step by step Feb 19, 2024 am 11:05 AM

Concise and easy-to-understand MyBatis introductory tutorial: write your first program step by step MyBatis is a popular Java persistence layer framework that simplifies the process of interacting with databases. This tutorial will show you how to use MyBatis to create and perform simple database operations. Step 1: Environment setup First, make sure your Java development environment has been installed. Then, download the latest version of MyBatis and add it to your Java project. You can download it from the official website of MyBatis

PHP implementation framework: Lumen framework introductory tutorial PHP implementation framework: Lumen framework introductory tutorial Jun 18, 2023 am 08:39 AM

Lumen is a PHP-based microframework developed by Laravel framework developers. It was originally designed to quickly build small API applications and microservices, while retaining some components and features of the Laravel framework. The Lumen framework is lightweight, fast, and easy to use, so it has received widespread attention and use. In this article, we will quickly get started with the Lumen framework and learn how to use the Lumen framework to build simple API applications. Framework preparation Before learning the Lumen framework, we need to

Basic elements of Java test classes: detailed analysis and example display Basic elements of Java test classes: detailed analysis and example display Jan 24, 2024 am 10:51 AM

Basic points of Java test classes: detailed analysis and example demonstration In Java development, testing is a crucial link. Testing can ensure the quality and functional correctness of the code and reduce the occurrence of potential bugs. The test class is the key to testing Java code. This article will analyze the basic points of Java test classes in detail and give specific code examples for demonstration. 1. Why test classes are needed During the development process, the code we write needs to go through different tests to verify its correctness. test

Getting started with the Python Flask framework Getting started with the Python Flask framework Jun 17, 2023 am 08:48 AM

PythonFlask framework introductory tutorial Flask is a simple and easy-to-use Python Web framework. It pays more attention to flexibility and lightweight, allowing programmers to build according to their own preferences. This article will introduce you to the basic concepts, installation and use of Flask, and use a simple example to demonstrate how to use Flask to build a web application. What is Flask? Flask is a lightweight web application framework based on Python that does not require the use of any special

Java Email Sending Guide: Easy Getting Started and Practical Demonstrations Java Email Sending Guide: Easy Getting Started and Practical Demonstrations Dec 27, 2023 am 09:17 AM

Java Email Sending Tutorial: Quick Start and Example Demonstration In recent years, with the popularity and development of the Internet, email has become an indispensable part of people's daily life and work. Sending emails through the Java programming language can not only achieve fast and efficient email sending, but also greatly improve work efficiency through automation. This article will introduce how to use the JavaMail library to send emails in Java and demonstrate it through specific code examples. Step 1: Import and configure the JavaMail library first

PHP implementation framework: ThinkPHP introductory tutorial PHP implementation framework: ThinkPHP introductory tutorial Jun 18, 2023 pm 09:42 PM

With the continuous development of Internet technology, various languages ​​​​and frameworks have also emerged. As a widely used scripting language, PHP is widely used in website development. Among the PHP frameworks, ThinkPHP is a very excellent framework that provides powerful functions and good performance. Using it can greatly improve the efficiency of website development. In this article, we will introduce you to the introductory tutorial of the ThinkPHP framework to help you quickly master this excellent framework. 1. What is ThinkPHPTh?

Naive Bayes examples in Python Naive Bayes examples in Python Jun 09, 2023 pm 11:36 PM

Python is a simple and easy-to-learn programming language with a rich set of scientific computing libraries and data processing tools. Among them, the Naive Bayes algorithm, as a classic machine learning method, is also widely used in the Python language. This article will use examples to introduce the usage and steps of Naive Bayes in Python. Introduction to Naive Bayes The Naive Bayes algorithm is a classification algorithm based on Bayes' theorem. Its core idea is to infer new data through the characteristics of the known training data set.

See all articles