无法为java邮件api添加附件

时间:2016-04-27 20:12:41

标签: java javamail

我正在尝试使用java mail api将txt文件作为附件发送。     代码是

         PrintWriter out = response.getWriter();
         out.println("Hiii from doget");
         String[] toAddress = {"abc@gmail.com"};

         boolean debug = false;


         String smtpServer = "smtp.srahosting.com";

         // Set the host smtp address
         Properties props = new Properties();
         props.put( "mail.smtp.host", smtpServer );
         props.put("mail.smtp.auth", "false"); 
         props.put("mail.smtp.port", "587");

         // create some properties and get the default Session
         Session session = Session.getInstance(props, null);
         session.setDebug(debug);

         // create a message
         MimeMessage msg = new MimeMessage(session);
         InternetAddress addressFrom;
        try {
            addressFrom = new InternetAddress("xyz@gmail.com");
            msg.setFrom(addressFrom);
         InternetAddress[] addressTo = new InternetAddress[toAddress.length];
         for (int i = 0; i < toAddress.length; i++) {
                        addressTo[i] = new InternetAddress( toAddress[i] );
         } // for
         msg.setRecipients(Message.RecipientType.TO, addressTo);

         msg.addHeader( "MyHeaderName", "myHeaderValue" );
         msg.setSubject("This is the Subject Line!");
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText("This is message body");
         Multipart multipart = new MimeMultipart();
         multipart.addBodyPart(messageBodyPart); 
         MimeBodyPart messageBodyPart2 = new MimeBodyPart();
         File filename = new File(request.getRealPath("/Test.java"));
         DataSource source = new FileDataSource(filename);
         messageBodyPart2.setDataHandler(new DataHandler(source));
      //   messageBodyPart2.setFileName("Ammu");
         Multipart multipart2 = new MimeMultipart();
         multipart.addBodyPart(messageBodyPart2);
         msg.setContent(multipart);
         try{
         Transport.send( msg );}
         catch(Exception e){
             e.printStackTrace();
             out.println("In exception "+"***"+e.getClass().getName());
             for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
                 out.println(ste);
              }
         }
         logThis("msg sent ");
            String title = "Send Email";
         String res = "Sent message successfully....";
         String docType =
         "<!doctype html public \"-//w3c//dtd html 4.0 " +
         "transitional//en\">\n";
         out.println(docType +
         "<html>\n" +
         "<head><title>" + title + "</title></head>\n" +
         "<body bgcolor=\"#f0f0f0\">\n" +
         "<h1 align=\"center\">" + title + "</h1>\n" +
         "<p align=\"center\">" + res + "</p>\n" +
         "</body></html>"); 
         }
        catch (MessagingException mex){
            logThis("In exception block");
            mex.printStackTrace();
        }
        }

没有依恋,工作正常。当我添加附件时,它会给出这些错误

**
VM.doGet(VaersMail.java:98)
VM.doPost(VaersMail.java:30)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
org.apache.coyote.ajp.AjpAprProcessor.process(AjpAprProcessor.java:188)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2522)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2511)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Th`enter code here`read.run(Thread.java:745)**

任何人都可以帮忙。我从网上尝试了很多代码。我正在使用servlet。 Auth = false,添加端口号。在此更改之前,代码也无法正常工作。如果我评论

     Multipart multipart2 = new MimeMultipart();
     multipart.addBodyPart(messageBodyPart2);

工作正常。 如果Auth或端口有任何问题,它也可以在没有附件的情况下工作吗?

0 个答案:

没有答案