Java发送简单邮件

 

 

因为工作需要,所以百度了找到了这个写邮件的代码

需要mail.jar包

import java.security.GeneralSecurityException;
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;
import com.sun.mail.util.MailSSLSocketFactory;

public class MailTool {
    public static void main(String[] args) throws MessagingException, GeneralSecurityException {
        Properties props = new Properties();

        // 开启debug调试
        props.setProperty("mail.debug", "true");
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        // 设置邮件服务器主机名
        props.setProperty("mail.host", "smtp.qq.com");
        // 发送邮件协议名称
        props.setProperty("mail.transport.protocol", "smtp");

        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);

        Session session = Session.getInstance(props);

        Message msg = new MimeMessage(session);
        msg.setSubject("你有新邮件啦");
        StringBuffer builder = new StringBuffer();
        builder.append("\n这是一封来自java程序的邮件");
        builder.append("\n发邮件时间为时间 " + System.currentTimeMillis());
        msg.setText(builder.toString());
        msg.setFrom(new InternetAddress("[email protected]"));

        Transport transport = session.getTransport();
        // "**发送人的邮箱地址**", "**你的邮箱密码或者授权码**");
        transport.connect("smtp.qq.com", "[email protected]", "XXXXXXXXXX");
        // 接收人的邮箱地址
        transport.sendMessage(msg, new Address[] {new InternetAddress("[email protected]")});
        transport.close();
    }
}

 

你可能感兴趣的:(游戏)