代码内容:
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.qq.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//使用JavaMail发送邮件的5个步骤
//1、创建session
Session session = Session.getInstance(prop);
//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport ts = session.getTransport();-----------》这一步报空指针
控制台报错信息:
javax.mail.NoSuchProviderException: java.lang.NullPointerException
at javax.mail.Session.getTransport(Session.java:298)
Session session = Session.getDefaultInstance(prop); 试试
读属性文件
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("/IcisReport.properties");
prop.load(in);
Set keyValue = prop.keySet();
for (Iterator it = keyValue.iterator(); it.hasNext();)
{
String key = (String) it.next();
}
不太知道哪里错了,好像是properties参数设置得不对
public void send(String from, String to, String password, String subject, String content, boolean debug) throws Exception{
Properties properties = new Properties();
if(debug){
properties.setProperty("mail.debug", "true");
}
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.host", "smtp.qq.com");
properties.put("mail.smtp.ssl.enable", true);
MailSSLSocketFactory mailSSLSocketFactory = new MailSSLSocketFactory();
mailSSLSocketFactory.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.socketFactory", mailSSLSocketFactory);
Session session = Session.getInstance(properties);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(content);
Transport transport = session.getTransport();
transport.connect(from, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
smtp.qq.com 您用的 是QQ 的邮箱吧 。这个是需要您到自己发送方的邮箱开通POP3/SMTP服务 获取独立的密码(不是QQ密码)并且设置到发送对象中,这一步您做了吗?
我这里有完整的代码 , 可以带邮件的,只发送文字的都有 ,需要的话可以给您
你的session中没有设置账号和密码 也有可能你的mail.smtp.port没有设置
#邮件发送协议
mail.transport.protocol=smtp
#SMTP邮件服务器
mail.smtp.host=smtp.office365.com
#SMTP邮件服务器默认端口
mail.smtp.port=587
#是否要求身份认证
mail.smtp.auth=true
// 创建邮件session
Session session = Session.getDefaultInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
你JDK什么版本?
已经解决了是 jar包冲突导致的