Jmail 出错,msg.setRecipients(javax.mail.Message.RecipientType.To,address);//找不到符号 只能是javax.mail.Message.RecipientType

East271536394 2008-06-01 10:57:53
package com.mail;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SimpleMailSendMessage{
public static void main(String [] args){
String host ="163.com";
String to = "zdf_zdf520@163.com";
String from = "zdf_zdf520@163.com";
String subject = "javaMailTest";
String messageText = "Hello ,java mail";
boolean sessionDebug = false;

Properties props = System.getProperties();
props.put("mail.host",host);
props.put("mail.transport.protocol","smtp");

Session session = Session.getDefaultInstance(props,null);

session.setDebug(sessionDebug);
try{
Message msg=new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress [] address = {new InternetAddress(to)};
msg.setRecipients(javax.mail.Message.RecipientType.To,address);//找不到符号
//只有javax.mail.Message.RecipientType 没有这个TO
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);

Transport.send(msg);
}catch(Exception e){
e.printStackTrace();
}


}

}

大家帮帮我,来指导下
...全文
687 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lisl2003 2008-06-02
  • 打赏
  • 举报
回复
我在我本机做了个测试,除了四楼说的问题外,应该还缺了用户名和密码校验。
我对java发邮件也不是很了解,提供一个程序作参考,不对的地方还请批正。

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SimpleMailSendMessage {
public static void main(String[] args) {
String host = "smtp.163.com";
String to = "yourmail@163.com";
String from = "yourmail@163.com";
String subject = "javaMailTest";
String messageText = "Hello ,java mail";
boolean sessionDebug = false;

Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.smtp.port", 25);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");

Session session = Session.getDefaultInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("yourmail@163.com","yourpassword");
}
});

session.setDebug(sessionDebug);
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to) };
msg.setRecipients(javax.mail.Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);

Transport.send(msg);
} catch (Exception e) {
e.printStackTrace();
}

}

}
East271536394 2008-06-01
  • 打赏
  • 举报
回复
package com.mail;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SimpleMailSendMessage{
public static void main(String [] args){
String host ="server.163.com";
String to = "zhangdongfang_123@163.com";
String from = "zdf_zdf520@163.com";
String subject = "javaMailTest";
String messageText = "Hello ,java mail";
boolean sessionDebug = false;

Properties props = System.getProperties();
props.put("mail.smtp.host",host);
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.port",25);

Session session = Session.getDefaultInstance(props,null);

session.setDebug(sessionDebug);
try{
Message msg=new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress [] address = {new InternetAddress(to)};
msg.setRecipients(javax.mail.Message.RecipientType.TO,address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);

Transport.send(msg);
System.out.println("send sucess");
}catch(Exception e){
e.printStackTrace();
}


}

}

这样还是不行呀,
运行时 出错,
javax.mail.MessagingException: Could not connect to SMTP host: server.163.com, p
ort: 25, response: -1
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1235)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:34
0)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at com.mail.SimpleMailSendMessage.main(SimpleMailSendMessage.java:33)
Press any key to continue...
lisl2003 2008-06-01
  • 打赏
  • 举报
回复
To ? TO ?
IT孤儿 2008-06-01
  • 打赏
  • 举报
回复
老大,注意那个地方是TO不是To啊,那个单词是全部大写的,
bobor_2008 2008-06-01
  • 打赏
  • 举报
回复
javax.mail.MessagingException: Could not connect to SMTP host: server.163.com, p
ort: 25, response: -1
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1235)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:34
0)


//--------------------------------------------

不是server.163.com应该是smtp.163.com吧?

你试试.

关注..........

62,614

社区成员

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

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