Java email development (1): Use JMail to send a simple email

Source: Internet
Author: User

JMail is a component for email development. Because the required jar is not officially developed, you need to download the required jar package, I use a javamail-1.4.7.

If you are not familiar with the process of sending and receiving emails, please visit another blog: how email works

General steps:

1. Create a Properties object and set the corresponding content for the object.

2. Create a Session Object

3. Create a Message object that encapsulates mail information, such as the sender, recipient, and topic.

4. Create a Transport object to send emails using this object

In general, there are two steps: Construct the mail object (step 1, only other objects are required when building the mail), send the mail (step 2)


Package com. zyh. demo; import java. util. properties; import javax. mail. address; import javax. mail. message; import javax. mail. messagingException; import javax. mail. session; import javax. mail. transport; import javax. mail. internet. internetAddress; import javax. mail. internet. mimeMessage; public class Demo1 {public static void main (String [] args) throws MessagingException {Properties props = new Properties (); props. SetProperty ("mail. smtp. auth "," true "); // you need to authenticate props to access the smtp server. setProperty ("mail. transport. protocol "," smtp "); // sets the protocol Session = session for accessing the server. getDefaultInstance (props); session. setDebug (true); // enable the debug function Message msg = new MimeMessage (session); msg. setFrom (new InternetAddress (""); // set the sender, 163 mailbox requires the sender to be consistent with the login user (required), other mailboxes do not understand msg. setText ("Hello world! "); // Set the message content msg. setSubject ("test"); // set the mail subject Transport trans = session. getTransport (); trans. connect ("", 25, "your email address (@)", "email password"); // connect to the smtp server of the mailbox, 25 is the default port trans. sendMessage (msg, new Address [] {new InternetAddress ("")}); // send the email trans. close (); // close the connection }}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.