2013-03-03 43 views
0

我发现一个教程帮助我创建一个邮件发送器,在第一次尝试工作正常,但在关闭netbeans并再次尝试,oups后,有一个错误,我可以“吨找出和NetBeans通知:connot find symbol , symbol attachFileString,JavaMail无法找到符号,不可编译源

这是我的电子邮件使用者代码

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); 

非常感谢

+0

你说的是两个错误。前者说'try/catch'块不需要,因为'attachFile()'方法不能抛出'IOException'。 – SJuan76 2013-03-03 20:29:04

+0

如果我消除try/catch它会给我这个错误,java.lang.RuntimeException:不可编译的源代码 - 错误的sym类型:javax.mail.internet.MimeBodyPart.attachFile \t at Emailer.sendEmailWithAttachments(Emailer.java:64) – 2013-03-03 20:41:40

回答

1

在您指示的行上似乎没有任何编译问题。有一个错字虽然这里:

multipart.addodyPart(attachPart); 

它应该阅读

multipart.addBodyPart(attachPart); 

此外,MimeBodyPart.#attachFile只在JavaMail的1.4增加。如果您使用的是旧版本,请确保获得这个版本。

+0

我纠正它,但同样的错误:/ – 2013-03-03 20:43:30

+0

你使用的是什么版本的'JDK'和什么版本的'JavaMail'?我在'JM 1.4'上使用'JDK 1.7'。 – Reimeus 2013-03-03 20:46:53

+0

我使用JDK 7和JavaMail包含在JDK 7 – 2013-03-03 20:51:18

0

删除周围的语句try catch块,并尝试重新编译。如例外解释,您调用的方法不会抛出该异常,并且当您尝试捕获不可能发生的非运行时异常时会发生编译错误。

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

+0

如果我消除try/catch它给了我这个错误,java.lang.RuntimeException:不可编译的源代码 - 错误的sym类型:javax.mail.internet.MimeBodyPart.attachFile在Emailer.sendEmailWithAttachments(Emailer.java:64) – 2013-03-03 20:44:23

+0

@Reimeus哦酷它的好,删除并重新加载我的电子邮件库和它的goine再次感谢大家的帮助 – 2013-03-03 21:04:15