java mail

package com.javaMail;

import java.net.InetAddress;
import java.util.Date;
import java.util.Properties;
import java.util.Vector;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class JavaMail {
 private String to = ""; // 收件人
 private String from = ""; // 发件人
 private String host = ""; // SMTP主机
 private String userMailName = ""; // 发件人邮箱用户名
 private String userMailPassword = ""; // 发件人邮箱密码
 private String fileName = ""; // 附件文件名
 private String subject = ""; // 邮件主题
 private String content = ""; // 邮件正文
 private Vector<String> file = new Vector<String>(); // 附件文件集合

 public String getTo() {
  return to;
 }

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

 public String getFrom() {
  return from;
 }

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

 public String getHost() {
  return host;
 }

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

 public String getUserMailName() {
  return userMailName;
 }

 public void setUserMailName(String userMailName) {
  this.userMailName = userMailName;
 }

 public String getUserMailPassword() {
  return userMailPassword;
 }

 public void setUserMailPassword(String userMailPassword) {
  this.userMailPassword = userMailPassword;
 }

 public String getFileName() {
  return fileName;
 }

 public void setFileName(String fileName) {
  this.fileName = fileName;
 }

 public String getSubject() {
  return subject;
 }

 public void setSubject(String subject) {
  this.subject = subject;
 }

 public String getContent() {
  return content;
 }

 public void setContent(String content) {
  this.content = content;
 }

 public Vector<String> getFile() {
  return file;
 }

 public void setFile(Vector<String> file) {
  this.file = file;
 }

 /**
  * 往附件组合中添加附件
  */
 public void addfile(String fname) {
  file.addElement(fname);
 }

 /**
  * 默认构造器
  */
 public JavaMail() {
 }

 /**
  * 构造器,提供直接的参数传入
  */
 public JavaMail(String to, String from, String smtpServer, String userName,
   String password, String subject, String content) {
  this.to = to;
  this.from = from;
  this.host = smtpServer;
  this.userMailName = userName;
  this.userMailPassword = password;
  this.subject = subject;
  this.content = content;
 }

 /**
  * 发送邮件
  *
  * @return 发送成功时返回 true
  * @throws Exception
  * @throws Exception
  */
 public void sendMail() throws Exception {
  Properties properties = new Properties();
  try {
   if (host != null)
    properties.put("mail.smtp.host", host);
   else if (properties.getProperty("mail.smtp.host") == null)
    properties.put("mail.smtp.host", InetAddress.getLocalHost()
      .getHostName());
   properties.put("mail.smtp.port", 25);
   properties.put("mail.smtp.auth", true);
  } catch (Exception ex) {
   throw new Exception(ex.getMessage());
  }
  MyAuthenticator myauth = new MyAuthenticator(this.userMailName,
    this.userMailPassword);
  // 创建JavaMail Session
  Session session = Session.getDefaultInstance(properties, myauth);
  // 根据session创建一个邮件消息
  MimeMessage msg = new MimeMessage(session);
  msg.isExpunged();
  Address address = null;
  try {
   if (this.from != null) {
    address = new InternetAddress(this.from);
    // 创建邮件发送者地址
    Address[] fromAddresses = { address };
    // 设置邮件消息的发送者
    msg.addFrom(fromAddresses);
   } else
    throw new Exception("未指定发件人地址");
   if (this.to != null) {
    // 创建邮件的接收者地址,并设置到邮件消息中
    Address toAdresses = new InternetAddress(this.to);
    // 设置邮件消息的接受者
    msg.addRecipient(Message.RecipientType.TO, toAdresses);
   } else
    throw new Exception("未指定收件人地址");
   if (content != null) {
    msg.setContent(content, "text/html ;charset=utf-8");
   } else
    msg.setText("");

   // 主题
   msg.setSubject(subject);
   // 设置邮件消息发送的时间
   msg.setSentDate(new Date());
   // 设置邮件消息的主要内容
   javax.mail.Transport.send(msg);
  } catch (Exception e) {
   throw new Exception(e);
  }

 }

 /**
  * 校验发信人权限类
  */
 class MyAuthenticator extends javax.mail.Authenticator {
  private String strUser = "";
  private String strPwd = "";

  public MyAuthenticator() {
  }

  public MyAuthenticator(String user, String password) {
   this.strUser = user;
   this.strPwd = password;
  }

  protected PasswordAuthentication getPasswordAuthentication() {
   return new PasswordAuthentication(strUser, strPwd);
  }
 }

 /**
  * 主方法,用于测试
  */
 public static void main(String[] args) {

 

 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值