如何解决这个异常? (从jsp程序向gmail帐户发送邮件时出现异常)

时间:2009-12-04 10:33:40

标签: java smtp

UPDATE:当我将它用作java程序时,代码正常工作。当我将其作为jsp运行时,它会给出以下异常。 java.security.AccessControlException:拒绝访问(java.security.SecurityPermission insertProvider.SunJSSE)

我发布了以下代码。我得到以下异常。如何解决这个问题?我已经在谷歌搜索但没有找到解决方案:( ** java.security.AccessControlException:

<%@ page import="java.security.*" %>
    <%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<html>
<body>
<%
String name=request.getParameter("name");
String from=request.getParameter("mail");
String message1=request.getParameter("msg");
try{   
String toAddress="mymailid@gmail.com";
String fromAddress=from;
String fromName=name;
String messageSubject="feedback"; 
String messageBody1=message1;
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  Properties props=new Properties();  
  props.put("mail.smtp.host","smtp.gmail.com"); 
       props.put("mail.debug","true"); 
    props.put("mail.smtp.starttls.enable","true"); 
Session session1 = Session.getDefaultInstance(props,new javax.mail.Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("mailid@gmail.com", "Password");
}
});
  Message message=new MimeMessage(session1);
  message.setFrom(new InternetAddress( fromAddress, fromName));
  message.setRecipient(Message.RecipientType.TO,new InternetAddress( toAddress));
  message.setSubject( messageSubject);
  message.setText( messageBody1);
  message.setSentDate(new Date());
  Transport.send(message);
 }
catch(Exception e)
{out.println(e);
     }    
%>
</body>
</html>

帮助我解决此错误

3 个答案:

答案 0 :(得分:0)

我认为发送邮件不需要添加安全提供程序。另外,使用com.sun。课程通常是不受欢迎的。

Herehere是使用JavaMail发送邮件的方法。

但我建议commons-mail

答案 1 :(得分:0)

我正在通过Gmail的smtp从我的应用程序使用JavaMail发送邮件,它运行正常。我不使用任何安全提供程序,只需将这些属性添加到消息属性中:

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

就是这样。邮件发送没有问题。 但是请注意,Gmail似乎希望您每隔一段时间登录一次网页邮件页面,或者它会停止通过smtp接收您的邮件(我不确定100%的这个,它发生在我身上)。

答案 2 :(得分:0)

当我在java中尝试这个snippnet时,它正在工作。当我在jsp中转换它时,如代码所示,它只显示了异常

java.security.AccessControlException:拒绝访问(java.security.SecurityPermission insertProvider.SunJSSE)

是否无法从jsp发送电子邮件?这是我第一次从jsp发送邮件。我在java中成功运行了该程序。