我现在想验证一下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();
}
}
}