JAVA MAIL
需要JAVAMAIL包
MyAuthenticator论证类
public class MyAuthenticator extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("weasel100@163.com", "password");
}
}
SendMessage发送简单的单个邮件
public class SendMessage {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.user", "weasel100");
props.put("mail.smtp.from", "weasel100@163.com");
props.put("mail.debug", "true");
MyAuthenticator auth = new MyAuthenticator();
Session session = Session.getInstance(props, auth);
session.setDebug(true);
Message msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress("weasel100.163.com"));
InternetAddress[] address = {
new InternetAddress("yrzu@sina.com"), new InternetAddress("weasel100@163.com")
};
msg.setRecipients(Message.RecipientType.TO, address);
//msg.setRecipient(Message.RecipientType.TO, new InternetAddress("weasel100@163.com"));
msg.setSentDate(new Date());
msg.setSubject("Java API Test!!!!!!!!!!!!!!!!");
msg.setText("Dear Mr. Wang /n" +
" Hello World!/n" +
"Best" +
"Li Si 2006.7.19");
Transport.send(msg);
} catch (MessagingException mex) {
//System.out.println("/n--Exception handling in msgsendsample.java");
mex.printStackTrace();
/*System.out.println();
Exception ex = mex;
do {
if (ex instanceof SendFailedException) {
SendFailedException sfex = (SendFailedException)ex;
Address[] invalid = sfex.getInvalidAddresses();
if (invalid != null) {
System.out.println(" ** Invalid Addresses");
if (invalid != null) {
for (int i = 0; i < invalid.length; i++)
System.out.println(" " + invalid[i]);
}
}
Address[] validUnsent = sfex.getValidUnsentAddresses();
if (validUnsent != null) {
System.out.println(" ** ValidUnsent Addresses");
if (validUnsent != null) {
for (int i = 0; i < validUnsent.length; i++)
System.out.println(" "+validUnsent[i]);
}
}
Address[] validSent = sfex.getValidSentAddresses();
if (validSent != null) {
System.out.println(" ** ValidSent Addresses");
if (validSent != null) {
for (int i = 0; i < validSent.length; i++)
System.out.println(" "+validSent[i]);
}
}
}
System.out.println();
if (ex instanceof MessagingException)
ex = ((MessagingException)ex).getNextException();
else
ex = null;
} while (ex != null);*/
}
System.out.println("End.");
}
}
SendFiles发送我个地址,及附件功能
public class SendFiles {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.user", "weasel100");
props.put("mail.smtp.from", "weasel100@163.com");
//props.put("mail.debug", "true");
MyAuthenticator auth = new MyAuthenticator();
Session session = Session.getInstance(props, auth);
//session.setDebug(true);
Message msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress("weasel100@163.com"));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress("weasel100@163.com"));
msg.setSentDate(new Date());
msg.setSubject("Java API Test!!!!!!!!!!!!!!!!");
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText("Dear Mr. Wang /n" +
" Hello World!/n" +
"Best" +
"Li Si 2006.7.19");
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource("F:/J2EE_yu/POM.jar");
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
MimeBodyPart mbp3 = new MimeBodyPart();
FileDataSource fds2 = new FileDataSource("F:/J2EE_yu/sqlnet.log");
mbp3.setDataHandler(new DataHandler(fds2));
mbp3.setFileName(fds2.getName());
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
mp.addBodyPart(mbp3);
msg.setContent(mp);
Transport.send(msg);
} catch (MessagingException mex) {
mex.printStackTrace();
}
System.out.println("End.");
}
}