gpt4 book ai didi

java - 发送消息导致异常

转载 作者:行者123 更新时间:2023-12-02 00:53:09 27 4
gpt4 key购买 nike

我尝试使用 Javamail API 发送消息,并使用 tomcat 作为网络服务器,但是每当我尝试发送没有文件和附件的消息时,以下代码都会导致一个大异常。尽管它可以将消息作为附件使用。

public static String send(String to,String body,Stringsubject,String file,String from)throws Exception{

if(file!=null||file!=" "){

File file1=new File(file);
MimeBodyPart mb=new MimeBodyPart();
FileDataSource f=new FileDataSource(file1.getCanonicalPath());
mb.setDataHandler(new DataHandler(f));
mb.setFileName(f.getName());
mm.addBodyPart(mb);
}

mb1.setText(body);
mm.addBodyPart(mb1);
message.setFrom(new InternetAddress(from));
Address[] add={ new InternetAddress(to) };

message.setRecipients(Message.RecipientType.TO,add);
message.setSubject(subject);
message.setContent(mm);
//message.setText(body);
Transport.send(message);
return "Message sent";
}

异常(exception):

javax.mail.MessagingException: IOException while sending message;
nested exception is:
java.io.FileNotFoundException: C:\Program Files\Apache Software Foundation\Tomcat 6.0 (Access is denied)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:779)
at javax.mail.Transport.send0(Transport.java:191)
at javax.mail.Transport.send(Transport.java:120)
at foo.SendMessage.send(SendMessage.java:57)
at foo.Mail.doPost(Mail.java:39)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
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:128)
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:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.io.FileNotFoundException: C:\Program Files\Apache Software Foundation\Tomcat 6.0 (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at javax.activation.FileDataSource.getInputStream(FileDataSource.java:82)
at javax.activation.DataHandler.writeTo(DataHandler.java:290)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1381)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:852)
at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:452)
at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:98)
at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:869)
at javax.activation.DataHandler.writeTo(DataHandler.java:302)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1381)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1742)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:737)
... 18 more

我的问题是,在将任何文件作为附件之前,我在上面的代码中使用了一个条件,那么为什么我会收到该异常?

最佳答案

if(file!=null||file!="") 不正确。我怀疑你想要的是 if (file != null && !file.trim().isEmpty())

具体说 if (file != null || file != "") 与说 if (true) 相同,因为您使用了 OR 运算符并且由于 file 不能同时具有值​​“”和 null,这些条件之一将计算为 true,从而使整个表达式为 true。

顺便说一句,file != "" 是一种不好的形式。当通过尝试测试相等性时,您应该始终使用 equals() 方法,而不是 ==!= 运算符。

关于java - 发送消息导致异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2096563/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com