Skip to content

Instantly share code, notes, and snippets.

@jsuwo
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsuwo/9224341 to your computer and use it in GitHub Desktop.
Save jsuwo/9224341 to your computer and use it in GitHub Desktop.
Starter code for Lab 9.
package ca.uwo.csd.cs2212.USERNAME;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class App {
private final static String SENDER_NAME = "John Doe";
private final static String SENDER_EMAIL = "jdoe@example.com";
private final static String SMTP_USERNAME = "username";
private final static String SMTP_PASSWORD = "password";
private final static String SMTP_SERVER = "localhost";
private final static String SMTP_PORT = "1025";
private static Session getSession() {
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_SERVER);
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(SMTP_USERNAME, SMTP_PASSWORD);
}
}
);
return session;
}
private static void sendMessage(Session session, String[] recipients) throws Exception {
Message msg = new MimeMessage(session);
Address sender = new InternetAddress(SENDER_EMAIL, SENDER_NAME);
msg.setFrom(sender);
for (String address : recipients)
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(address));
msg.setSubject("Hello");
msg.setText("Hello World");
Transport.send(msg);
}
public static void main(String[] args) throws Exception {
if (args.length == 0) {
System.out.println("Please specify a space-separated list of email addresses as arguments.");
System.exit(-1);
}
Session session = getSession();
sendMessage(session, args);
}
}
<div style='font-family: sans-serif'>
<p>
Hello Customer,
</p>
<p>
Thank you for your order. Your items will be shipped within <strong>24 hours</strong>.
</p>
<p>
Sincerely,<br/>
The $companyName Team
</p>
</div>
<div style='font-family: sans-serif'>
<p>
Hello Customer,
</p>
<p>
Thank you for your order. You have ordered the following products:
</p>
<table>
<thead>
<tr>
<th style="text-align: right">Item #</th>
<th style="text-align: left">Product</th>
<th style="text-align: right">Price</th>
</tr>
</thead>
<tbody>
#foreach($product in $productsOrdered)
<tr>
<td style="text-align: right">$velocityCount</td>
<td style="text-align: left">$product.getName()</td>
<td style="text-align: right">$product.getPrice()</td>
</tr>
#end
</tbody>
</table>
<p>
Your items will be shipped within <strong>24 hours</strong>.
</p>
<p>
Sincerely,<br/>
The $companyName Team
</p>
</div>
Hello Customer,
Thank you for your order. Your items will be shipped within 24 hours.
Sincerely,
The $companyName Team
Hello Customer,
Thank you for your order. You have ordered the following products:
#foreach($product in $productsOrdered)
* Item $velocityCount - $product.getName() ($product.getPrice())
#end
Your items will be shipped within 24 hours.
Sincerely,
The $companyName Team
private static String getTextBody() {
StringBuilder sb = new StringBuilder();
sb.append("Hello Customer,\n\n");
sb.append("Thank you for your order. Your items will be shipped within 24 hours.\n\n");
sb.append("Sincerely,\n");
sb.append("The ACME Team");
return sb.toString();
}
private static String getHtmlBody() {
StringBuilder sb = new StringBuilder();
sb.append("<div style='font-family: sans-serif'>");
sb.append("<p>Hello Customer,</p>");
sb.append("<p>Thank you for your order. Your items will be shipped within <strong>24 hours</strong>.</p>");
sb.append("<p>Sincerely,<br/>");
sb.append("The ACME Team</p>");
sb.append("</div>");
return sb.toString();
}
package ca.uwo.csd.cs2212.USERNAME;
public class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return this.name;
}
public double getPrice() {
return this.price;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment