javamail API中的SMTP身份validation问题

import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import javax.mail.*; import javax.mail.internet.*; import javax.mail.event.*; import java.net.*; import java.util.*; public class servletmail extends HttpServlet { public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException { PrintWriter out=response.getWriter(); response.setContentType("text/html"); try { Properties props=new Properties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.host","smtp.gmail.com"); props.put("mail.smtp.port", "25"); props.put("mail.smtp.auth", "true"); Authenticator authenticator = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("user", "pass"); } }; Session sess=Session.getDefaultInstance(props,authenticator); Message msg=new MimeMessage(sess); msg.setFrom(new InternetAddress("abc@gmail.com")); msg.addRecipient(Message.RecipientType.TO, new InternetAddress("abc@gmail.com")); msg.setSubject("Hello JavaMail"); msg.setText("Welcome to JavaMail"); Transport.send(msg); out.println("mail has been sent"); } catch(Exception e) { System.out.println("err"+e); } } } 

我正在使用以上im gettin d跟随错误

 servletmail.java:22: reference to Authenticator is ambiguous, both class java.ne t.Authenticator in java.net and class javax.mail.Authenticator in javax.mail mat ch Authenticator authenticator = new Authenticator() ^ servletmail.java:22: reference to Authenticator is ambiguous, both class java.ne t.Authenticator in java.net and class javax.mail.Authenticator in javax.mail mat ch Authenticator authenticator = new Authenticator() ^ 2 errors 

我跟着这个例子

http://java.sun.com/developer/onlineTraining/JavaMail/contents.html

我应该如何得到输出..将上面的代码…工作需要做什么变化…使用thunderbird smtp服务器

该错误表明这里有两个可能的Authenticator类 – 即java.net.Authenticator and javax.mail.Authenticator如错误所示。

这是因为您导入了java.net。*和javax.mail。*并且编译器不知道您需要哪个Authenticator

通过显式导入修复此问题

 import javax.mail.Authenticator; 

或者将第22行的身份validation者限定为

 javax.mail.Authenticator authenticator = new javax.mail.Authenticator() 

UPDATE

由于您在发送邮件时遇到问题,请首先检查您的网络管理员是否已授予您连接到gmail上的smtpserver的权限。 你是代理人吗?

创建sesssess.setDebug(true);添加到代码中: sess.setDebug(true);

查看消息,看看你得到了多远。

请尝试以下url提供的其他调试提示: http : //java.sun.com/products/javamail/FAQ.html#debug

更新2

我试过运行你的代码,它适用于我,包括发送电子邮件。 我不得不为道具添加一行

 props.put("mail.smtp.starttls.enable","true"); 

当然,更改此return new PasswordAuthentication("user", "pass"); 到你的实际用户名/密码。 和“abc@gmail.com”到您的实际电子邮件ID。