Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/ky0men/Java_SEM2
Browse files Browse the repository at this point in the history
  • Loading branch information
haophuc-tonthat committed Mar 5, 2022
2 parents 574dc90 + 752c6b7 commit 922ee33
Show file tree
Hide file tree
Showing 40 changed files with 67 additions and 679 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added out/artifacts/HotelManagement_SEM2_jar/mail.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
673 changes: 0 additions & 673 deletions out/production/HotelManagement-SEM2/resources/html/EmailTemplate.txt

This file was deleted.

65 changes: 63 additions & 2 deletions src/controller/ResetPasswordController.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,22 @@
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Scanner;

public class ResetPasswordController implements Initializable {

Expand Down Expand Up @@ -253,17 +261,70 @@ public void sendEmailResetPass(int randomCode){
InternetAddress [] address = {new InternetAddress(to)};
msg.setRecipients (Message. RecipientType.TO, address);
msg.setSubject (subject);
msg.setText (message);
// msg.setText (message);
// Transport transport = mailSession.getTransport ("smtp");
// transport.connect (host, user, pass);
// transport.sendMessage (msg, msg.getAllRecipients());
// transport.close ();
// System.out.println("Send Code");
//ReplaceString HTML File
String h1 = "Reset Password Successfully";
String title = "Your new password";
String content = "Password: "+ randomCode;
replaceString("thisIsYourH1", h1);
replaceString("thisIsYourTitle", title);
replaceString("thisIsYourContent" , content);

//Set Content Email
msg.setContent(readFileHTMLToString(), "text/html; charset=UTF-8");
Transport transport = mailSession.getTransport ("smtp");
transport.connect (host, user, pass);
transport.sendMessage (msg, msg.getAllRecipients());
transport.close ();
System.out.println("Send Code");

//Default HTML File
replaceString(h1,"thisIsYourH1");
replaceString(title,"thisIsYourTitle");
replaceString(content,"thisIsYourContent");
} catch (MessagingException e) {
e.printStackTrace();
}

}
//HTML Template config
private void replaceString(String oldValue, String newValue){
Path path = Paths.get("email-template/email-template.txt");
Charset charset = StandardCharsets.UTF_8;

String content = null;
try {
content = Files.readString(path, charset);
} catch (IOException e) {
e.printStackTrace();
}
content = content.replaceAll(oldValue, newValue);
try {
Files.writeString(path, content, charset);
} catch (IOException e) {
e.printStackTrace();
}
}

private String readFileHTMLToString(){
Scanner scanner = null;
try {
scanner = new Scanner(new File("email-template/email-template.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
StringBuilder sb = new StringBuilder();
while(scanner.hasNextLine()) {
sb.append(scanner.nextLine());
}

String body = sb.toString();
return body;
}

public void changePasswordInDB(Connection conn, int randomCode, String username){
Statement stm = null;
Expand Down

0 comments on commit 922ee33

Please sign in to comment.