import
java.io.UnsupportedEncodingException;
import
java.util.Date;
import
java.util.Properties;
import
javax.mail.Authenticator;
import
javax.mail.Message;
import
javax.mail.PasswordAuthentication;
import
javax.mail.Session;
import
javax.mail.Transport;
import
javax.mail.internet.InternetAddress;
import
javax.mail.internet.MimeMessage;
public
class
TextMessage {
static
String username=
"lileiqx@163.com"
;
static
String password=
"***************"
;
public
static
void
main(String [] args)
throws
Exception{
String
from=
"lileiqx@163.com"
;
String
to=
"lileiqx@163.com"
;
String
subject=
"test"
;
String
body=
"test!!!"
;
Properties
props = System.getProperties();
props.put(
"mail.smtp.host"
,
"smtp.163.com"
);
props.put(
"mail.smtp.auth"
,
"true"
);
props.put(
"mail.transport.protocol"
,
"smtp"
);
Authenticator
a =
new
Authenticator() {
public
PasswordAuthentication getPasswordAuthentication() {
return
new
PasswordAuthentication(username, password);
}
};
Session
session = Session.getDefaultInstance(props, a);
MimeMessage
msg=
new
MimeMessage(session);
String
nick=
""
;
try
{
nick=javax.mail.internet.MimeUtility.encodeText(
"我的昵称"
);
}
catch
(UnsupportedEncodingException e) {
e.printStackTrace();
}
msg.setFrom(
new
InternetAddress (from,nick));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
msg.setSentDate(
new
Date());
msg.setSubject(subject);
msg.setText(body);
Transport.send(msg);
}
}