📄 mailnotifier.java.svn-base
字号:
package com.sixtwenty;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
import java.sql.*;
/**
* The mail notifier system to alert the users by an email when
* -> a new project for a subject they attend is created
* -> their project are open to assignement
* -> their project due date has changed
*/
public class MailNotifier {
/**
* The server's host
*/
protected String _serverHost;
/**
* The user's login to connect to the smtp server
*/
protected String _userLogin;
/**
* The password to connect to the smtp server
*/
protected String _password;
/**
* The author of the email
*/
protected String _from;
/**
* The constructor of the MailNotifier Class
* @param serverHost The server's host
* @param userLogin The user's login to connect to the smtp server
* @param password The password to connect to the smtp server
* @param from The recipient of the email
*/
public MailNotifier(String serverHost, String userLogin, String password, String from) {
_serverHost = serverHost;
_userLogin = userLogin;
_password = password;
_from = from;
}
/**
* The Notification to send to all the users which will participate in this project
* @param name The project's name
* @param projId The project's identifier
* @param openDate The project's open date (can be null)
* @param closeDate The project's closing date (can be null)
* @param description The project's description
* @param url The project's url
* @param emails The list of user's emails to send the notification
*/
public void newProjectNot(String name, String projId, Date openDate, Date closeDate, String description, String url, String[] emails) {
// The subject of the email to send
String emailSubject = "New project notification: " + name + "(code: " + projId + ")";
// The content of the email to send
String emailContent = "Mail automatically sent to you because a new project has been created: \n";
emailContent = emailContent + "Project name: " + name + "\n";
emailContent = emailContent + "Project code: " + projId + "\n";
emailContent = emailContent + "description: " + description + "\n";
emailContent = emailContent + "url: " + url + "\n";
// Should we insert the open date ?
if (openDate != null) {
emailContent = emailContent + "open date: " + openDate + "\n";
}
// Should we insert the closing date ?
if (closeDate != null) {
emailContent = emailContent + "due date: " + closeDate + "\n";
}
emailContent = emailContent + "\nPlease do not reply to this email";
// send all the emails
try {
send(emailSubject, emailContent, emails);
} catch(Exception e) {
System.err.println(e);
}
}
/**
* The Notification to send to all the users which are concerned by the assignement opening of a project
* @param name The project's name
* @param projId The project's identifier
* @param openDate The project's open date (can be null)
* @param closeDate The project's closing date (can be null)
* @param description The project's description
* @param url The project's url
* @param emails The list of user's emails to send the notification
*/
public void projOpenAssignNot(String name, String projId, Date openDate, Date closeDate, String description, String url, String[] emails) {
// The subject of the email to send
String emailSubject = "Assignement opened notification for the project: " + name + "(code: " + projId + ")";
// The content of the email to send
String emailContent = "Mail automatically sent to you because you can now submit your assignement \n";
emailContent = "for this project :" + name + "(code: " + projId + ")\n\n Sum up of the project: \n";
emailContent = emailContent + "Project name: " + name + "\n";
emailContent = emailContent + "Project code: " + projId + "\n";
emailContent = emailContent + "description: " + description + "\n";
emailContent = emailContent + "url: " + url + "\n";
// Should we insert the open date ?
if (openDate != null) {
emailContent = emailContent + "open date: " + openDate + "\n";
}
// Should we insert the closing date ?
if (closeDate != null) {
emailContent = emailContent + "due date: " + closeDate + "\n";
}
emailContent = emailContent + "\nPlease do not reply to this email";
// send all the emails
try {
send(emailSubject, emailContent, emails);
} catch(Exception e) {
System.err.println(e);
}
}
/**
* The Notification to send to all the users which are concerned by the changement of the due date of a project
* @param name The project's name
* @param projId The project's identifier
* @param openDate The project's open date (can be null)
* @param closeDate The project's closing date (can be null)
* @param description The project's description
* @param url The project's url
* @param emails The list of user's emails to send the notification
*/
public void projChangeDueDateAssignNot(String name, String projId, Date openDate, Date closeDate, String description, String url, String[] emails) {
// The subject of the email to send
String emailSubject = "Assignement due date has changed for the project: " + name + "(code: " + projId + ")";
// The content of the email to send
String emailContent = "Mail automatically sent to you because the due date for this assignement has changed: \n\n";
emailContent = emailContent + "Project name: " + name + "\n";
emailContent = emailContent + "Project code: " + projId + "\n";
emailContent = emailContent + "description: " + description + "\n";
emailContent = emailContent + "url: " + url + "\n";
// Should we insert the open date ?
if (openDate != null) {
emailContent = emailContent + "open date: " + openDate + "\n";
}
// Should we insert the closing date ?
if (closeDate != null) {
emailContent = emailContent + "due date: " + closeDate + "\n";
}
emailContent = emailContent + "\nPlease do not reply to this email";
// send all the emails
try {
send(emailSubject, emailContent, emails);
} catch(Exception e) {
System.err.println(e);
}
}
/**
* The method to send an email to a list of email addresses
* @param subject The email's subject
* @param content The email's content
* @param emails The email addresses to send this email
*/
protected void send(String subject, String content, String[] emails) throws Exception {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", _serverHost);
props.setProperty("mail.user", _userLogin);
props.setProperty("mail.password", _password);
Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject(subject);
message.setContent(content, "text/plain");
// set the from and to address
InternetAddress addressFrom = new InternetAddress(_from);
message.setFrom(addressFrom);
// We send the email to all the email addresses in "emails"
for (int i=0; i < emails.length; i++) {
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(emails[i]));
}
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -