随笔 - 23  文章 - 11  trackbacks - 0
<2006年9月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(3)

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

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)  编辑  收藏 所属分类: 其他

FeedBack:
# re: 一个简单的发送邮件的例子[未登录] 2007-10-19 11:40 yuan
强!!!!!!!!!!  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: