把JavaMail包装成简单的Sendmail

本文提供了一个Java邮件发送的示例代码,介绍了如何通过Java程序实现SMTP邮件发送功能,包括设置邮件服务器、构建邮件内容等关键步骤。
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

package steeven;



/*

Calss:  Sendmail

Author: steeven@kali.com.cn

QQ:     7461522

Date:   11/13/2001

Notes:

        Find samples in main();

        If you send mail only from/to intranet, change domain as u need.

Setup:

        1. change host, mailFooter

*/





import java.util.*;

import java.sql.*;

import javax.mail.*;

import javax.mail.internet.*;

import javax.activation.*;



public class Sendmail extends Common{



  public String host = "smtpServer";              //smtp server

  static String mailFooter = "/n/n/n此邮件由WebMail发送 http://www/mail.jsp/n/n";

  String domain = "company";

  boolean sessionDebug;



  Message msg;

  Multipart multipart;



  public Sendmail() throws Exception{

    Properties props = System.getProperties();

    props.put("mail.host", host);

    props.put("mail.transport.protocol", "smtp");

    Session session = Session.getDefaultInstance(props, null);

    // Set debug on the Session so we can see what is going on

    // Passing false will not echo debug info, and passing true

    // will.

    session.setDebug(sessionDebug);

    // Instantiate a new MimeMessage and fill it with the

    // required information.

    msg = new MimeMessage(session);

    msg.setSentDate(new java.util.Date());

    multipart = new MimeMultipart();

    msg.setContent(multipart);

  }

  public Sendmail(String to, String from, String subject, String body) throws Exception{

    this();

    this.setSendTo(to);

    this.setFrom(from);

    this.setSubject(subject);

    this.setBody(body);

  }

  public Sendmail(String[] to, String from, String subject, String body) throws Exception{

    this();

    this.setSendTo(to);

    this.setFrom(from);

    this.setSubject(subject);

    this.setBody(body);

  }

  public void setHost(String host) throws Exception{

    this.host=host;

  }

  void setFrom(String from) throws Exception{

    msg.setFrom(new InternetAddress(from "@" domain));

  }

  void setSendTo(String[] to) throws Exception{

    for (int i=0;i
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值