Java测试发送邮件

新逸网络 908 0
import java.util.Properties;  
   
import javax.mail.Address;  
import javax.mail.Message;  
import javax.mail.MessagingException;  
import javax.mail.Session;  
import javax.mail.Transport;  
import javax.mail.internet.InternetAddress;  
import javax.mail.internet.MimeMessage;  
   
public class MailTest {  
    public static void main(String[] args) throws MessagingException {  
        Properties props = new Properties();  
        // 开启debug调试  
        props.setProperty("mail.debug", "false");  
        // 发送服务器需要身份验证  
        props.setProperty("mail.smtp.auth", "true");  
        // 设置邮件服务器主机名  
        props.setProperty("mail.host", "smtp.163.com");  
        // 发送邮件协议名称  
        props.setProperty("mail.transport.protocol", "smtp");  
   
        // 设置环境信息  
        Session session = Session.getInstance(props);  
   
        // 创建邮件对象  
        Message msg = new MimeMessage(session);  
        msg.setSubject("JavaMail测试");  
        // 设置邮件内容  
        msg.setText("您好,,,");  
        // 设置发件人  
        msg.setFrom(new InternetAddress("xxxx@163.com"));  
   
        Transport transport = session.getTransport();  
        // 连接邮件服务器  
        transport.connect("xxxx@163.com", "password");  
        // 发送邮件  
        transport.sendMessage(msg, new Address[] { new InternetAddress("xxxx@qq.com") });  
        // 关闭连接  
        transport.close();  
    }  
}

发表评论 取消回复
表情 图片 链接 代码

分享