mailnotifier.java.svn-base

来自「远程学生作业提交系统,所用技术JSP,Servelet,Ajax,Web Ser」· SVN-BASE 代码 · 共 180 行

SVN-BASE
180
字号
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 threading sending
        SendMailThread smt = new SendMailThread(_serverHost, _userLogin, _password, _from);
        // 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 {
            smt.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 threading sending
        SendMailThread smt = new SendMailThread(_serverHost, _userLogin, _password, _from);

        // 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 {
            smt.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";
        // The threading sending
        SendMailThread smt = new SendMailThread(_serverHost, _userLogin, _password, _from);

        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 {
            smt.send(emailSubject, emailContent, emails);
        } catch(Exception e) {
            System.err.println(e);
        }
    }
   }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?