View Javadoc

1   package org.telscenter.pas.ui.beans.issueReporter.actions;
2   
3   import java.awt.event.ActionEvent;
4   import java.security.Security;
5   import java.util.Date;
6   import java.util.Properties;
7   
8   import javax.mail.Message;
9   import javax.mail.PasswordAuthentication;
10  import javax.mail.Session;
11  import javax.mail.Transport;
12  import javax.mail.internet.InternetAddress;
13  import javax.mail.internet.MimeMessage;
14  import javax.swing.ImageIcon;
15  
16  import org.telscenter.pas.steps.actions.DialogSaveAction;
17  import org.telscenter.pas.ui.beans.issueReporter.Issue;
18  import org.telscenter.pas.ui.beans.issueReporter.IssueUI;
19  import org.telscenter.pas.ui.dialog.PasDialogManager;
20  import org.telscenter.pas.ui.dialog.PasIssueReporterConfirmDialogUI;
21  
22  public class ReportIssueAction extends DialogSaveAction {
23  
24  	private IssueUI issueUI;
25  
26  	private boolean wasSent = false;
27  
28  	protected Issue currentIssue;
29  
30  	public ReportIssueAction(ImageIcon imageIcon) {
31  		super(imageIcon);
32  	}
33  
34  	public ReportIssueAction(String string, IssueUI issueUI) {
35  		super(string);
36  		this.issueUI = issueUI;
37  	}
38  
39  	public ReportIssueAction(IssueUI issueUI) {
40  		super(Messages.getString("ReportIssueAction.action_name")); //$NON-NLS-1$
41  		this.issueUI = issueUI;
42  	}
43  
44  	public void actionPerformed(ActionEvent e) {
45  		makeIssue();
46  		boolean result = submitIssueEmail();
47  		if (result == true) {
48  			hideRootDialog(e.getSource());
49  		}
50  	}
51  
52  	public void makeIssue() {
53  		currentIssue = issueUI.getCurrentIssue();
54  	}
55  
56  	public boolean submitIssueEmail() {
57  		// configure the jvm to use the jsse security.
58  		//Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
59  		boolean debug = false;
60  		final String SMTP_HOST_NAME = Messages.getString("ReportIssueAction.0"); //$NON-NLS-1$
61  		final String SMTP_PORT = Messages.getString("ReportIssueAction.1"); //$NON-NLS-1$
62  		final String emailFromAddress = Messages.getString("ReportIssueAction.2"); //$NON-NLS-1$
63  		final String SSL_FACTORY = Messages.getString("ReportIssueAction.3"); //$NON-NLS-1$
64  		final String username = Messages.getString("ReportIssueAction.4"); //$NON-NLS-1$
65  		final String password = Messages.getString("ReportIssueAction.5"); //$NON-NLS-1$
66  		final String[] sendTo = { Messages.getString("ReportIssueAction.6") }; //$NON-NLS-1$
67  		final String mailDebug = Messages.getString("ReportIssueAction.7"); //$NON-NLS-1$
68  		final String fallback = Messages.getString("ReportIssueAction.8"); //$NON-NLS-1$
69  		final String auth = Messages.getString("ReportIssueAction.9"); //$NON-NLS-1$
70  		Properties props = new Properties();
71  
72  		props.put("mail.smtp.host", SMTP_HOST_NAME); //$NON-NLS-1$
73  		props.put("mail.smtp.auth", auth); //$NON-NLS-1$
74  		props.put("mail.debug", mailDebug); //$NON-NLS-1$
75  		props.put("mail.smtp.port", SMTP_PORT); //$NON-NLS-1$
76  		props.put("mail.smtp.socketFactory.port", SMTP_PORT); //$NON-NLS-1$
77  		props.put("mail.smtp.socketFactory.class", SSL_FACTORY); //$NON-NLS-1$
78  		props.put("mail.smtp.socketFactory.fallback", fallback); //$NON-NLS-1$
79  
80  		Session mailSession = Session.getDefaultInstance(props,
81  				new javax.mail.Authenticator() {
82  
83  					protected PasswordAuthentication getPasswordAuthentication() {
84  						return new PasswordAuthentication(username, password);
85  					}
86  				});
87  
88  		mailSession.setDebug(debug);
89  		PasIssueReporterConfirmDialogUI ui = new PasIssueReporterConfirmDialogUI();
90  		try {
91  
92  			Message msg = new MimeMessage(mailSession);
93  			msg.setFrom(new InternetAddress(emailFromAddress)); //$NON-NLS-1$
94  			InternetAddress[] sendToAddress = { new InternetAddress(sendTo[0]) }; //$NON-NLS-1$
95  			msg.setRecipients(Message.RecipientType.TO, sendToAddress);
96  
97  			msg.setSubject(Messages
98  					.getString("ReportIssueAction.email.subject")); //$NON-NLS-1$
99  			msg.setSentDate(new Date());
100 			// If the desired charset is known, you can use
101 			// setText(text, charset)
102 			msg.setText(currentIssue.toString());
103 
104 			Transport.send(msg);
105 
106 			ui.setMessage(Messages.getString("ReportIssueAction.sent")); //$NON-NLS-1$
107 
108 			wasSent = true;
109 		} catch (Exception e) {
110 			ui.setMessage(Messages.getString("ReportIssueAction.error")); //$NON-NLS-1$
111 			e.printStackTrace();
112 			wasSent = false;
113 		} finally {
114 			PasDialogManager.showPasMessageDialog("", ui.createDialogPanel(), //$NON-NLS-1$
115 					null);
116 		}
117 		return wasSent;
118 	}
119 
120 }