MySql 通过电子邮件发送查询

MySql Query Sent by Email

我有一个简单的查询,select 来自几个不同表的一些字段,我需要每月 运行 一次。我知道我可以安排每个月 "job" 的 CREATE EVENT,但是,是否可以在查询 运行 之后将该信息通过电子邮件发送到某些地址?这样我就不需要登录服务器查看新文件了?

is it possible to have that information emailed to some addresses after the query runs?

如果您正在寻找 MySQL 内置解决方案,那么可能 NO。这个特殊应该在应用程序结束时处理。

因此,如果您将查询安排为 cron job in linux(或)batch job in windows,那么您可以配置 cron(或) batch 在查询完成后向收件人列表发送电子邮件。

如何配置cron发送邮件可以查看HERE

Mysql 不支持该功能。

您可以使用 cron 作业 (Quartz) 每月安排一个作业,

您可以在哪里获取数据并发送包含您的数据的电子邮件。

参考下面的link石英作业:

http://www.mkyong.com/java/example-to-run-multiple-jobs-in-quartz/

我认为 Mysql 不支持发送电子邮件。

在这种情况下,您可以开发一个辅助程序来发送创建的文件,并使用-计划任务、Cron ...(这取决于您使用的服务器的操作系统)来执行它。

辅助程序可以像 this code 添加要附加的 file/s(attachFiles 变量)。

public class EmailAttachmentSender {

public static void sendEmailWithAttachments(String host, String port,
        final String userName, final String password, String toAddress,
        String subject, String message, String[] attachFiles)
        throws AddressException, MessagingException {
    // sets SMTP server properties
    Properties properties = new Properties();
    properties.put("mail.smtp.host", host);
    properties.put("mail.smtp.port", port);
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.user", userName);
    properties.put("mail.password", password);

    // creates a new session with an authenticator
    Authenticator auth = new Authenticator() {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password);
        }
    };
    Session session = Session.getInstance(properties, auth);

    // creates a new e-mail message
    Message msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress(userName));
    InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
    msg.setRecipients(Message.RecipientType.TO, toAddresses);
    msg.setSubject(subject);
    msg.setSentDate(new Date());

    // creates message part
    MimeBodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setContent(message, "text/html");

    // creates multi-part
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);

    // adds attachments
    if (attachFiles != null && attachFiles.length > 0) {
        for (String filePath : attachFiles) {
            MimeBodyPart attachPart = new MimeBodyPart();

            try {
                attachPart.attachFile(filePath);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            multipart.addBodyPart(attachPart);
        }
    }

    // sets the multi-part as e-mail's content
    msg.setContent(multipart);

    // sends the e-mail
    Transport.send(msg);

}

/**
 * Test sending e-mail with attachments
 */
public static void main(String[] args) {
    // SMTP info
    String host = "smtp.gmail.com";
    String port = "587";
    String mailFrom = "your-email-address";
    String password = "your-email-password";

    // message info
    String mailTo = "your-friend-email";
    String subject = "New email with attachments";
    String message = "I have some attachments for you.";

    // attachments
    String[] attachFiles = new String[3];
    attachFiles[0] = "e:/Test/Picture.png";
    attachFiles[1] = "e:/Test/Music.mp3";
    attachFiles[2] = "e:/Test/Video.mp4";

    try {
        sendEmailWithAttachments(host, port, mailFrom, password, mailTo,
            subject, message, attachFiles);
        System.out.println("Email sent.");
    } catch (Exception ex) {
        System.out.println("Could not send email.");
        ex.printStackTrace();
    }
}

我自己没有这样做,但我看不出它为什么不起作用:创建一个 UDF(用户定义的函数),它接受电子邮件参数并发送电子邮件。您可以编写 UDF,例如在 C++ 中,手头有所有必要的库。