问题描述
jsp源码:<%@ page contentType="text/html;charset=GB2312" %><%@ page import="javax.mail.internet.*" %><%@ page import="javax.activation.*" %><%@ page import="javax.mail.*" %><%@ page import="java.util.*" %><%@ page import="java.sql.*" %><%@ page import="java.io.*" %><%@ page import="com.bwm.db.Conn"%><%@ page import="com.bwm.page.Show" %><%request.setCharacterEncoding("GB2312");Show show=new Show();Conn con=new Conn();boolean blnError=true;boolean blnSend=false;String strTo ="";String strUserName=(String)session.getAttribute("name");String strUse=(String)session.getAttribute("use");System.out.println("send.jsp第19行。strUserName="+strUserName+",strUse="+strUse);String strSql="";if(strUse!=null&&strUse.equals("student")){ strSql="SELECT email FROM tb_student WHERE sname='"+strUserName+"'";}else if(strUse!=null&&strUse.equals("company")){ strSql="SELECT email FROM tb_company WHERE cname='"+strUserName+"'";}else { out.print(show.errorBox("你还没有登录不能发送Email","警告信息")); return;}System.out.println("send.jsp第29行。strSql="+strSql);ResultSet rs=con.getRs(strSql);if(rs.next()){ strTo=rs.getString(1); System.out.println("send.jsp第33行。strTo="+strTo);}//send EmailInternetAddress[] address = null;String strMailserver = "smtp.163.com";String [] arrstrFrom=request.getParameterValues("from");String strSubject = request.getParameter("subject");String strMessage = request.getParameter("message");System.out.println("send.jsp第41行。strSubject="+strSubject+",strMessage="+strMessage);if(arrstrFrom.length>0){ int intI=0; for(intI=0;intI<arrstrFrom.length;intI++){ if(!arrstrFrom[intI].equals("")){ try { System.out.println("send.jsp第47行。arrstrFrom[intI]="+arrstrFrom[intI]); // 设定所要用的Mail 服务器和所使用的传输协议 java.util.Properties props = System.getProperties(); props.put("mail.host",strMailserver); props.put("mail.transport.protocol","smtp"); // 产生新的Session 服务 javax.mail.Session mailSession = javax.mail.Session.getDefaultInstance(props,null); mailSession.setDebug(false); Message msg = new MimeMessage(mailSession); // 设定传送邮件的发信人 msg.setFrom(new InternetAddress(strTo)); // 设定传送邮件至收信人的信箱 address = InternetAddress.parse(arrstrFrom[intI],false); msg.setRecipients(Message.RecipientType.TO, address); // 设定信中的主题 msg.setSubject(strSubject); // 设定送信的时间 msg.setSentDate(new java.util.Date()); // 设定传送信的MIME Type msg.setText(strMessage); // 送信 Transport.send(msg); out.print("邮件已顺利传送到"+arrstrFrom[intI]+" <br>"); blnSend=true; }catch (Exception e) { if(blnError) out.print(show.errorBox(e.getMessage(),"发送失败")); blnError=false; e.printStackTrace(); } } }}else{ out.print(show.errorBox("请添写完整信息!","错误信息"));}if(blnSend){ out.print(show.errorBox("邮件发送成功!","发送信息"));}else if(blnError&!blnSend){ out.print(show.errorBox("邮件发送失败!","发送信息"));}%>控制台信息:send.jsp第19行。strUserName=1,strUse=studentsend.jsp第29行。strSql=SELECT email FROM tb_student WHERE sname='1'send.jsp第33行。strTo=canglang0@163.comsend.jsp第41行。strSubject=a,strMessage=asend.jsp第47行。arrstrFrom[intI]= canglang0@163.com javax.mail.SendFailedException: Sending failed; nested exception is:class javax.mail.MessagingException: 553 You are not authorized to send mail, authentication is requiredat javax.mail.Transport.send0(Transport.java:218)at javax.mail.Transport.send(Transport.java:80)at org.apache.jsp.mail.send_jsp._jspService(send_jsp.java:136)at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)at java.lang.Thread.run(Thread.java:619) 问题补充:梦中有你 写道
解决方案
javax.mail.SendFailedException: Sending failed; nested exception is:class javax.mail.MessagingException: 553 You are not authorized to send mail, authentication is required 身份验证失败,你的设置发送人的账号、密码有问题,还有你的代码怎么写在JSP上面哦??不会建个Class?那么乱。。。贴一段我写的JavaMail的Demo:package com._100fly.javamail;import java.util.Properties;import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transport;import javax.mail.Message.RecipientType;import javax.mail.internet.AddressException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;public class SencMail {/** * @param args * @throws MessagingException * @throws AddressException * @throws MessagingException */public static void main(String[] args) throws AddressException, MessagingException {// TODO Auto-generated method stubProperties props = new Properties();//两个属性必须配置//连哪种服务器props.setProperty("mail.transport.protocol", "smtp");//设置需要验证(给提供用户名、密码)props.setProperty("mail.smtp.auth", "true");props.setProperty("mail.host", "smtp.gmail.com");Session session = Session.getInstance(props,//返回验证信息的内部类new Authenticator(){protected PasswordAuthentication getPasswordAuthentication(){return new PasswordAuthentication("mengzhongyouni@gmail.com","zeng@4717");}});session.setDebug(true);Message msg = new MimeMessage(session);try {msg.setFrom(new InternetAddress("hello@gmail.com"));msg.setSubject("中文主题");msg.setRecipients(RecipientType.TO, InternetAddress.parse("1260128980@qq.com,mengzhongyouni@gmail.com"));msg.setContent("<span style='color:red'>我是以HTML代码解析出来的</span>", "text/html;charset=gbk");//没有指定收件人 会在msg里面去找Transport.send(msg);} catch (AddressException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (MessagingException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
解决方案二:
哈哈,难道是哪天在泡泡群,问了未果吗?
解决方案三:
没有权限发送发信人的密码没设,邮件服务器需要认证引用class javax.mail.MessagingException: 553 You are not authorized to send mail, authentication is required