菜鸟刚用jsp,现在要帮公司做个模块,用到javamail邮件发送的功能,公司内同事间邮件发送,小弟在网上找了些代码,但是就是实现不了,用163邮箱测试报如下错
javax.mail.MessagingException: Could not connect to SMTP host: smtp.163.com, port: 25; nested exception is: java.net.ConnectException: Connection timed out: connect 
好像是连接超时,不知道怎么修改,求救!

解决方案 »

  1.   

    源代码如下:
    <%@ page contentType="text/html;charset=GB2312" %>
    <%request.setCharacterEncoding("gb2312");%>
    <%@ page import="java.util.*,javax.mail.*"%>
    <%@ page import="javax.mail.internet.*"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>邮件发送</title>
    </head><body>
    <%
    try{
    String tto=request.getParameter("to");
    String ttitle=request.getParameter("title");
    String tcontent=request.getParameter("content");Properties props=new Properties();
    props.put("mail.smtp.host","smtp.163.com");
    props.put("mail.smtp.auth","true"); //这样才能通过验证
    Session s=Session.getInstance(props);
    s.setDebug(true);MimeMessage message=new MimeMessage(s);//给消息对象设置发件人/收件人/主题/发信时间
    InternetAddress from=new InternetAddress("[email protected]");
    message.setFrom(from);
    InternetAddress to=new InternetAddress(tto);//发邮件的目的地(收件人信箱)
    message.setRecipient(Message.RecipientType.TO,to);
    message.setSubject(ttitle);
    message.setSentDate(new Date());
    //给消息对象设置内容
    BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
    mdp.setContent(tcontent,"text/html;charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
    Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对
    //象(事实上可以存放多个)
    mm.addBodyPart(mdp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
    message.setContent(mm);//把mm作为消息对象的内容message.saveChanges();
    Transport transport=s.getTransport("smtp");
    transport.connect("smtp.163.com","XXXXXXXX","YYYYYYYY");
    transport.sendMessage(message,message.getAllRecipients());
    transport.close();
    %>
    <div align="center">
    <p><font color="#FF6600">发送成功!</font></p>
    </div>
    <%
    }catch(MessagingException e){
    out.println(e.toString());
    }
    %>
    </body>
    </html>