Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Sending Email

Java Sending Email

It is very simple to send E-mail using a Java application, but first you should install the JavaMail API and the Java Activation Framework (JAF) on your machine.

You can also use the download links provided by this site:

Download and extract these files; in the newly created top-level directory, you will find some jar files for these two applications. You need to add the mail.jar and activation.jar files to your CLASSPATH.

If you use a third-party mail server such as QQ's SMTP server, you can see the complete user authentication example at the bottom of the article.


Sending a simple E-mail

The following is an example of sending a simple E-mail. Assume your local host is already connected to the network.

SendEmail.java file code:

//

File name: SendEmail.java

import

java

.

util

.*;

import

javax

.

mail

.*;

import

javax

.

mail

.

internet

.*;

import

javax

.

activation

.*;

public

class

SendEmail

{

public

static

void

main

(

String

[

]

args

)

{

//

Recipient email address

String

to

=

"

abcd@gmail.com

"

;

//

Sender email address

String

from

=

"

web@gmail.com

"

;

//

Specify the mail host to send from as localhost

String

host

=

"

localhost

"

;

//

Get system properties

Properties

properties

=

System

.

getProperties

(

)

;

//

Set the mail server

properties

.

setProperty

(

"

mail.smtp.host

"

,

host

)

;

//

Get the default session object

Session

session

=

Session

.

getDefaultInstance

(

properties

)

;

try

{

//

Create a default MimeMessage object

MimeMessage

message

=

new

MimeMessage

(

session

)

;

//

Set the From: header field

message

.

setFrom

(

new

InternetAddress

(

from

)

)

;

//

Set the To: header field

message

.

addRecipient

(

Message

.

RecipientType

.

TO

,

new

InternetAddress

(

to

)

)

;

//

Set the Subject: header field

message

.

setSubject

(

"

This is the Subject Line!

"

)

;

//

Set the message body

message

.

setText

(

"

This is actual message

"

)

;

//

Send the message

Transport

.

send

(

message

)

;

System

.

out

.

println

(

"

Sent message successfully....

"

)

;

}

catch

(

MessagingException

mex

)

{

mex

.

printStackTrace

(

)

;

}

}

}

Compile and run this program to send a simple E-mail:


$ java SendEmail
Sent message successfully....

If you want to send an e-mail to multiple recipients, use the following method to specify multiple recipient IDs:

void

addRecipients

(

Message

.

RecipientType

type

,

Address

[

]

addresses

)

throws

MessagingException

The following is a description of the parameters:


Sending an HTML E-mail

The following is an example of sending an HTML E-mail. Assume your local host is already connected to the network.

It is very similar to the previous example, except that we use the setContent() method to set the content to "text/html" via the second parameter, to specify that HTML content is to be sent.

SendHTMLEmail.java file code:

//

File name: SendHTMLEmail.java

import

java

.

util

.*;

import

javax

.

mail

.*;

import

javax

.

mail

.

internet

.*;

import

javax

.

activation

.*;

public

class

SendHTMLEmail

{

public

static

void

main

(

String

[

]

args

)

{

//

Recipient email address

String

to

=

"

abcd@gmail.com

"

;

//

Sender email address

String

from

=

"

web@gmail.com

"

;

//

Specify the mail host to send from as localhost

String

host

=

"

localhost

"

;

//

Get system properties

Properties

properties

=

System

.

getProperties

(

)

;

//

Set the mail server

properties

.

setProperty

(

"

mail.smtp.host

"

,

host

)

;

//

Get the default Session object.

Session

session

=

Session

.

getDefaultInstance

(

properties

)

;

try

{

//

Create a default MimeMessage object.

MimeMessage

message

=

new

MimeMessage

(

session

)

;

//

Set the From: header field

message

.

setFrom

(

new

InternetAddress

(

from

)

)

;

//

Set the To: header field

message

.

addRecipient

(

Message

.

RecipientType

.

TO

,

new

InternetAddress

(

to

)

)

;

//

Set the Subject: header field

message

.

setSubject

(

"

This is the Subject Line!

"

)

;

//

Send an HTML message; HTML tags can be inserted

message

.

setContent

(

"

This is actual message

"

,

"

text/html

"

)

;

//

Send the message

Transport

.

send

(

message

)

;

System

.

out

.

println

(

"

Sent message successfully....

"

)

;

}

catch

(

MessagingException

mex

)

{

mex

.

printStackTrace

(

)

;

}

}

}

Compile and run this program to send an HTML e-mail:


$ java SendHTMLEmail
Sent message successfully....

Sending an E-mail with attachments

The following is an example of sending an E-mail with attachments. Assume your local host is already connected to the network.

SendFileEmail.java file code:

//

File name: SendFileEmail.java

import

java

.

util

.*;

import

javax

.

mail

.*;

import

javax

.

mail

.

internet

.*;

import

javax

.

activation

.*;

public

class

SendFileEmail

{

public

static

void

main

(

String

[

]

args

)

{

//

Recipient email address

String

to

=

"

abcd@gmail.com

"

;

//

Sender email address

String

from

=

"

web@gmail.com

"

;

//

Specify the mail host to send from as localhost

String

host

=

"

localhost

"

;

//

Get system properties

Properties

properties

=

System

.

getProperties

(

)

;

//

Set the mail server

properties

.

setProperty

(

"

mail.smtp.host

"

,

host

)

;

//

Get the default Session object.

Session

session

=

Session

.

getDefaultInstance

(

properties

)

;

try

{

//

Create a default MimeMessage object.

MimeMessage

message

=

new

MimeMessage

(

session

)

;

//

Set the From: header field

message

.

setFrom

(

new

InternetAddress

(

from

)

)

;

//

Set the To: header field

message

.

addRecipient

(

Message

.

RecipientType

.

TO

,

new

InternetAddress

(

to

)

)

;

//

Set the Subject: header field

message

.

setSubject

(

"

This is the Subject Line!

"

)

;

//

Create a message part

BodyPart

messageBodyPart

=

new

MimeBodyPart

(

)

;

//

Message

messageBodyPart

.

setText

(

"

This is message body

"

)

;

//

Create a multipart message

Multipart

multipart

=

new

MimeMultipart

(

)

;

//

Set the text message part

multipart

.

addBodyPart

(

messageBodyPart

)

;

//

Attachment part

messageBodyPart

=

new

MimeBodyPart

(

)

;

String

filename

=

"

file.txt

"

;

DataSource

source

=

new

FileDataSource

(

filename

)

;

messageBodyPart

.

setDataHandler

(

new

DataHandler

(

source

)

)

;

messageBodyPart

.

setFileName

(

filename

)

;

multipart

.

addBodyPart

(

messageBodyPart

)

;

//

Send the complete message

message

.

setContent

(

multipart

)

;

//

Send the message

Transport

.

send

(

message

)

;

System

.

out

.

println

(

"

Sent message successfully....

"

)

;

}

catch

(

MessagingException

mex

)

{

mex

.

printStackTrace

(

)

;

}

}

}

Compile and run your program to send an E-mail with an attachment.


$ java SendFileEmail
Sent message successfully....

User authentication section

If you need to provide a username and password to the e-mail server for user authentication, you can do so with the following settings:

props

.

put

(

"

mail.smtp.auth

"

,

"

true

"

)

;

props

.

setProperty

(

"

mail.user

"

,

"

myuser

"

)

;

props

.

setProperty

(

"

mail.password

"

,

"

mypwd

"

)

;

The other sending mechanisms for e-mail remain consistent with the above.

Example of sending mail requiring username and password authentication:

This example uses the QQ mail server. You need to log into the QQ mailbox backend, enable the POP3/SMTP service in "Settings" => Account, as shown in the figure below:

QQ mailbox sets the password by generating an authorization code:

The Java code is as follows:

SendEmail2.java file code:

//

Example of sending mail requiring username and password

//

File name: SendEmail2.java

//

This example uses QQ mailbox. You need to set it in the QQ backend

import

java

.

util

.

Properties

;

import

javax

.

mail

.

Authenticator

;

import

javax

.

mail

.

Message

;

import

javax

.

mail

.

MessagingException

;

import

javax

.

mail

.

PasswordAuthentication

;

import

javax

.

mail

.

Session

;

import

javax

.

mail

.

Transport

;

import

javax

.

mail

.

internet

.

InternetAddress

;

import

javax

.

mail

.

internet

.

MimeMessage

;

public

class

SendEmail2

{

public

static

void

main

(

String

[

]

args

)

{

//

Recipient email address

String

to

=

"

xxx@qq.com

"

;

//

Sender email address

String

from

=

"

xxx@qq.com

"

;

//

Specify the mail host to send from as smtp.qq.com

String

host

=

"

smtp.qq.com

"

;

//

QQ mail server

//

Get system properties

Properties

properties

=

System

.

getProperties

(

)

;

//

Set the mail server

properties

.

setProperty

(

"

mail.smtp.host

"

,

host

)

;

properties

.

put

(

"

mail.smtp.auth

"

,

"

true

"

)

;

//

Get the default session object

Session

session

=

Session

.

getDefaultInstance

(

properties

,

new

Authenticator

(

)

{

public

PasswordAuthentication

getPasswordAuthentication

(

)

{

return

new

PasswordAuthentication

(

"

xxx@qq.com

"

,

"

QQ mailbox authorization code

"

)

;

//

Sender mail username, authorization code

}

}

)

;

try

{

//

Create a default MimeMessage object

MimeMessage

message

=

new

MimeMessage

(

session

)

;

//

Set the From: header field

message

.

setFrom

(

new

InternetAddress

(

from

)

)

;

//

Set the To: header field

message

.

addRecipient

(

Message

.

RecipientType

.

TO

,

new

InternetAddress

(

to

)

)

;

//

Set the Subject: header field

message

.

setSubject

(

"

This is the Subject Line!

"

)

;

//

Set the message body

message

.

setText

(

"

This is actual message

"

)

;

//

Send the message

Transport

.

send

(

message

)

;

System

.

out

.

println

(

"

Sent message successfully....from runoob.com

"

)

;

}

catch

(

MessagingException

mex

)

{

mex

.

printStackTrace

(

)

;

}

}

}