Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd707496

Browse files
send email in java example
1 parent80aaee1 commitd707496

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
packagesporadic;
2+
3+
importjava.util.Properties;
4+
5+
importjavax.mail.Message;
6+
importjavax.mail.MessagingException;
7+
importjavax.mail.Session;
8+
importjavax.mail.Transport;
9+
importjavax.mail.internet.InternetAddress;
10+
importjavax.mail.internet.MimeMessage;
11+
12+
//TODO: make this program work!
13+
14+
/**
15+
* To send an e-mail using your Java Application is simple enough but to start
16+
* with you should have JavaMail API and Java Activation Framework (JAF)
17+
* installed on your machine.
18+
*
19+
* [DONE] You can download latest version of JavaMail (Version 1.2) from Java's
20+
* standard website.
21+
*
22+
* [DONE] You can download latest version of JAF (Version 1.1.1) from Java's
23+
* standard website.
24+
*
25+
* Download and unzip these files, in the newly created top level directories
26+
* you will find a number of jar files for both the applications. You need to
27+
* add mail.jar and activation.jar files in your CLASSPATH.
28+
*
29+
* Send a Simple E-mail: Here is an example to send a simple e-mail from your
30+
* machine.
31+
*
32+
* Here it is assumed that your localhost is connected to the internet and
33+
* capable enough to send an email.
34+
*/
35+
36+
// This app is not running now, I guess it's b/c my localhost is not connected
37+
// to Internet yet. I need to figure this out.
38+
publicclassSendEmail {
39+
40+
publicstaticvoidmain(String[]args) {
41+
// Recipient's email ID needs to be mentioned.
42+
Stringto ="sunjiahuan@gmail.com";
43+
44+
// Sender's email ID needs to be mentioned
45+
Stringfrom ="steve.j.sun@gmail.com";
46+
47+
// Assuming you are sending email from localhost
48+
Stringhost ="10.34.128.145";
49+
50+
// Get system properties
51+
Propertiesproperties =System.getProperties();
52+
53+
// Setup mail server
54+
properties.setProperty("mail.smtp.host",host);
55+
56+
// Get the default Session object.
57+
Sessionsession =Session.getDefaultInstance(properties);
58+
59+
try {
60+
// Create a default MimeMessage object.
61+
MimeMessagemessage =newMimeMessage(session);
62+
63+
// Set From: header field of the header.
64+
message.setFrom(newInternetAddress(from));
65+
66+
// Set To: header field of the header.
67+
message.addRecipient(Message.RecipientType.TO,newInternetAddress(
68+
to));
69+
70+
// Set Subject: header field
71+
message.setSubject("This is the Subject Line!");
72+
73+
// Now set the actual message
74+
message.setText("This is actual message");
75+
76+
// Send message
77+
Transport.send(message);
78+
System.out.println("Sent message successfully....");
79+
}catch (MessagingExceptionmex) {
80+
mex.printStackTrace();
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp