JAVA发送EMAIL的例子
?
import
javax.mail.*;
02
import
javax.mail.internet.MimeMessage;
03
import
javax.mail.internet.InternetAddress;
04
import
java.io.UnsupportedEncodingException;
05
import
java.util.Properties;
06
?
?07
/**
08
?
* Created by IntelliJ IDEA.
09
?
* User: Wizzer
10
?
* Date: 2010-12-29
11
?
* Time: 16:39:50
12
?
* To change this template use File | Settings | File Templates.
13
?
*/
14
public
class
Mail {
15
????
public
static
void
main(String args[])
throws
MessagingException, UnsupportedEncodingException {
16
????
Properties props =
new
Properties();
17
????
props.put(
"mail.smtp.host"
,
"smtp.qq.com"
);
18
????
props.put(
"mail.smtp.auth"
,
"true"
);
19
????
PopupAuthenticator auth =
new
PopupAuthenticator();?
20
????
Session session = Session.getInstance(props, auth);
21
????
MimeMessage message =
new
MimeMessage(session);
22
????
Address addressFrom =
new
InternetAddress(PopupAuthenticator.mailuser+
"@qq.com"
,
"George Bush"
);
23
????
Address addressTo =
new
InternetAddress(
"116****@qq.com"
,
"George Bush"
);
//收件人
24
????
message.setText(
"邮件发送成功"
);
25
????
message.setSubject(
"Javamal最终测试"
);
26
????
message.setFrom(addressFrom);
27
????
message.addRecipient(Message.RecipientType.TO,addressTo);
28
????
message.saveChanges();
29
????
Transport transport = session.getTransport(
"smtp"
);
30
????
transport.connect(
"smtp.qq.com"
, PopupAuthenticator.mailuser,PopupAuthenticator.password);
31
????
transport.send(message);
32
????
transport.close();
33
????
}
34
?
?35
}
36
class
PopupAuthenticator
extends
Authenticator {
37
public
static
final
String mailuser=
"wizzer"
;?
38
public
static
final
String password=
"********"
;
39
public
PasswordAuthentication getPasswordAuthentication() {
40
return
new
PasswordAuthentication(mailuser,password);
41
}
42
}