[英]How to use libraries correctly in Netbeans (Java)?
這個問題是關於用Java編程時如何處理Netbeans中的庫的。
我有一個Java項目,我們稱之為ABC。 它的活動之一是發送電子郵件。 我的其他一些Java項目也發送電子郵件,因此我決定創建一個單獨的Java項目來發送消息。 該項目稱為SendEmail。 SendEmail使用外部jar文件(javax.mail。*)。 通過轉到SendEmail的項目屬性->庫->添加JAR,可以包含這些文件。 測試SendEmail可以正常工作:調用其方法sendMail(title,contents)時,我是否收到發送的電子郵件。
Project ABC使用SendEmail,因此我已將其添加到ABC的庫中:project Properties-> Libraries-> Add Project。 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的庫中。
在您的代碼中,沒有身份驗證器部分。 此代碼只能使用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:庫 :1. javax.activation 2. javax.mail
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.