javamail发信的连接SMTP验证的问题

jcfox 2003-08-23 02:50:05
MailBean.java

package fastfox.util;
import java.util.*;
import java.text.SimpleDateFormat;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
public class MailBean {


public static void sendMessage(String smtpHost,String from,String to,String subject,String messageText)throws MessagingException{
System.out.println("Configuring mail session for:"+smtpHost);
SmtpAuth sa=new SmtpAuth();
sa.getuserinfo("jcren","mypassword");
java.util.Properties props=new java.util.Properties();
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host",smtpHost);
System.out.println("Constructing message- from="+from+" to="+to);
InternetAddress fromAddress=new InternetAddress(from);
InternetAddress[] toAddresss=new InternetAddress[3];
toAddresss[0]=new InternetAddress("jcren@163.com");
toAddresss[1]=new InternetAddress("xxxxx@163.com");
toAddresss[2]=new InternetAddress("ccccc@163.com");
int i=0;
while(i<toAddresss.length){
Session mailSession=Session.getDefaultInstance(props,sa);
MimeMessage testMessage=new MimeMessage(mailSession);
testMessage.setFrom(fromAddress);
testMessage.addRecipient(javax.mail.Message.RecipientType.TO,toAddresss[i]);
testMessage.setSentDate(new java.util.Date());
testMessage.setSubject(subject);
testMessage.setText(messageText);
System.out.println("Message constructed");

Transport.send(testMessage);
System.out.println("Message sent!");
i++;
}
}

public static void main(String[] args){
String smtpHost="smtp.163.com";
String from="jcren@163.com";
String to="jcren@163.com";
String subject="Test message";
StringBuffer theMessage=new StringBuffer();
theMessage.append("ddddhello22,\n\n");
theMessage.append("Hope all is well on you end.\n");
theMessage.append("Cheers");
try{
MailBean.sendMessage(smtpHost,from,to,subject,theMessage.toString());
}catch(javax.mail.MessagingException exc){
exc.printStackTrace();
}
}

static class SmtpAuth extends javax.mail.Authenticator {
private String user,password;

public void getuserinfo(String getuser,String getpassword){
user = getuser;
password = getpassword;
}
protected javax.mail.PasswordAuthentication getPasswordAuthentication(){
return new javax.mail.PasswordAuthentication(user,password);
}
}
}
JSP引用:
<%@ page contentType="text/html;charset=gb2312" import="java.util.*,java.sql.*" %>
<jsp:useBean id="mailbean" scope="page" class="fastfox.util.MailBean" />
<%
String mailto=request.getParameter("cust_mail");
String mailfrom="jcren@163.com";
String Subject="Subject";
String Content=request.getParameter("content");
mailbean.sendMessage("smtp.163.com",mailfrom,mailto,Subject,Content);
%>


报错:
javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP host: smtp.163.com, port: 25;
nested exception is:
java.net.SocketException: Socket operation on nonsocket: connect
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at fastfox.util.MailBean.sendMessage(MailBean.java:35)
at _sales._hasten._sendedit__jsp._jspService(/sales/hasten/sendedit.jsp:8)

我用telnet smtp.163.com 25 却是通的。这是怎么一回事啊?help me!
调试通过就给分,不够再加
...全文
42 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jcfox 2003-08-24
  • 打赏
  • 举报
回复
问题已经解决,关闭瑞星的邮件发送监控就OK了。感谢大家,同时也希望得到zoecyf的一份更详细的代码
jcfox 2003-08-24
  • 打赏
  • 举报
回复
fastfox@21cn.com
谢谢了
zoecyf 2003-08-23
  • 打赏
  • 举报
回复
我这有一份我做的,要的话我可以发给你,留下邮箱吧。
5liao 2003-08-23
  • 打赏
  • 举报
回复
呵呵...我昨天也碰到了相同的问题,后来我把瑞星的
邮件监控程序打开就行了,他自支弹出一个小窗
口要我输入用户名和密码,我也用163.com,不可理解。
icecloud 2003-08-23
  • 打赏
  • 举报
回复
Transport.send(testMessage);
换用transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
试试看,我也是,用send()死活发不走

http://www.csdn.net/Develop/read_article.asp?id=20621
icecloud 2003-08-23
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2180/2180217.xml?temp=.3253137
适用人群通用各大网易系,腾讯QQ系,新浪系,阿里系等主流邮箱;同时也适用于企业开发的企业邮箱,进行收件和发件。课程概述通用各大网易系,腾讯QQ系,新浪系,阿里系等主流邮箱;同时也适用于企业开发的企业邮箱,进行收件和发件。POP3是Post Office Protocol 3的简称,即邮局协议的第3个版本,它规定怎样将个人计算机连接到Internet的邮件服务器和下载电子邮件的电子协议。它是因特网电子邮件的第一个离线协议标准,POP3允许用户从服务器上把邮件存储到本地主机(即自己的计算机)上,同时删除保存在邮件服务器上的邮件,而POP3服务器则是遵循POP3协议的接收邮件服务器,用来接收电子邮件的SMTP 的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议。它是一组用于从源地址到目的地址传输邮件的规范,通过它来控制邮件的中转方式。SMTP 协议属于 TCP/IP 协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地。SMTP 服务器就是遵循 SMTP 协议的发送邮件服务器。   SMTP 认证,简单地说就是要求必须在提供了账户名和密码之后才可以登录 SMTP 服务器,这就使得那些垃圾邮件的散播者无可乘之机。。【开发者如何进行快速开发邮件发送系统???本课程系统进行快速研发,项目实战】 部分截图如下:完整版请查看课件或者视频

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧