2017-10-14 79 views
0

这个问题是关于如何在Java中编程时在Netbeans中处理库。如何在Netbeans(Java)中正确使用库?

我有一个Java项目,我们称之为ABC。其活动之一是发送电子邮件。我的一些其他Java项目也发送电子邮件,所以我决定创建一个单独的Java项目来发送消息。这个项目被称为SendEmail。 SendEmail使用外部jar文件(javax.mail。*)。通过转到SendEmail的项目“属性” - >“库” - >“添加JAR”包含这些内容。测试SendEmail工作正常:当调用sendMail(标题,内容)方法时,我会收到发送的电子邮件。

项目ABC使用SendEmail,所以我已经添加到ABC的库:项目属性 - >库 - >添加项目。 ABC编译并运行良好,直到它达到它想要发送电子邮件的地步:它崩溃。

private void informUser(){ 
//create message title 
//create message contents 
SendEmail email = new SendEmail(); 
email.sendMail(title, contents); // <- it crashes here 
} 

错误信息指出它找不到Authenticator类。该类位于SendEmail库中包含的外部jar文件中。 我只能通过将外部jar文件包含到ABC的库中来避免发生这种崩溃。这是我没有想到的必要。 ABC不使用这些外部jar文件,只有SendEmail可以。

我的问题:我做错了什么?我认为ABC不使用这些外部jar,所以他们不需要在ABC的库中。

回答

0

在你的代码中没有验证器部分。此代码只能使用Gmail电子邮件,您可以更改smtp服务器选项。我的代码:

import java.io.ByteArrayInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.security.Security; 
import java.util.Properties; 

import javax.activation.DataHandler; 
import javax.activation.DataSource; 
import javax.mail.Message; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 


public class GmailSender extends javax.mail.Authenticator { 

    private String user; 
    private String password; 
    private Session session; 

    static { 
     Security.addProvider(new JSSEProvider()); 
    } 

    public GmailSender(String user, String password) { 
     this.user = user; 
     this.password = password; 

     Properties props = new Properties(); 
     props.setProperty("mail.transport.protocol", "smtp"); 
     props.setProperty("mail.smtp.host", "smtp.gmail.com"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.port", "465"); 
     props.put("mail.smtp.socketFactory.port", "465"); 
     props.put("mail.smtp.socketFactory.class", 
       "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.socketFactory.fallback", "false"); 
     props.setProperty("mail.smtp.quitwait", "false"); 

     session = Session.getDefaultInstance(props, this); 
    } 

    protected PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication(user, password); 
    } 

    public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception { 
     try { 
      MimeMessage message = new MimeMessage(session); 
      DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain")); 
      message.setSender(new InternetAddress(sender)); 
      message.setSubject(subject); 
      message.setDataHandler(handler); 
      if (recipients.indexOf(',') > 0) 
       message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients)); 
      else 
       message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients)); 
      Transport.send(message); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public class ByteArrayDataSource implements DataSource { 
     private byte[] data; 
     private String type; 

     public ByteArrayDataSource(byte[] data, String type) { 
      super(); 
      this.data = data; 
      this.type = type; 
     } 

     public ByteArrayDataSource(byte[] data) { 
      super(); 
      this.data = data; 
     } 

     public void setType(String type) { 
      this.type = type; 
     } 

     public String getContentType() { 
      if (type == null) 
       return "application/octet-stream"; 
      else 
       return type; 
     } 

     public InputStream getInputStream() throws IOException { 
      return new ByteArrayInputStream(data); 
     } 

     public String getName() { 
      return "ByteArrayDataSource"; 
     } 

     public OutputStream getOutputStream() throws IOException { 
      throw new IOException("Not Supported"); 
     } 
    } 


} 

而且你需要这个类:

import java.security.AccessController; 
import java.security.Provider; 

public class JSSEProvider extends Provider { 
    public JSSEProvider() { 
     super("HarmonyJSSE", 1.0, "Harmony JSSE Provider"); 
     AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() { 
      public Void run() { 
       put("SSLContext.TLS", 
         "org.apache.harmony.xnet.provider.jsse.SSLContextImpl"); 
       put("Alg.Alias.SSLContext.TLSv1", "TLS"); 
       put("KeyManagerFactory.X509", 
         "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl"); 
       put("TrustManagerFactory.X509", 
         "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl"); 
       return null; 
      } 
     }); 
    } 
} 

对于使用此代码:

class SendEmailTask extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 
      try { 
       GmailSender sender = new GmailSender("from email", "from email password"); 
       //subject, body, sender, to 
       StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
       StrictMode.setThreadPolicy(policy); 
       sender.sendMail("your title", 
         "your content", 
         "from email", 
         "to email"); 
      } catch (Exception e) {    
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 

     } 
    } 

和运行

SendEmailTask sendEmailTask = new SendEmailTask(); 
sendEmailTask.execute(); 

UPD1: Libs: 1. javax.activation 2. javax.mail

+0

对不起,但我不认为这回答了我的问题。我有一个可以发送电子邮件的程序,正如我在问题中解释的那样。我的问题涉及在另一个Netbeans项目中使用此电子邮件发送程序以及该项目中所需的库设置。 – Joe