如何从批处理程序或java发送电子邮件

时间:2013-03-19 02:40:20

标签: java batch-file

我正在尝试创建一个antiviruse备份系统,我希望程序在用户创建帐户之后向用户发送电子邮件,其中包含警告和备份系统的副本。有没有关于用java发送电子邮件的好教程?

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

这是示例: 使用java-mail-1.4.4.jar

package net.spring.mail;


import java.io.IOException; 
import java.util.Date; 
import java.util.Properties; 

import javax.mail.Authenticator; 
import javax.mail.Message; `enter code here`
import javax.mail.MessagingException; 
import javax.mail.Multipart; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart; 
/**
 * javaMail发送邮件
 * 支持多邮件发送
 * @author fazhen.zheng
 *
 */
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);

    }

    /**
     * 测试发带附件邮件
     */
    public static void main(String[] args) {
        // 发件人信息
        String host = "smtp.163.com";
        String port = "25";
        String mailFrom = "example@163.com";
        String password = "passwrod";

        // 收件人信息
        String mailTo = "example@ehualu.com";
        String subject = "javaMail";
        String message = "这是一封由javaMail自动发出的测试邮件,请勿回复。";

        // 附件
        String[] attachFiles = new String[1];
        attachFiles[0] = "c:/EHL_SysManager.jar";
        try {
            sendEmailWithAttachments(host, port, mailFrom, password, mailTo,
                    subject, message, attachFiles);
            System.out.println("邮件发送成功.");
        } catch (Exception ex) {
            System.out.println("发送失败");
            ex.printStackTrace();
        }
    }
}