Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#2] Code Optimization #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[#2] Code Optimization
  • Loading branch information
sarathnexer committed Jan 16, 2024
commit e43450d93de2bed736ba9c00924b7e1b97080d54
50 changes: 21 additions & 29 deletions src/controller/Mail.java → src/controller/MailController.java
Original file line number Diff line number Diff line change
@@ -1,57 +1,50 @@
package controller;
import java.io.File;
import java.util.Properties;

import dto.MailDto;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
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.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.File;
import java.util.Properties;

public class MailController {

import javafx.scene.image.Image;

public class Mail {
private String username ;
private String password ;
private String from ;
private String to ;
public Mail(String from,String password, String to){
this.from=from;
this.to = to;
this.username = from;
this.password = password;
sendMail();
MailDto mailDto;
public MailController(String from, String password, String to){
mailDto = new MailDto();
mailDto.setFrom(from);
mailDto.setTo(to);
mailDto.setUsername(from);
mailDto.setPassword(password);
sendMail(mailDto);
}

public void sendMail(){
public void sendMail(MailDto mailDto){
Properties props = new Properties();

props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,password);
return new PasswordAuthentication(mailDto.getUsername(),mailDto.getPassword());
}
});

try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setFrom(new InternetAddress(mailDto.getFrom()));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
InternetAddress.parse(mailDto.getTo()));
message.setSubject("Website Status Notification.");
MimeMultipart multipart = new MimeMultipart("related");

Expand Down Expand Up @@ -81,8 +74,7 @@ protected PasswordAuthentication getPasswordAuthentication() {
System.out.println("Done");

} catch (MessagingException e) {
System.out.println("Error sending email.");
e.printStackTrace();
System.out.println("Error sending email: "+e.getMessage());
}
}
// public static void main(String[] args){
Expand Down
Loading