Skip to content

Commit

Permalink
Update BuildAndSendMail.java
Browse files Browse the repository at this point in the history
  • Loading branch information
JitseGoutbeek authored Jun 28, 2024
1 parent 2306fbe commit 1d1de70
Showing 1 changed file with 46 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
package eu.sig.training.ch05.buildandsendmail;

public class BuildAndSendMail {
// tag::buildAndSendMail[]
public void buildAndSendMail(MailMan m, String firstName, String lastName,
String division, String subject, MailFont font, String message1,
String message2) {
// Format the email address
String mId = firstName.charAt(0) + "." + lastName.substring(0, 7) + "@"
+ division.substring(0, 5) + ".compa.ny";
// Format the message given the content type and raw message
MailMessage mMessage = formatMessage(font,
message1 + message2);
// Send message
m.send(mId, subject, mMessage);
}
// end::buildAndSendMail[]
public class EmailService {
private final MailMan mailMan;

public EmailService(MailMan mailMan) {
this.mailMan = mailMan;
}

public void sendEmail(EmailDetails details, EmailContent content) {
String emailAddress = generateEmailAddress(details);
MailMessage formattedMessage = formatMessage(content);
mailMan.send(emailAddress, details.getSubject(), formattedMessage);
}

private String generateEmailAddress(EmailDetails details) {
return String.format("%c.%s@%.5s.compa.ny",
details.getFirstName().charAt(0),
details.getLastName().substring(0, Math.min(7, details.getLastName().length())),
details.getDivision());
}

private MailMessage formatMessage(EmailContent content) {
return new MailMessage(content.getFont(), content.getCombinedMessage());
}
}

class EmailDetails {
private final String firstName;
private final String lastName;
private final String division;
private final String subject;

// Constructor, getters...
}

class EmailContent {
private final MailFont font;
private final String message1;
private final String message2;

// Constructor, getters...

public String getCombinedMessage() {
return message1 + message2;
}
}

@SuppressWarnings("unused")
private MailMessage formatMessage(MailFont font, String string) {
Expand All @@ -36,4 +66,4 @@ private class MailMessage {

}

}
}

0 comments on commit 1d1de70

Please sign in to comment.