sendmail.java

来自「Spring2.0宝典」· Java 代码 · 共 175 行

JAVA
175
字号
package lee;

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

/**
 * @author  yeeku.H.lee kongyeeku@163.com
 * @version  1.0
 * <br>Copyright (C), 2005-2008, yeeku.H.Lee
 * <br>This program is protected by copyright laws.
 * <br>Program Name:
 * <br>Date: 
 */

public class SendMail
{
	String to = "";//收件人
	String from = "";//发件人
	String smtpServer = "";//smtp服务器
	String username = "" ;
	String password = "" ;
	String filename = "";//附件文件名
	String subject = "";//邮件主题
	String content = "";//邮件正文
	Vector file = new Vector();//附件文件集合

	public SendMail()
	{
	}
	public SendMail(String to,String from,String smtpServer,String username,String password,String subject,String content)
	{
		this.to = to;
		this.from = from;
		this.smtpServer = smtpServer;
		this.username = username;
		this.password = password;
		this.subject = subject;
		this.content = content;
	}

	public void setSmtpServer(String smtpServer)
    {
        this.smtpServer = smtpServer;
    }
	public void setUserName(String usename)
    {
        this.username = usename;
    }
	public void setPassWord(String pwd)
    {
        this.password = pwd;
    }
	public void setTo(String to)
    {
        this.to = to; 
    }
	public void setFrom(String from)
    {
        this.from = from;
    }
	public void setSubject(String subject)
    {
        this.subject = subject;
    }
	public void setContent(String content)
    {
        this.content = content;
    }

	//把邮件主题转换为中文
	public String transferChinese(String strText)
	{
		try
		{
			strText = MimeUtility.encodeText(new String(strText.getBytes(), "GB2312"), "GB2312", "B");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return strText;
	}
	//增加附件
	public void attachfile(String fname)
	{
		file.addElement(fname);
	}

	public boolean send()
	{
		//构造mail session
		Properties props = System.getProperties();
		props.put("mail.smtp.host",smtpServer);
		props.put("mail.smtp.auth","true");
		Session session=Session.getDefaultInstance(props, new Authenticator()
			{
				public PasswordAuthentication getPasswordAuthentication()
				{
					return new PasswordAuthentication(username,password); 
				}
			});
		try
		{
			//构造MimeMessage 并设定基本的值
			MimeMessage msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(from));
			InternetAddress[] address={new InternetAddress(to)};
			msg.setRecipients(Message.RecipientType.TO,address);
			subject = transferChinese(subject);
			msg.setSubject(subject);    
			//构造Multipart
			Multipart mp = new MimeMultipart();
			//向Multipart添加正文
			MimeBodyPart mbpContent = new MimeBodyPart();
			mbpContent.setText(content);
			//向MimeMessage添加(Multipart代表正文)
			mp.addBodyPart(mbpContent);
			//向Multipart添加附件
			Enumeration efile=file.elements();
			while(efile.hasMoreElements())
			{
				MimeBodyPart mbpFile = new MimeBodyPart();
				filename=efile.nextElement().toString();
				FileDataSource fds = new FileDataSource(filename);
				mbpFile.setDataHandler(new DataHandler(fds));
				mbpFile.setFileName(fds.getName());
				//向MimeMessage添加(Multipart代表附件)
				mp.addBodyPart(mbpFile);
			}    
			file.removeAllElements();
			//向Multipart添加MimeMessage
			msg.setContent(mp);
			msg.setSentDate(new Date());
			//发送邮件
			Transport.send(msg);  
		}
		catch (MessagingException mex)
		{
			mex.printStackTrace();
			Exception ex = null;
			if ((ex=mex.getNextException())!=null)
			{
				ex.printStackTrace();
			}
			return false;
		}
		return true;
	}

	public static void main(String[] args)
	{
		SendMail sendmail = new SendMail();
		sendmail.setSmtpServer("smtp.163.com");
        //此处设置登陆的用户名
		sendmail.setUserName("spring_test");
        //此处设置登陆的密码
		sendmail.setPassWord("123456");
        //发送的地址
		sendmail.setTo("spring_test");
        //发送人
		sendmail.setFrom("spring_test");
        //设置标题
		sendmail.setSubject("你好,这是测试!");
        //设置内容
		sendmail.setContent("你好这是一个带多附件的测试!"); 
        //粘贴附件
		sendmail.attachfile("SendMail.java");
		sendmail.attachfile("build.xml");
		sendmail.send();
	}
}//end

⌨️ 快捷键说明

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