问题描述
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("[email protected]"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
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 working with above im gettin d following error
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
我应该如何获得输出..上面的代码会工作吗?使用雷鸟smtp服务器需要进行哪些更改..im
how should i get the output..will the above code...workwhat are the changes that need to be made..im using thunderbird smtp server
推荐答案
该错误表明存在两个可能在其中引用的Authenticator
类-错误所指的是java.net.Authenticator and javax.mail.Authenticator
.
The error indicates that there are two possible Authenticator
classes which are being referenced here - namely java.net.Authenticator and javax.mail.Authenticator
as the error says.
这是因为您已经导入了java.net.*和javax.mail.*,并且编译器不知道您需要哪个Authenticator
.
This is because you have imported java.net.* and javax.mail.* and the compiler does not know which Authenticator
you need.
通过显式导入解决此问题
Fix this by explicitly importing
import javax.mail.Authenticator;
OR 将第22行的Authenticator限定为
OR qualify the Authenticator on line 22 as
javax.mail.Authenticator authenticator = new javax.mail.Authenticator()
更新
由于您在发送邮件时遇到问题,请首先检查您的网络管理员是否已授予您通过gmail连接到smtpserver的权限.您是代理人吗?
Since you are having problems with sending the mail, first check whether your network admins have given you permissions to connect to the smtpserver at gmail. Are you behind a proxy?
创建sess
后,将此行添加到代码中:sess.setDebug(true);
After creating sess
, add this line to the code: sess.setDebug(true);
看看消息,看看你能走多远.
Look at the messages and see how far you get.
尝试以下提供的其他调试技巧: http://java.sun .com/products/javamail/FAQ.html#debug
Try the other debug tips given at: http://java.sun.com/products/javamail/FAQ.html#debug
更新2
我尝试运行您的代码,它对我有用,包括发送电子邮件.我必须为道具添加一行
I've tried running your code and it works for me including sending the email.I had to add one line for props
props.put("mail.smtp.starttls.enable","true");
当然,请将此return new PasswordAuthentication("user", "pass");
更改为您的实际用户名/密码.然后将"[email protected]"添加到您的实际电子邮件ID.
And of course, change this return new PasswordAuthentication("user", "pass");
to your actual username/password.And "[email protected]" to your actual email id.
这篇关于javamail API中的SMTP身份验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!