从错误看好象是你的与名写错了。呵呵
不能这么简单吧。
另外:
你的程序能用sina.com.cn做smtp服务器?
我也做过一个这样的程序。
一般的商用邮件服务器为了防止你利用他的smtp服务器进行垃圾邮件传播
都会要求你进行身份验证的,这个问题你解决了么?
如何实现,谢谢。
不能这么简单吧。
另外:
你的程序能用sina.com.cn做smtp服务器?
我也做过一个这样的程序。
一般的商用邮件服务器为了防止你利用他的smtp服务器进行垃圾邮件传播
都会要求你进行身份验证的,这个问题你解决了么?
如何实现,谢谢。
解决方案 »
- java下载网络文件程序
- |javamy|Java根据JavaBean的规则却要写成get,set,这样表字段多了看起来不明了,有什么解决方法?有.NET与Java代码比较 谢谢
- 手机wap怎样实现下载功能
- JBoss中配置数据源产生 Failed to register driver for: com.mysql.jdbc.Driver 异常
- 有高手懂WSDL吗?有些问题要请教!
- 纯JAVA中这样去连接数据库
- webwork,欢迎大家谈谈,QQ群20362791
- weblogic下配置SSL!
- Tuxedo在J2EE的B/S构架中还有必要吗?
- 求助:ejb 出错
- 关于TOMCAT的问题,救命啊!!!!!!
- jbuilder工程中添加文件的问题
把你的代码贴出来吧,或者发到我的电子邮件
[email protected](巴特鲁) :
要求认证的smtp,你您可以使用Authenticator抽象类的子类,并通过 getPasswordAuthentication()方法返回一个 PasswordAuthentication
实例,在该实例中把用户名和密码传递进去即可
具体可参见sun的教程:
http://cn.sun.com/developers/onlineTraining/JavaMail/
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*; public class SimpleSendMessage { public static void main(String[] args) { String host = "smtp.sina.com.cn"; //或者host="202.106.187.180";都可用
//String host = "61.166.155.284"; //自己的服务器,不可用
String to = "[email protected]";
String from = "[email protected]"; //可随便写
String subject = "mail test";
String messageText = "I am sending a message using ";
boolean sessionDebug = false; // Create some properties and get the default Session.
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true"); //允许smtp校验
try {
Session session =Session.getInstance(props,new MailAuthenticator("username","passwd"));//sina的帐号和密码
PasswordAuthentication psw=new PasswordAuthentication("username","passwd");//sina的帐号和密码
session.setPasswordAuthentication(new URLName(host),psw);
session.setDebug(sessionDebug);
Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
////增加附件
Multipart mailBody = new MimeMultipart();
MimeBodyPart mainBody=new MimeBodyPart();
mainBody.setText("hello !");
mailBody.addBodyPart(mainBody);
FileDataSource fds=new FileDataSource("d:\\mail.gif");
MimeBodyPart mimeAttach=new MimeBodyPart();
mimeAttach.setDataHandler(new DataHandler(fds));
mimeAttach.setFileName(fds.getName());
mailBody.addBodyPart(mimeAttach);
msg.setContent(mailBody);
////
Transport.send(msg);
}
catch(MessagingException mex){
mex.printStackTrace();
//System.out.println("can't send mail");
}
} //end main()
}
class MailAuthenticator extends Authenticator
{
private String UserName;
private String Password;
public MailAuthenticator(String UserName,String Password)
{
this.UserName = UserName;
this.Password = Password;
} public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(this.UserName,this.Password);
}
}
----------
好像只有sina的信箱可以使用本程序,sohu和hotmail等都不可以
String from = "[email protected]"; //可随便写很多都要求论证,只有用户名与之相对且密码正确才能够发得出去,不然会报没有权限的错这里也有不应对:
String host = "smtp.sina.com.cn"; //或者host="202.106.187.180";都可用
只能用前一种即 smtp.sina.com.cn,而不能用IP,202.106.187.180
所以你的错误也就是不应该用的IP,而用我上面提到的方法
smtp.yourservername
我做过测试啊,用ip可以的