-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailSend.java
More file actions
125 lines (97 loc) · 3.84 KB
/
EmailSend.java
File metadata and controls
125 lines (97 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import javazoom.jl.player.Player;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.sound.sampled.*;
import javax.swing.*;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
public class EmailSend {
private final int BUFFER_SIZE = 128000;
private File soundFile;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceLine;
public Connect connect = new Connect();
public JFrame frame = new JFrame("Email");
public JPanel panel;
public void emailFrame() {
frame.setLayout(null);
//panel.setLayout();
JLabel label = new JLabel("Recipient ");
label.setBounds(50,90,90,50);
frame.add(label);
JTextField reciepentAddress = new JTextField(20);
reciepentAddress.setBounds(130, 100, 140,25);
frame.add(reciepentAddress);
JButton button = new JButton("Send");
button.setBounds(140,160,110,30);
frame.add(button);
button.addActionListener(e-> {
String x = connect.displayInventory();
sendMail(reciepentAddress.getText() , x);
});
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setVisible(true);
}
public void sendMail(String recipient, String table) {
// Recipient's email ID needs to be mentioned.
String to = recipient;
// Sender's email ID needs to be mentioned
//make sure to allow unsecure connections
String from = "YourEmail@email.com";
// Assuming you are sending email from through gmails smtp
String host = "smtp.gmail.com";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.auth", "true");
// Get the Session object.// and pass username and password
Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("YourEmail@gmail.com", "YourPassword");
}
});
// Used to debug SMTP issues
session.setDebug(true);
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject("Inventory Table");
// Now set the actual message
message.setText(table);
System.out.println("sending...");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
frame.dispose();
playSound("1312.mp3");
JOptionPane.showMessageDialog(null, "Message sent successfully");
} catch (MessagingException mex) {
JOptionPane.showMessageDialog(null, mex);
//mex.printStackTrace();
}
}
public void playSound(String filename){
try {
BufferedInputStream buffer = new BufferedInputStream(
new FileInputStream(filename));
Player player = new Player(buffer);
player.play();
}
catch (Exception e) {
System.out.println(e);
}
}
}