问题描述
我现在想验证一下javamail的基本功能,遇到了javax.mail.MessagingException: Could not connect to SMTP host: smtp.163.com, port: 25;的问题,请大牛指点……package com;import java.util.Properties;import javax.mail.Message;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;public class sendMail {public static void main(String args[]){String host = "smtp.163.com";String from = "fasong@163.com";String to = "jieshou@163.com";try{Properties props = System.getProperties();props.put("mail.smtp.host", host);Session session = Session.getDefaultInstance(props, null);MimeMessage message = new MimeMessage(session);//设置发件人message.setFrom(new InternetAddress(from));//设置收件人message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));//设置消息主题message.setSubject("你好 JavaMail");//设置消息内容message.setText("Welcome to JavaMail world!");//发送消息Transport.send(message);System.out.println("消息已经发送成功");}catch(Exception e){e.printStackTrace();}}} 问题补充:11lingxian 写道
解决方案
你先ping 一下 smtp.163.com 看看能通吗?C:>ping smtp.163.com正在 Ping smtp.163.gslb.netease.com [123.125.50.133] 具有 32 字节的数据:来自 123.125.50.133 的回复: 字节=32 时间=181ms TTL=46请求超时。来自 123.125.50.133 的回复: 字节=32 时间=148ms TTL=46来自 123.125.50.133 的回复: 字节=32 时间=156ms TTL=46123.125.50.133 的 Ping 统计信息: 数据包: 已发送 = 4,已接收 = 3,丢失 = 1 (25% 丢失),往返行程的估计时间(以毫秒为单位): 最短 = 148ms,最长 = 181ms,平均 = 161msC:>
解决方案二:
props.put("mail.smtp.host", host); props.put("mail.smtp.auth","true"); props.put("username","ptvnet"); props.put("password","******"); Session session = Session.getDefaultInstance(props, new Authenticator(){protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password);}});163的邮箱是需要验证的