游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1351|回复: 0

使用JavaMail发送邮件wxh zt

[复制链接]

1367

主题

1993

帖子

2118

积分

金牌会员

Rank: 6Rank: 6

积分
2118
发表于 2006-4-10 13:26:00 | 显示全部楼层 |阅读模式

作者:asklxf
/**
* Copyright_2006, Liao Xuefeng
* Created on 2006-4-7
*/
package com.crackj2ee.util.mail;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class SimpleMailSender {

    public static void main(String[] arge) throws Exception {
        String from = "abc_abc_abc@163.com";
        String to = "xyz_xyz_xyz@163.com";
        String subject = "Test mail";
        String body = "A text mail";
        Properties props = System.getProperties();
        // 设置SMTP邮件服务器:
        props.put("mail.smtp.host", "smtp.163.com");
        // SMTP服务器需要验证:
        props.put("mail.smtp.auth", "true");
        // 传入用户名和口令:
        Session session = Session.getDefaultInstance(props,
                new PasswordAuthenticator("your_username", "your_password"));
        // 创建新邮件:
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(from));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
        msg.setSubject(subject);
        msg.setText(body);
        msg.setSentDate(new Date());
        // 发送:
        Transport.send(msg);
    }
}

class PasswordAuthenticator extends Authenticator {

    private String username;
    private String password;

    public PasswordAuthenticator(String username, String password) {
        this.username = username;
        this.password = password;
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
    }

}

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-24 04:09

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表