惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
博客园_首页
H
Help Net Security
WordPress大学
WordPress大学
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
B
Blog
I
InfoQ
SecWiki News
SecWiki News
T
Tailwind CSS Blog
Spread Privacy
Spread Privacy
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
P
Palo Alto Networks Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
Kaspersky official blog
M
MIT News - Artificial intelligence
S
Schneier on Security
T
Threat Research - Cisco Blogs
F
Fortinet All Blogs
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
The Cloudflare Blog
Recent Announcements
Recent Announcements
Security Latest
Security Latest
G
GRAHAM CLULEY
IT之家
IT之家
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
腾讯CDC
Google DeepMind News
Google DeepMind News
V
V2EX
S
Securelist
TaoSecurity Blog
TaoSecurity Blog
B
Blog RSS Feed
S
SegmentFault 最新的问题
博客园 - 叶小钗
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Project Zero
Project Zero
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
F
Full Disclosure

博客园 - AlleNny

AFW短信防火墙 v1.0 beta 发布 [原创]从硬盘安装Fedora Core 6 Windows XP 使用技巧 中国教育十大谎言 Windows Vista演示“胡言乱语” 正则表达式--递归匹配与非贪婪匹配 正则表达式-分组构造 正则表达式-替换和分组 Tricks of command line commands IIS下配置Mantis+PHP+MYSQL环境[转] JAAS:灵活的Java安全机制[转] 中国十大害人的“俗话”[转]----好文章共同分享 Quake 4 最佳开源软件一览 那些温暖的碎肉沫——DOOM 3杂感 SQL注入不完全思路与防注入程序 Modify Eclipse parameter to support VSS plugin Oracle 的一些小技巧 俺的新电脑的配置,嘿嘿
如何用JavaMail发邮件
AlleNny · 2006-11-28 · via 博客园 - AlleNny


这篇文章是要介绍如何要用JavaMail通过需认证的SMTP服务器发HTML格式的邮件。
首先在sun网站上下载JavaMail的实现,和JAF的实现(不知道为啥不放在一起),加入你的classpath。

代码先从Authenticator继承一个class,比如叫SMTPAuthenticator,这个要用于和SMTP服务器连接时做认证的。

class SMTPAuthenticator extends Authenticator {
    
private String user;

    
private String password;

    
public SMTPAuthenticator(String s, String s1) {
        user 
= s;
        password 
= s1;
    }


    
public PasswordAuthentication getPasswordAuthentication() {
        
return new PasswordAuthentication(user, password);
    }


}

然后开始发邮件,

    Session sendMailSession = null;
    SMTPTransport transport 
= null;
    Properties props 
= new Properties();

    
// 与服务器建立Session的参数设置
    props.put("mail.smtp.host""smtp.163.com"); // 写上你的SMTP服务器。
    props.put("mail.smtp.auth""true"); // 将这个参数设为true,让服务器进行认证。
    SMTPAuthenticator auth = new SMTPAuthenticator("user""mypassword"); // 不用多说,用户名,密码。

    sendMailSession 
= Session.getInstance(props, auth); // 建立连接。
    
// SMTPTransport用来发送邮件。
    transport = (SMTPTransport) sendMailSession.getTransport("smtp");
    transport.connect();
    
// 创建邮件。
    Message newMessage = new MimeMessage(sendMailSession);
    newMessage.setFrom(
new InternetAddress("me@163.com"));
    newMessage.setRecipient(Message.RecipientType.TO, 
new InternetAddress("somebody@gmail.com"));
    newMessage.setSubject(
"This a test mail for Java Mail API);
    newMessage.setSentDate(new Date());
    
    
// 使用MimeMultipart和MimeBodyPart才能发HTML格式邮件。
    BodyPart bodyPart = new MimeBodyPart();
    bodyPart.setContent(generateEmailBody(), 
"text/html;charset=gb2312"); // 发一个HTML格式的
    Multipart mp = new MimeMultipart();
    mp.addBodyPart(bodyPart);
    newMessage.setContent(mp);

    Transport.send(newMessage);

OK,邮件发出去啦~~