0

我发现一个教程可以帮助我创建一个邮件发件人,在第一次尝试时工作正常,但是在我关闭 netbeans 并再次尝试之后,oups,有一个错误,我无法弄清楚,netbeans 通知:connot find symbol , symbol attachFileString,

这是我的电子邮件代码

import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class Emailer {

    public static void sendEmailWithAttachments(String host, String port,
            final String userName, final String password, String toAddress,
            String subject, String message, String[] attachFiles)
            throws AddressException, MessagingException {
        // sets SMTP server properties
        Properties properties = new Properties();
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", port);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.user", userName);
        properties.put("mail.password", password);

        // creates a new session with an authenticator
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
            }
        };
        Session session = Session.getInstance(properties, auth);

        // creates a new e-mail message
        Message msg = new MimeMessage(session);

        msg.setFrom(new InternetAddress(userName));
        InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
        msg.setRecipients(Message.RecipientType.TO, toAddresses);
        msg.setSubject(subject);
        msg.setSentDate(new Date());

        // creates message part
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent(message, "text/html");

        // creates multi-part
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);

        // adds attachments
        if (attachFiles != null && attachFiles.length > 0) {
            for (String filePath : attachFiles) {
                MimeBodyPart attachPart = new MimeBodyPart();

                try {
                    attachPart.attachFile(filePath);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

                multipart.addodyPart(attachPart);
            }
        }

        // sets the multi-part as e-mail's content
        msg.setContent(multipart);

        // sends the e-mail
        Transport.send(msg);

    }

}

错误是

java.lang.RuntimeException: Uncompilable source code - exception java.io.IOException is never thrown in body of corresponding try statement
    at Emailer.sendEmailWithAttachments(Emailer.java:63)

第 63 行(Emailer.java:63)是:

attachPart.attachFile(filePath);

非常感谢

4

2 回答 2

1

您指出的行上似乎没有任何编译问题。不过这里有一个错字:

multipart.addodyPart(attachPart);

它应该读

multipart.addBodyPart(attachPart);

此外,仅在 JavaMail 1.4 中添加了MimeBodyPart.#attachFile 。如果您使用的是旧版本,请确保获取版本。

于 2013-03-03T20:30:17.037 回答
0

删除语句周围的 try catch 块并尝试再次编译它。正如异常所解释的,您调用的方法不会引发该异常,并且当您尝试捕获无法发生的非运行时异常时会发生编译错误。

编辑:或者它可能是错字。

于 2013-03-03T20:29:15.667 回答