Skip to content
Permalink
faf025cfc2
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
360 lines (330 sloc) 17.6 KB
package utilities;
import java.awt.HeadlessException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import database.TicketQueries;
import entities.Ticket;
import entities.User;
public class Mail {
private String sender = "Senior.design.synchrony.test@gmail.com";
private String password = "synchrony123";
private User client;
private String profileLink = "https://seniordesign.cfapps.io/html/webpages/profileSettings.jsp";
private String ticketLink = "";
public Mail(User client){
this.client=client;
}
//Sends an email to address specified by recipient param
// messegeIndex param is for choosing which
public void sendTicketConfirmation(int[] ticketIDs) throws IOException, InterruptedException {
String subject = "Your test device order";
// I really want to read this from a seperate file, but even when it's in the same directory eclipse insists on moving everything around :(
String msg = "<!DOCTYPE html>"
+ "<html>"
+ "<body>"
+ "<h4>We've received your order! We'll let you know when your device{s} ship{!s}.</h4>"
+ "<p>The following ticket{s} {was/were} generated:</p>"
+ "<div name = 'tickets' style='text-indent: 20px'>"
+ "{TICKETS GO HERE}"
+ "</div>"
+ "</body>"
+ "<footer style='text-align: center;'><font size='1'>To change notification settings, please visit your <a href='"+profileLink+"'>profile settings page</a></font></footer>"
+ "</html>";
// The messege needs to be tailored to make sense
msg=msg.replace("{s}",(ticketIDs.length==1 ? "" : "s")); //make occurances of 'device' and 'ticket' plural or singular
msg=msg.replace("{was/were}",ticketIDs.length==1 ? "was" : "were"); //use appropriate verbs
msg=msg.replace("{!s}",ticketIDs.length==1 ? "s" : "s"); //verbs again
String tickethtml = "";
for(int tickID : ticketIDs)
{
tickethtml+="<p>Ticket #<a href='"+ticketLink+"'>"+tickID+"</a></p>";
}
msg=msg.replace("{TICKETS GO HERE}",tickethtml);
Properties properties = System.getProperties();
properties = setProp(sender, client.getEmail());
Session session = Session.getInstance(properties, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(sender, password);
}
});
try {
Address address = new InternetAddress(client.getEmail());
MimeMessage message = new MimeMessage(session);
message.setFrom(address);
message.addRecipient(Message.RecipientType.TO, address);
message.setSubject(subject);
message.setText(msg,"utf-8","html");
message.saveChanges();
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (Exception mex) {
mex.printStackTrace();
}
}
public void sendTicketApproval(Ticket[] tickets) throws IOException, InterruptedException {
String subject = "{DEVICE(S)} {is/are(3)} on the way!";
String messege = "<!DOCTYPE html>"
+ "<html>"
+ "<body>"
+ "<h4>Your ticket{s} {have/has} been approved!</h4>"
+ "<p>I hope you're exicted, because the device{s} you ordered {is/are} about to ship. Happy testing!</p>"
+ "{list_header}"
+ "<div name = 'tickets' style='text-indent: 20px'>"
+ "{TICKETS GO HERE}"
+ "</div>"
+ "</body>"
+ "<footer style='text-align: center;'><font size='1'>To change notification settings, please visit your <a href='"+profileLink+"'>profile settings page</a></font></footer>"
+ "</html>";
subject=subject.replace("{DEVICE(S)}", tickets[0].getDeviceName() + (tickets.length>1 ? " and "+(tickets.length-1)+" more" : ""));
subject=subject.replace("{is/are(3)}",tickets.length>2 ? "are" : "is");
messege=messege.replace("{s}",tickets.length>1 ? "s" : "");
messege=messege.replace("{have/has}",tickets.length>1 ? "have" : "has");
messege=messege.replace("{is/are}",tickets.length>1 ? "are" : "is");
messege=messege.replace("{list_header}",tickets.length>1 ? "The following tickets were approved:" : "");
String ticketList = "";
if(tickets.length>1)
{
for (Ticket tic : tickets) {
ticketList+="<p>Ticket #<a href='"+ticketLink+"'>"+tic.getId()+"</a> - "+tic.getDeviceName()+"</p>";
}
}
messege=messege.replace("{TICKETS GO HERE}",ticketList);
Properties properties = System.getProperties();
properties = setProp(sender, client.getEmail());
Session session = Session.getInstance(properties, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(sender, password);
}
});
try {
Address address = new InternetAddress(client.getEmail());
MimeMessage message = new MimeMessage(session);
message.setFrom(address);
message.addRecipient(Message.RecipientType.TO, address);
message.setSubject(subject);
message.setText(messege,"utf-8","html");
message.saveChanges();
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (Exception mex) {
mex.printStackTrace();
}
}
public void sendTicketRejection(Ticket[] tickets) throws IOException, InterruptedException{
String subject = "Bad news, we've had to reject your order.";
String messege = "<!DOCTYPE html>"
+ "<html>"
+ "<body>"
+ "<h4>Unfortunately, we couldn't deliver on your recent order. The following ticket{s} {have/has} been rejected:</h4>"
+ "<div name = 'tickets' style='text-indent: 20px'>"
+ "{TICKETS GO HERE}"
+ "</div>"
+ "<p>There may have been an availablilty conflict. You can try submitting a new order for{ a} similar device{s},"
+ " or for more information you can contact an administrator.</p>"
+ "</body>"
+ "<footer style='text-align: center;'><font size='1'>To change notification settings, please visit your <a href='"+profileLink+"'>profile settings page</a></font></footer>"
+ "</html>";
messege=messege.replace("{s}",tickets.length>1 ? "s" : "");
messege=messege.replace("{have/has}",tickets.length>1 ? "have" : "has");
messege=messege.replace("{ a}",tickets.length>1 ? "" : " a");
String ticketText = "";
for (Ticket t : tickets) {
ticketText+="<p>Ticket #<a href='"+ticketLink+"'>"+t.getId()+"</a> - "+t.getDeviceName()+"</p>";
}
messege=messege.replace("{TICKETS GO HERE}",ticketText);
Properties properties = System.getProperties();
properties = setProp(sender, client.getEmail());
Session session = Session.getInstance(properties, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(sender, password);
}
});
try {
Address address = new InternetAddress(client.getEmail());
MimeMessage message = new MimeMessage(session);
message.setFrom(address);
message.addRecipient(Message.RecipientType.TO, address);
message.setSubject(subject);
message.setText(messege,"utf-8","html");
message.saveChanges();
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (Exception mex) {
mex.printStackTrace();
}
}
public void sendTemporaryPassword(String newpassword) {
String subject = "Your temporary {Application name} password";
String messege = "<!DOCTYPE html>"
+ "<html>"
+ "<body>"
+ "<p>To login in, please use the password <b>"+newpassword+"</b></p><br>"
+ "<p>You will be prompted to set a new password. If you didn't initiate this, sorry. Someone's probably messing with you.</p>"
+ "</body>"
+ "<footer style='text-align: center;'><font size='1'>To change notification settings, please visit your <a href='"+profileLink+"'>profile settings page</a></font></footer>"
+ "</html>";
Properties properties = System.getProperties();
properties = setProp(sender, client.getEmail());
Session session = Session.getInstance(properties, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(sender, password);
}
});
try {
Address address = new InternetAddress(client.getEmail());
MimeMessage message = new MimeMessage(session);
message.setFrom(address);
message.addRecipient(Message.RecipientType.TO, address);
message.setSubject(subject);
message.setText(messege,"utf-8","html");
message.saveChanges();
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (Exception mex) {
mex.printStackTrace();
}
}
public void sendUrgentRequest(Ticket[] tickets) {
String subject = "{CLIENT} has urgent need of {a/some} device{s}";
// I really want to read this from a seperate file, but even when it's in the same directory eclipse insists on moving everything around :(
String msg = "<!DOCTYPE html>"
+ "<html>"
+ "<body>"
+ "<h4>{CLIENT} is very excited about getting a device, and doesn't want to be kept waiting.</h4>"
+ "<p>You'd better drop everything you're doing to take care of them immediately. Who knows what might happen if they don't get their device{s} right freakin' now?</p>"
+ "<p>Here {is/are} the ticket{s} in question:</p>"
+ "<div name = 'tickets' style='text-indent: 20px'>"
+ "{TICKETS GO HERE}"
+ "</div>"
+ "</body>"
+ "<footer style='text-align: center;'><font size='1'>To change notification settings, please visit your <a href='"+profileLink+"'>profile settings page</a></font></footer>"
+ "</html>";
// The messege needs to be tailored to make sense
subject=subject.replace("{CLIENT}",""+tickets[0].getRequestorName());
subject=subject.replace("{a/some}",tickets.length==1 ? "a" : "some");
subject=subject.replace("{s}",(tickets.length==1 ? "" : "s"));
msg=msg.replace("{CLIENT}",""+tickets[0].getRequestorName()); //
msg=msg.replace("{s}",(tickets.length==1 ? "" : "s")); //make occurances of 'device' and 'ticket' plural or singular
msg=msg.replace("{was/were}",tickets.length==1 ? "was" : "were"); //use appropriate verbs
msg=msg.replace("{is/are}",tickets.length==1 ? "is" : "are");
msg=msg.replace("{!s}",tickets.length==1 ? "s" : "s"); //verbs again
String tickethtml = "";
for(Ticket ticket : tickets)
{
tickethtml+="<p>Ticket #<a href='"+ticketLink+"'>"+ticket.getId()+"</a></p>";
}
msg=msg.replace("{TICKETS GO HERE}",tickethtml);
Properties properties = System.getProperties();
properties = setProp(sender, client.getEmail());
Session session = Session.getInstance(properties, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(sender, password);
}
});
try {
Address address = new InternetAddress(client.getEmail());
MimeMessage message = new MimeMessage(session);
message.setFrom(address);
message.addRecipient(Message.RecipientType.TO, address);
message.setSubject(subject);
message.setText(msg,"utf-8","html");
message.saveChanges();
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (Exception mex) {
mex.printStackTrace();
}
}
public void sendAdminSummary(int threadWaitTime) throws ClassNotFoundException, InterruptedException, SQLException {
String subject = "Summary of recent activity";
String message = "<!DOCTYPE html>"
+ "<html>"
+ "<body>"
+ "<h4>Here's what's hapened since you've been away</h4>"
+ "<p>{TICKET COUNT} device{req-s} {req-have/has} been requested.</p>"
+ "<div name = 'tickets' style='text-indent: 20px'>"
+ "{TICKETS GO HERE}"
+ "</div>"
+ "<p>{RETURN COUNT} device{ret-s} {ret-have/has} been marked returned and should be on {ret-their/its} way back.</p>"
+ "<div name = 'returns' style='text-indent: 20px'"
+ "{RETURNS GO HERE}"
+ "</div>"
+ "</body>"
+ "<footer style='text-align: center;'><font size='1'>To change notification settings, please visit your <a href='"+profileLink+"'>profile settings page</a></font></footer>"
+ "</html>";
Object[] ticketActivity = TicketQueries.getRecentClientActivity();
Ticket[] requests = (Ticket[]) ticketActivity[0];
String array = "Requests array:\n";
for (Ticket t : requests) {
array+=" "+t.getDeviceName()+"\n";
}
// System.out.println(array);
Ticket[] returns = (Ticket[]) ticketActivity[1];
array = "Returns array:\n";
for (Ticket t :returns) {
array+=" "+t.getDeviceName()+"\n";
}
// System.out.println(array);
String requestString = "";
for (Ticket tic : requests) {
requestString+="<p>Ticket #"+tic.getId()+" - "+tic.getDeviceName()+" - requested by "+tic.getRequestorName()+"</p>";
}
String returnString = "";
for (Ticket tic : returns) {
returnString+="<p>Ticket #"+tic.getId()+" - "+tic.getDeviceName()+" - returned by "+tic.getRequestorName()+"</p>";
}
message=message.replace("{req-s}",(requests.length==1 ? "" : "s")); //make occurances of 'device' and 'ticket' plural or singular
message=message.replace("{ret-s}",(returns.length==1 ? "" : "s"));
message=message.replace("{req-have/has}",(requests.length==1 ? "has" : "have"));
message=message.replace("{ret-have/has}",(returns.length==1 ? "has" : "have"));
message=message.replace("{ret-their/its}",(returns.length==1 ? "its" : "their"));
message=message.replace("{TICKET COUNT}",""+requests.length);
message=message.replace("{RETURN COUNT}",""+returns.length);
message=message.replace("{TICKETS GO HERE}",requestString);
message=message.replace("{RETURNS GO HERE}",returnString);
Properties properties = System.getProperties();
properties = setProp(sender, client.getEmail());
Session session = Session.getInstance(properties, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(sender, password);
}
});
try {
Address address = new InternetAddress(client.getEmail());
MimeMessage mess = new MimeMessage(session);
mess.setFrom(address);
mess.addRecipient(Message.RecipientType.TO, address);
mess.setSubject(subject);
mess.setText(message,"utf-8","html");
mess.saveChanges();
Transport.send(mess);
System.out.println("Sent message successfully....");
} catch (Exception mex) {
mex.printStackTrace();
}
}
private Properties setProp(String email, String targetEmail) {
Properties props = null;
try {
props = System.getProperties();
props.setProperty("mail.transport.protocol", "smtp");
if (email.contains(",") || targetEmail.contains(",")) {
System.out.println("Please send one email to one person at a time...");
} else if (email.contains("@gmail.com")) {
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
} else {
System.out.println("Your Email Address is invalid\n or host not supported!");
}
} catch (HeadlessException exp) {
System.out.println(exp);
}
return props;
}
}