Moved email stuff to zutil and moved transoced to its own package

This commit is contained in:
Ziver Koc 2016-12-22 16:24:46 +01:00
parent 05048442a1
commit 7cac4e155c
20 changed files with 518 additions and 537 deletions

View file

@ -1,97 +0,0 @@
package zall.util;
import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import sun.net.smtp.SmtpClient;
/**
* Simplifies sending of a email
*
* @author Ziver
*/
public class Email {
public static enum ContentType{
PLAIN, HTML
}
private static final SimpleDateFormat dateFormatter =
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
private static String host = "localhost";
private String from;
private String niceFrom = null;
private String to;
private String replyTo = null;
private ContentType type = ContentType.PLAIN;
private String subject;
private String message;
public Email(String from, String to){
this.from = from;
this.to = to;
}
public void setFrom(String f){
from = f;
}
public void setNiceFrom(String nf){
niceFrom = nf;
}
public void setReplyTo(String rpt){
replyTo = rpt;
}
public void setTo(String t){
to = t;
}
public void setContentType(ContentType t){
type = t;
}
public void setSubject(String s){
subject = s;
}
public void setMessage(String msg){
message = msg;
}
public static void setServer(String host){
Email.host = host;
}
public void send() throws IOException{
if(from == null)
throw new IllegalArgumentException("From value missing!");
if(to == null)
throw new IllegalArgumentException("To value missing!");
SmtpClient smtp = new SmtpClient( host );
smtp.from(from);
smtp.to(to);
PrintStream msg = smtp.startMessage();
//************ Headers
msg.println("From: "+(niceFrom!=null ? "\""+niceFrom+"\"" : "")+" <"+from+">");
msg.println("Reply-To: <"+replyTo+">");
if( replyTo != null )
msg.println("Reply-To: <"+replyTo+">");
msg.println("To: " + to); // so mailers will display the To: address
msg.println("Subject: "+subject);
// Date
msg.println("Date: "+dateFormatter.format(new Date(System.currentTimeMillis())));
// Content type
switch( type ){
case HTML:
msg.println("Content-Type: text/html;"); break;
default:
msg.println("Content-Type: text/plain;"); break;
}
msg.println();
//*********** Mesasge
msg.println( message );
smtp.closeServer();
}
}

75
src/zall/util/ZalleryEmail.java Executable file
View file

@ -0,0 +1,75 @@
package zall.util;
import zall.Zallery;
import zall.bean.User;
import zutil.db.DBConnection;
import zutil.net.smtp.Email;
import zutil.net.smtp.SMTPClient;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
/**
*
*/
public class ZalleryEmail {
private static String host;
public static void setSMTPHost(String host){
ZalleryEmail.host = host;
}
public static void sendVerificationEmail(User user) throws IOException {
// Email
Email email = new Email();
email.setFrom("admin@koc.se", "Koc.se Admin");
email.setTo(user.getEmail());
email.setSubject("Registration at "+ Zallery.getWebsiteName());
email.setContentType(Email.ContentType.HTML);
email.setMessage("You receive this message because you have requested an account" +
"<br>at "+Zallery.getWebsiteName()+". Please click the link to verify your email address: " +
"<p><a href='"+Zallery.getWebsiteURL()+"?action=verfemail&id="+user.getId()+"&hash="+user.getEmailVerificationHash()+"'>"+Zallery.getWebsiteURL()+"?action=verfemail&id="+user.getId()+"&hash="+user.getEmailVerificationHash()+"</a>" +
"<p> You will have to wait for an admin to activate your account after you have verified your email.");
SMTPClient smtp = new SMTPClient(host);
smtp.send(email);
smtp.close();
}
public static void sendActivationEmail(User user) throws IOException {
Email email = new Email();
email.setFrom("admin@koc.se", "Koc.se Admin");
email.setTo(user.getEmail());
email.setSubject("Account activation at " + Zallery.getWebsiteName());
email.setContentType(Email.ContentType.HTML);
email.setMessage("Your account has now been activated by an admin. You can now login and use the site. " +
"<a href='" + Zallery.getWebsiteURL() + "'>" + Zallery.getWebsiteURL() + "</a>");
SMTPClient smtp = new SMTPClient(host);
smtp.send(email);
smtp.close();
}
public static void sendNewUserRegistrationToAdmin(User user, DBConnection db) throws SQLException, IOException{
// Email the admin about new user
Email email = new Email();
email.setFrom("admin@koc.se", "Koc.se Admin");
email.setSubject("New user activation request at "+Zallery.getWebsiteName());
email.setContentType(Email.ContentType.HTML);
email.setMessage("A new user has registered for an account at " +
"<a href='"+Zallery.getWebsiteURL()+"'>"+Zallery.getWebsiteName()+"</a>:" +
"<p>Email: <b>" + user.getEmail() + "</b>" +
"<br>Name: <b>" + user.getName() + "</b>"
);
SMTPClient smtp = new SMTPClient(host);
for(User admin : User.loadSuperUsers(db)){
if( admin.isEmailVerified() ){
email.setTo( admin.getEmail() );
smtp.send(email);
}
}
smtp.close();
}
}