Error java.io.FileNotFoundException 发送电子邮件 RPGLE/Java

huangapple go评论80阅读模式
英文:

Error java.io.FileNotFoundException sending an email RPGLE/Java

问题

我正在开发一个使用 javax.mail 的类,该类从我的 AS400 RPGLE 程序中接收一些参数,然后发送一封包含 主题正文附件 的电子邮件。

在参数中,我传递了服务器的 路径(例如:'\\192.0.0.100\docs\new')以及将要附加的文件的 名称(例如:File.pdf),在这种情况下是一个 .pdf 文件。为了读取服务器内的文件,我使用 SmbFile 进行身份验证,传递了 用户密码

我已经多次在本地运行代码进行了测试,在我的 Eclipse 中从主类调用方法并传递硬编码的参数,它可以成功发送电子邮件。但问题出现在我将类部署到生产环境时,我会收到一个 "javax.mail.MessagingException: IOException while sending message;
nested exception is: java.io.FileNotFoundException: \\192.0.0.100\docs\new\File.pdf(文件或目录不存在于该名称中。)
" 的错误。

有趣的是,错误发生在程序调用 Transport.send(message); 之时,在发送电子邮件之前,我添加了 if(sFileReader.exists()) 来判断是否有错误。

我还在代码中添加了一些日志来监视通过变量传递的值,它们都是正确的,与我在本地测试中使用的值完全相同。我还尝试在生产环境中发送不带附件的电子邮件,它可以正常工作。

你们有任何想法吗?非常感谢。

以下是我的代码示例以及我如何在本地调用我的方法:

public static void main(String[] args) throws Exception {
    sendEmail("192.0.0.100", "100", "user", "pwd", "test@email.com", 
              "test@email.com", "Subject", "Body", "smb://192.0.0.100/doc/new/", "File.PDF",
              "domain", "user", "pwd");
}

public static void sendEmail(String server_ip, String server_port, final String username_mail, final String password_mail, String EMAIL_FROM, String list_emails, String subject, String body, String attachment_path, String attachment_name, String domain_attach, String user_attach, String password_attach) throws AddressException, MessagingException {
    Session session = null;
    Properties properties = null;
    MimeMessage message = null;
    MimeBodyPart attachmentBodyPart = null, textBodyPart = null;
    Multipart multipart = null;
    NtlmPasswordAuthentication auth_attach = null;
    SmbFile sPathReader = null, sFileReader = null;
    DataSource source = null;

    properties = System.getProperties();  
    properties.setProperty("mail.smtp.host", server_ip);
    properties.put("mail.smtp.port", server_port);

    if(username_mail != "" && password_mail != "") {
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username_mail, password_mail);
            }
        };
        session = Session.getInstance(properties, auth);
    } else {
        session = Session.getDefaultInstance(properties);  
    }

    try {        	
        // Authenticate to the server
        auth_attach = new NtlmPasswordAuthentication(domain_attach, user_attach, password_attach);
        sPathReader = new SmbFile(attachment_path, auth_attach);
        sFileReader = new SmbFile(sPathReader, attachment_name);

        if(sFileReader.exists()) {
            message = new MimeMessage(session);  
            message.setFrom(new InternetAddress(EMAIL_FROM));
            
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(list_emails)); 
            
            message.setSubject(subject); 
            
            textBodyPart = new MimeBodyPart();
            
            textBodyPart.setText(body);
            
            attachmentBodyPart = new MimeBodyPart();
            multipart = new MimeMultipart();	            
            source = new FileDataSource(sFileReader.getUncPath());
            
            attachmentBodyPart.setDataHandler(new DataHandler(source));
            attachmentBodyPart.setFileName(attachment_name);
        	
            multipart.addBodyPart(textBodyPart);
            multipart.addBodyPart(attachmentBodyPart);

            message.setContent(multipart);

            // Send message  
            Transport.send(message);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
英文:

I'm developing a class that uses javax.mail which simply receives some parameters from my AS400 RPGLE program and sends an email that contains a Subject, Body and an Attachment.

In the parameters I'm passing the path of a server (ex: '\\192.0.0.100\docs\new') and the name (ex: File.pdf) of the file that will be attached, which in this case it's a .pdf file. To read the file inside the server I'm authenticating using SmbFile passing the domain, user and password.

I've tested the code several times running it locally in my eclipse calling the method from my main class passing the parameters hardcoded and it sends the email successfully, the problem starts when I deploy my class in production, I get a "javax.mail.MessagingException: IOException while sending message;
nested exception is: java.io.FileNotFoundException: \\192.0.0.100\docs\new\File.pdf (A file or a directory in the name does not exist.)
".

The interesting part is that the error comes when the program calls Transport.send(message);, before sending the email I've put a if(sFileReader.exists()) and it gives no error.

I've also put some logs to monitor the values passed through the variables and they are all correct, exactly as the ones that I was using for my local tests. Tried also in production to send an email without an attachment and it works.

Do you guys have any idea? Thanks a lot.

Below an example of my code and how I was calling my method locally:

public static void main(String[] args) throws Exception {
sendEmail("192.0.0.100", "100", "user", "pwd", "test@email.com", 
"test@email.com", "Subject", "Body", "smb://192.0.0.100/doc/new/", "File.PDF",
"domain", "user", "pwd");
}
public static void sendEmail(String server_ip, String server_port, final String username_mail, final String password_mail, String EMAIL_FROM, String list_emails, String subject, String body, String attachment_path, String attachment_name, String domain_attach, String user_attach, String password_attach) throws AddressException, MessagingException {
Session session = null;
Properties properties = null;
MimeMessage message = null;
MimeBodyPart attachmentBodyPart = null, textBodyPart = null;
Multipart multipart = null;
NtlmPasswordAuthentication auth_attach = null;
SmbFile sPathReader = null, sFileReader = null;
DataSource source = null;
properties = System.getProperties();  
properties.setProperty("mail.smtp.host", server_ip);
properties.put("mail.smtp.port", server_port);
if(username_mail != "" && password_mail != "") {
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username_mail, password_mail);
}
};
session = Session.getInstance(properties, auth);
}else {
session = Session.getDefaultInstance(properties);  
}
try {	        	
// Authenticate to the server
auth_attach = new NtlmPasswordAuthentication(domain_attach, user_attach, password_attach);
sPathReader = new SmbFile(attachment_path, auth_attach);
sFileReader = new SmbFile(sPathReader, attachment_name);
if(sFileReader.exists()) {
message = new MimeMessage(session);  
message.setFrom(new InternetAddress(EMAIL_FROM));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(list_emails)); 
message.setSubject(subject); 
textBodyPart = new MimeBodyPart();
textBodyPart.setText(body);
attachmentBodyPart = new MimeBodyPart();
multipart = new MimeMultipart();	            
source = new FileDataSource(sFileReader.getUncPath());
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(attachment_name);
multipart.addBodyPart(textBodyPart);
multipart.addBodyPart(attachmentBodyPart);
message.setContent(multipart);
// Send message  
Transport.send(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}

答案1

得分: 2

我发现了问题,我的程序对于需要从 Windows 服务器获取文件的情况工作正常,但在我的情况下,我是从 AS400 使用 RPGLE 程序调用我的类,它有一种不同的方式来访问外部 URL,它需要在路径中添加 "/QNTC/",例如 "/QNTC/192.0.0.100/docs/test.pdf",在 Java 中不需要身份验证,IBM 会处理,所以在这种情况下只需要一个包含 URL 的简单字符串。

IBM 对此有很好的文档,说明了如何在 IFS 下配置 QNTC 路径。

希望对某人有帮助。

英文:

I've found out the problem, my program works correctly for the ones that need to get a file from a server in windows, but in my case I was calling my class from AS400 using a RPGLE program and it has a different way of accessing external urls, it needs a "/QNTC/" in the path, for example "/QNTC/192.0.0.100/docs/test.pdf", it's not needed to authenticate in Java, IBM will do it, so in this case a simple String containing the URL was needed.

IBM has a good documentation about it, on how to configure under IFS the QNTC paths.

I hope it helps somebody.

huangapple
  • 本文由 发表于 2020年10月8日 15:36:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64257839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定