用javamail 发送邮件

作者:袖梨 2022-07-02
/*
* File: Mail.java
* Date: 2005-2-28
* Author: fuweilin
* project:
* Ver:
* Note:
* */
package com.toone.eg.common;
/**
* @author fuwln
* @version 1.0
*/
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
/**
* @author fuwl
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class Mail {
private MimeMessage mimeMsg; //MIME邮件对象
private Session session; //邮件会话对象
private Properties props; //系统属性
private boolean needAuth = false; //smtp是否需要认证
private String username = ""; //smtp认证用户名和密码
private String password = "";
private Multipart mp; //Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
/**
*
*/
public Mail() {
setSmtpHost("smtp.163.com");//如果没有指定邮件服务器,就从getConfig类中获取
createMimeMessage();
}
public Mail(String smtp){
setSmtpHost(smtp);
createMimeMessage();
}
/**
* @param hostName String
*/
public void setSmtpHost(String hostName) {
System.out.println("设置系统属性:mail.smtp.host = "+hostName);
if(props == null)props = System.getProperties(); //获得系统属性对象
props.put("mail.smtp.host",hostName); //设置SMTP主机
}
/**
* @return boolean
*/
public boolean createMimeMessage()
{
try{
System.out.println("准备获取邮件会话对象!");
session = Session.getDefaultInstance(props,null); //获得邮件会话对象
}
catch(Exception e){
System.err.println("获取邮件会话对象时发生错误!"+e);
return false;

相关文章

精彩推荐