package cn.com.demo.test;
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;
/**
* 建议用之前下个mail.jar包,我这是把账户和地址当成一个的
* */
public class Test_Email_N {
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.transport.protocol", "smtp");
// SSL加密一些邮箱需要。如QQ邮箱
// 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时间 " + System.currentTimeMillis());
msg.setText("aaaaaaa");
msg.setFrom(new InternetAddress("--发送邮件人的地址---"));
Transport transport = session.getTransport();
transport.connect("smtp.163.com", "----发送邮件人的账户----", "---授权码---");//现在163开启服务都要设置授权码的
transport.sendMessage(msg, new Address[] { new InternetAddress("----接收人的地址---") });
transport.close();
}
}
java邮件发送代码编写
最新推荐文章于 2023-08-18 11:50:09 发布