package org.wzywjy.mail;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class TestMail {
public final static String SMTPSERVER = "smtp.163.com";
public final static String POPSERVER = "pop.163.com";
public final static String ACCOUNT = "test";
public final static String PWD = "test";
public final static String MAILADDR = "test@163.com";
public void sendMail(String to, String from, String subject, String body) throws AddressException, MessagingException {
Properties pro = System.getProperties();
pro.put("mail.smtp.host", SMTPSERVER);
pro.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(pro, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, InternetAddress.parse(to, false)[0]);
msg.setSubject(subject);
msg.setText(body);
msg.setHeader("X-Mailer", "LOTONtechEmail");
msg.setSentDate(new Date());
Transport transport = session.getTransport("smtp");
System.out.println("connecting...");
transport.connect(SMTPSERVER, ACCOUNT, PWD);
System.out.println("Sending message");
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
}
public static void main(String[] args) {
TestMail test = new TestMail();
try {
test.sendMail(MAILADDR, MAILADDR, "test", "我的一个测试");
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
posted on 2006-09-15 10:30
小小~咖啡豆 阅读(295)
评论(1) 编辑 收藏 所属分类:
其他