⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mail.java

📁 java阿里巴巴代码
💻 JAVA
字号:
/*
 * Created on 2005-7-28
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package com.ahbay.mailMgr;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
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;


/**
 * @author: 王仁超
 * @Date: 2005-7-28
 * @FileName: mail.java
 * @PackageName: com.ahbay.mailMgr
 * @Method Name: mail
 */
public class mail {
	
	/*
	 * public property
	 */
	public String strTo = "";
	public String strFrom = "";
	public String strSubject = "";
	public String strText = "";
	public String strUsername = "";  
	public String strPassword = "";
	public String strSmtp = "";
	public String strFilename  = "";
	public boolean needAuth = false; 
	
	/*
	 * private property
	 */	
	private MimeMessage mimeMsg; 
	private Session session; 
	private Properties props; 	 
	private Multipart mptMailContent; 	
	
	/*
	 * public method
	 */
	public void setStrTo(String strTo) {
		this.strTo = strTo;
	}
	public void setStrFrom(String strFrom) {
		this.strFrom = strFrom;
	}
	public void setStrSubject(String strSubject) {
		this.strSubject = strSubject;
	}
	public void setStrText(String strText) {
		this.strText = strText;
	}
	public void setStrUsername(String strUsername) {
		this.strUsername = strUsername;
	}
	public void setStrPassword(String strPassword) {
		this.strPassword = strPassword;
	}
	public void setStrSmtp(String strSmtp) {
		this.strSmtp = strSmtp;
	}
	public void setStrFilename(String strFilename) {
		this.strFilename = strFilename;
	}
	
	/**
	 * 
	 * @author: 王仁超
	 * @Date: 2005-7-28
	 * @FileName: mail.java
	 * @PackageName: com.ahbay.mailMgr
	 * @Method Name: sendSimpleMail
	 */
	public boolean sendSimpleMail()
	{
		if(this.props == null) 
		{
			this.props = new Properties(); 
		} 
		if (!this.setSmtpHost()) return false;
		if (!this.createMimeMessage()) return false;
		if (!this.setNeedAuth()) return false;
		if (!this.setMailBody()) return false;	
		
		try
		{ 
			
			this.mimeMsg.setFrom(new InternetAddress(this.strFrom)); 
			this.mimeMsg.setRecipient(Message.RecipientType.TO, new InternetAddress(this.strTo)); 
			this.mimeMsg.setSubject(this.strSubject); 
			this.mimeMsg.setSentDate(new Date()); 
			this.mimeMsg.setContent(mptMailContent);
			this.mimeMsg.saveChanges();
			
			Transport transport;
			transport = this.session.getTransport("smtp");
			if (needAuth)
			{
				transport.connect((String)props.get("mail.smtp.host"),this.strUsername,this.strPassword);
			}
			transport.send(this.mimeMsg);	
            //transport.sendMessage(this.mimeMsg,this.mimeMsg.getAllRecipients());
            transport.close();
		} 
		catch(MessagingException m) 
		{ 
			System.err.println(m.getMessage());
			return false;
		} 
		return true;
	}
	/**
	 * 
	 * @author: 王仁超
	 * @Date: 2005-7-28
	 * @FileName: mail.java
	 * @PackageName: com.ahbay.mailMgr
	 * @Method Name: setMailFileAffix
	 */
	public boolean setMailFileAffix()
	{
		try
		{
            BodyPart bp = new MimeBodyPart(); 
			FileDataSource fileds = new FileDataSource(this.strFilename);			
			bp.setFileName(fileds.getName());
			bp.setDataHandler(new DataHandler(fileds));
			this.mptMailContent.addBodyPart(bp);
			
			BodyPart mdp=new MimeBodyPart(); 
            DataHandler dh=new DataHandler("JavaMail附件测试","text/plain;charset=gb2312");
            
            mdp.setFileName("xxf.txt");
            mdp.setDataHandler(dh); 
            this.mptMailContent.addBodyPart(mdp);
		}
		catch(Exception e)
		{
			System.err.println("增加邮件附件:"+this.strFilename+"发生错误!"+e);
			return false;
		}
		return true;
	}
	
	/*
	 * private method
	 */

	/**
	 * 
	 * @author: 王仁超
	 * @Date: 2005-7-28
	 * @FileName: mail.java
	 * @PackageName: com.ahbay.mailMgr
	 * @Method Name: setSmtpHost
	 */
	private boolean setSmtpHost()
	{  
		this.props.put("mail.smtp.host",this.strSmtp); 
		return true;
	}
	/**
	 * 
	 * @author: 王仁超
	 * @Date: 2005-7-28
	 * @FileName: mail.java
	 * @PackageName: com.ahbay.mailMgr
	 * @Method Name: createMimeMessage
	 */
	private boolean createMimeMessage()
	{
		try
		{ 
			this.session = Session.getDefaultInstance(this.props,null);
		}
		catch(Exception e)
		{
			System.err.println("获取邮件会话对象时发生错误!"+e);
			return false;
		}
		
		try
		{
			this.mimeMsg = new MimeMessage(this.session); 
			mptMailContent = new MimeMultipart();		 
		}
		catch(Exception e)
		{
			System.err.println("创建MIME邮件对象失败!"+e);
			return false;
		}
		return true;
	}
	/**
	 * 
	 * @author: 王仁超
	 * @Date: 2005-7-28
	 * @FileName: mail.java
	 * @PackageName: com.ahbay.mailMgr
	 * @Method Name:  setNeedAuth
	 */
	private boolean setNeedAuth()
	{
		if(this.needAuth)
		{
			this.props.put("mail.smtp.auth","true");
		}
		else
		{
			this.props.put("mail.smtp.auth","false");
		}
		return true;
	}
	/**
	 * 
	 * @author: 王仁超
	 * @Date: 2005-7-28
	 * @FileName: mail.java
	 * @PackageName: com.ahbay.mailMgr
	 * @Method Name: setMailBody
	 */
	private boolean setMailBody()
	{
		try
		{
			BodyPart bp = new MimeBodyPart();
			bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+this.strText,"text/html;charset=GB2312");
			this.mptMailContent.addBodyPart(bp); 
		}
		catch(Exception e)
		{
			System.err.println("设置邮件正文时发生错误!"+e);
			return false;
		}
		return true;
	}
	
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -