首页 > 代码库 > 纯java实现邮件发送服务(亲测好用)

纯java实现邮件发送服务(亲测好用)

       今天自己测试了一下用java代码实现发送有限的服务,非常简单.直接贴代码:

       

import com.sun.mail.util.MailSSLSocketFactory;import javax.mail.*;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import java.security.GeneralSecurityException;import java.util.Date;import java.util.Properties;public class Main {    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.163.com");       // props.setProperty("mail.port", "465");        // 发送邮件协议名称        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("seenews 错误");        StringBuilder builder = new StringBuilder();        builder.append("url = " + "http://blog.csdn.net/never_cxb/article/details/50524571");        builder.append("\n页面爬虫错误");        builder.append("\n时间 " + new Date().getTime());        msg.setText(builder.toString());        msg.setFrom(new InternetAddress("m15101172298@163.com"));        Transport transport = session.getTransport();        transport.connect("smtp.163.com", "m15101172298@163.com", "**你自己的邮箱地址**");        transport.sendMessage(msg, new Address[] { new InternetAddress("623362984@qq.com") });        transport.close();    }}

这应该就ok了。对了,之前必须要下载相应的jar包:activation.jar和mail.jar。

纯java实现邮件发送服务(亲测好用)