java使用javax.mail发邮件

阅读更多

1, 引用jar 包

 
    com.sun.mail
    javax.mail
    1.5.6
   

 2, 代码实现, 不解释

 

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.PrintStream;
import java.util.Date;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message.RecipientType;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class EmailSender
{
  private String from = "[email protected]";
  private String to = "[email protected]";
  private String mail_subject = "TEST";
  private String mail_message = "TEST";
  private String filename;
  private String urlPath = "";
  private String attachmentFlag;
  private String messageType = "text";
  private String host = "xx.x.x.com";

  public static void main(String[] args) throws Exception {
    if (args.length > 0)
      try
      {
        if (args.length == 1) {
          Properties properties = new Properties();
          BufferedInputStream stream = new BufferedInputStream(new FileInputStream(args[0]));
          properties.load(stream);
          stream.close();

          EmailSender es = new EmailSender();
          es.setFrom(properties.getProperty("ALERT_FROM"));
          es.setTo(properties.getProperty("ALERT_TO"));
          es.setHost(properties.getProperty("ALERT_HOST"));
          es.setMessageType(properties.getProperty("ALERT_TYPE"));
          es.setMail_subject(properties.getProperty("ALERT_SUBJECT") + new Date().toString());

          es.setFilename(properties.getProperty("ALERT_CONTENT"));
          es.setAttachmentFlag("N");

          StringTokenizer stk_to = new StringTokenizer(properties.getProperty("ALERT_TO"), ";");
          String mailTo = "";
          while (stk_to.hasMoreTokens())
            try {
              mailTo = stk_to.nextToken();
              es.setTo(mailTo);
              String sResult = es.sendMail();
              System.out.println("Mail: " + mailTo + ":" + sResult);
            } catch (Exception e) {
              System.out.println("Mail: " + mailTo + ":" + e.getMessage());
            }
          return;
        }EmailSender es = new EmailSender();

        es.setFrom(args[0]);
        es.setTo(args[1]);
        es.setHost(args[2]);
        es.setMessageType("file");
        es.setMail_subject(args[3]);
        es.setUrlPath(args[4]);
        es.setFilename(args[5]);
        es.setAttachmentFlag("Y");
        try
        {
          String sResult = es.sendMail();
          System.out.println("Mail: " + es.getTo() + ":" + sResult);
        } catch (Exception e) {
          System.out.println("Mail: " + es.getTo() + ":" + e.getMessage());
        }

      }
      catch (Exception e)
      {
        System.out.println("Error Message: " + e.getMessage());
      }
    else
      System.out.println("Usage: emailSender.jar [conf_file] | emailSender.jar [from][to][host][subject][path][file]");
  }

  public String sendMail() throws Exception
  {
    String sResult = "";
    try
    {
      String success = "N";
      StringTokenizer stk_to = new StringTokenizer(getTo(), ";");
      StringTokenizer stk_from = new StringTokenizer(getFrom(), ";");
      Properties props = System.getProperties();
      props.put("mail.smtp.host", this.host);
      props.put("mail.smtp.auth", "false");
      Session session = Session.getDefaultInstance(props, null);
      MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress(stk_from.nextToken()));
      while (stk_to.hasMoreTokens()) {
        String mailTo = stk_to.nextToken();
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
      }
      message.setSubject(getMail_subject());
      BodyPart messageBodyPart = new MimeBodyPart();

      if ((getAttachmentFlag() != null) && (getAttachmentFlag().equalsIgnoreCase("y"))) {
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);
        messageBodyPart = new MimeBodyPart();
        messageBodyPart.setDataHandler(new DataHandler(new FileDataSource(this.urlPath + this.filename)));
        messageBodyPart.setFileName(getFilename());
        multipart.addBodyPart(messageBodyPart);
        message.setContent(multipart);
      }

      if ((getMessageType() != null) && (getMessageType().equalsIgnoreCase("file"))) {
        BufferedReader in = new BufferedReader(new FileReader(this.urlPath + this.filename));
        String line = null;
        StringBuffer text = new StringBuffer();
        while ((line = in.readLine()) != null) {
          text.append(line + "\n");
        }
        in.close();
        setMail_message(text.toString());
      }
      messageBodyPart.setText(getMail_message());
      Multipart multipart = new MimeMultipart();
      multipart.addBodyPart(messageBodyPart);
      message.setContent(multipart);

      Transport.send(message);
      success = "Y";
      return sResult + success;
    }
    catch (Exception e)
    {
      sResult = sResult + " Exception " + e.getMessage();
    }return sResult;
  }

  public String getFrom()
  {
    return this.from;
  }

  public void setFrom(String from) {
    this.from = from;
  }

  public String getTo() {
    return this.to;
  }

  public void setTo(String to) {
    this.to = to;
  }

  public String getMail_subject() {
    return this.mail_subject;
  }

  public void setMail_subject(String mail_subject) {
    this.mail_subject = mail_subject;
  }

  public String getMail_message() {
    return this.mail_message;
  }

  public void setMail_message(String mail_message) {
    this.mail_message = mail_message;
  }

  public String getFilename() {
    return this.filename;
  }

  public void setFilename(String filename) {
    this.filename = filename;
  }

  public String getUrlPath() {
    return this.urlPath;
  }

  public void setUrlPath(String urlPath) {
    this.urlPath = urlPath;
  }

  public String getAttachmentFlag() {
    return this.attachmentFlag;
  }

  public void setAttachmentFlag(String attachmentFlag) {
    this.attachmentFlag = attachmentFlag;
  }

  public String getMessageType() {
    return this.messageType;
  }

  public void setMessageType(String messageType) {
    this.messageType = messageType;
  }

  public void setHost(String host) {
    this.host = host;
  }
  public String getHost() {
    return this.host;
  }
}

 

3, 测试

java -jar $WKDIR/emailSender.jar [email protected]@126.com mail.com "Hello World" $WKDIR/ 12.csv  111

你可能感兴趣的:(java使用javax.mail发邮件)