设为首页 加入收藏

TOP

发送邮件 java实现
2014-11-24 02:41:03 来源: 作者: 【 】 浏览:2
Tags:发送 邮件 java 实现

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.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailDemo {
public static void main(String args[]) throws Exception {
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.sina.com");
props.setProperty("mail.port", "25");

Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("test", "123456");
}
});
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("test@sina.com"));
msg.setSubject("你好吗?");
msg.setSentDate(new Date());
msg.setContent("我很好,你呢?", "text/html;charset=gbk");
msg.setRecipients(RecipientType.TO, InternetAddress.parse("test@sina.com,test@sohu.com"));
Transport.send(msg);
}
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇SUSE Linux 10(kernel-2.6)平台下.. 下一篇Java反射机制的应用例子

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: