合并外部jar

时间:2016-06-19 08:18:46

标签: java javamail

我在Netbeans IDE中创建了简单的Mail send java项目。我在lib文件夹中添加了一个外部jar javax.mail.jar 。我想将多个JAR合并到一个Jar中但 javax.mail.jar 没有合并。请帮助我如何合并 javax.mail.jar

源代码:

package nrb.vanggi;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Properties;
 import java.util.concurrent.ThreadLocalRandom;
 import javax.mail.BodyPart;
 import javax.mail.Message;
 import javax.mail.MessagingException;
 import javax.mail.PasswordAuthentication;
 import javax.mail.Session;
 import javax.mail.Transport;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeBodyPart;
 import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Example extends JFrame {
Session session = null;

public Example() {
    initUI();
}

public final void initUI() {

    JPanel panel = new JPanel();
    getContentPane().add(panel);

    panel.setLayout(null);

    JButton send = new JButton("Send");
    send.setBounds(50, 60, 80, 30);
    send.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            startingMailSend();

            emailSending();

        }
    });

    panel.add(send);

    setSize(300, 200);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private void startingMailSend() {

    //1) get the session object     
    Properties properties = System.getProperties();
    properties.setProperty("mail.smtp.host", "");
    properties.put("mail.smtp.auth", "true");

    //properties.put("mail.smtp.port", "465");
    //properties.put("mail.smtp.ssl.enable", "true");
    session = Session.getDefaultInstance(properties,
            new javax.mail.Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("rahim@duttafashion.info", "**********");
                }
            });

}

private void emailSending() {

    try {
        System.err.println("call");
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("hh:mm:ss dd, MMM");

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress("UserName" + "<" + "rahim@duttafashion.info" + ">"));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress("vanggibd@gmail.com"));
        message.setSubject(ThreadLocalRandom.current().nextLong(5000, 10000) + " " + "This is Subject");

        MimeMultipart multipart = new MimeMultipart("related");
        BodyPart messageBodyPart1 = new MimeBodyPart();
        messageBodyPart1.setContent(df.format(cal.getTime()) + "" + "Body area How are all????", "text/html");
        multipart.addBodyPart(messageBodyPart1);

        message.setContent(multipart);
        Transport.send(message);
        System.out.println("message sent....");
        JOptionPane.showMessageDialog(null, "Mail send success");
    } catch (MessagingException ex) {
        JOptionPane.showMessageDialog(null, ex);
        ex.printStackTrace();
        System.out.println("error");
    } catch (Exception ee) {
        System.err.println(" errorrrr" + ee);
    } finally {
        System.err.println("finally call");
    }

}

public static void main(String[] args) {

    Example ex = new Example();
    ex.setVisible(true);

}

}

Sample build.xml configure:
<target name="-post-jar">
     <jar jarfile="dist/MailSend.jar">
      <zipfileset src="${dist.jar}" excludes="META-INF/*" />   
     <zipfileset src="lib/javax.mail.jar" excludes="META-INF/*"/>
     <manifest>
         <attribute name="Main-Class" value="nrb.vanggi.Example"/>
     </manifest>
     </jar>
    </target>

0 个答案:

没有答案