sim*_*lue 2 java servlets jakarta-mail
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用上面的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
Run Code Online (Sandbox Code Playgroud)
我跟着这个例子
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;
Run Code Online (Sandbox Code Playgroud)
或者将第22行的身份验证者限定为
javax.mail.Authenticator authenticator = new javax.mail.Authenticator()
Run Code Online (Sandbox Code Playgroud)
UPDATE
由于您在发送邮件时遇到问题,请首先检查您的网络管理员是否已授予您连接到gmail上的smtpserver的权限.你是代理人吗?
创建后sess,将此行添加到代码中:sess.setDebug(true);
查看消息,看看你能走多远.
请尝试以下网址提供的其他调试技巧:http://java.sun.com/products/javamail/FAQ.html#debug
更新2
我已经尝试运行你的代码,它适用于我,包括发送电子邮件.我不得不为道具添加一行
props.put("mail.smtp.starttls.enable","true");
Run Code Online (Sandbox Code Playgroud)
当然,将其更改return new PasswordAuthentication("user", "pass");为您的实际用户名/密码.并将"abc@gmail.com"添加到您的实际电子邮件ID中.
| 归档时间: |
|
| 查看次数: |
2021 次 |
| 最近记录: |