Java发送邮件实例

   发纯文本的邮件          

 

             首先下载commons-email-1.1.jar、mail.jar和activation.jar(它们可以从sun的网站上下载,从下载的javamail和jaf中找到)。
         (一)使用javax.mail.*

 

  import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @description 
 * @author lyf 
 * @createTime Jul 28, 2011
 * @version 1.1.1.1
 */
public class SendTextMail {
 String SMTPHost="";//SMTP服务器
 String user="";//登录SMTP服务器的账号
 String passwd;
 String from="";//发件人邮箱
 String to="";//收件人邮箱
 String subject="";//邮件标题
 String content="";//邮件内容
 public SendTextMail()
 {
  
 }
 
 /**
  * 发送邮件
  * @createTime Jul 28, 2011
  */
 public void sendMail()
 {
  Properties props=new Properties();
  props.put("mail.smtp.host", SMTPHost);
  props.put("mail.smtp.auth", "true");
  
  try {
   SmtpAuth auth=new SmtpAuth();
   auth.setAccount(user, passwd);
   //create a Session
   Session mailSession=Session.getDefaultInstance(props,auth);
   mailSession.setDebug(true);
   //create a Message Object
   Message message=new MimeMessage(mailSession);
   //sender
   message.setFrom(new InternetAddress(from));
   //getter
   message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
   //subject
   message.setSubject(subject);
   //set the content
   message.setText(content);
   //send time
   message.setSentDate(new Date());
   //priority 1:urgency 2:general 3:slow
   message.setHeader("X-Priority", "1");
   message.saveChanges();
   //create a Transport Object
   Transport transport=mailSession.getTransport("smtp");
   //connect to SMTP Server
   transport.connect(SMTPHost, user, passwd);
   //sendMail
   transport.sendMessage(message, message.getAllRecipients());
   
  } catch (Exception e) {
   
   e.printStackTrace();
  }
 }
 static class SmtpAuth extends Authenticator
 {
  String user,passwd;
  //setting the user information
  void setAccount(String user,String passwd)
  {
   this.user=user;
   this.passwd=passwd;
  }
  protected PasswordAuthentication getPasswordAuthentication()
  {
   return (new PasswordAuthentication(user,passwd));
   
  }
 }
 /**
  * @return the sMTPHost
  */
 public String getSMTPHost() {
  return SMTPHost;
 }
 /**
  * @param host the sMTPHost to set
  */
 public void setSMTPHost(String host) {
  SMTPHost = host;
 }
 /**
  * @return the user
  */
 public String getUser() {
  return user;
 }
 /**
  * @param user the user to set
  */
 public void setUser(String user) {
  this.user = user;
 }
 /**
  * @return the from
  */
 public String getFrom() {
  return from;
 }
 /**
  * @param from the from to set
  */
 public void setFrom(String from) {
  this.from = from;
 }
 /**
  * @return the to
  */
 public String getTo() {
  return to;
 }
 /**
  * @param to the to to set
  */
 public void setTo(String to) {
  this.to = to;
 }
 /**
  * @return the subject
  */
 public String getSubject() {
  return subject;
 }
 /**
  * @param subject the subject to set
  */
 public void setSubject(String subject) {
  
  try {
   subject=new String(subject.getBytes("ISO8859-1"),"UTF-8");
  } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  this.subject = subject;
 }
 /**
  * @return the content
  */
 public String getContent() {
  return content;
 }
 /**
  * @param content the content to set
  */
 public void setContent(String content) {
  try {
   content=new String(content.getBytes("ISO8859-1"),"UTF-8");
  } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  this.content = content;
 }
 /**
  * @return the passwd
  */
 public String getPasswd() {
  return passwd;
 }
 /**
  * @param passwd the passwd to set
  */
 public void setPasswd(String passwd) {
  this.passwd = passwd;
 }
 

}

   (二)使用org.apache.commons.mail.*(此方法比方法一简单很多)

import org.apache.commons.mail.EmailException;     
import org.apache.commons.mail.SimpleEmail;     
    
public class Send      
{     
	
	private String SMTPHost="";//SMTP服务器
	private String user="";//登录SMTP服务器的账号
	private String passwd;
	private String from="";//发件人邮箱
	private String to="";//收件人邮箱
	private String subject="";//邮件标题
	private String content="";//邮件内容    
     
    public Send(String host, String user, String passwd,String to, String subject, String content) {
		
		SMTPHost = host;
		this.user = user;
		this.passwd = passwd;
		this.from = user;
		this.to = to;
		this.subject = subject;
		this.content = content;
	}

	public static void main(String[] args)     
    {     
    	Send send=new Send("smtp.163.com","****@163.com","******","**email_address**","","");
    
    	send.setSubject("注册成功!!!");
    	send.setContent("您好!!!您的注册已通过审核请注意查看!!!");
    	send.send();     
    }     
         
    public void send()     
    {     
        SimpleEmail email = new SimpleEmail();        
        email.setTLS(true);             
        email.setHostName(this.SMTPHost);           
        email.setAuthentication(this.user, this.passwd);        
             
        try      
        {   
        	//to
            email.addTo(this.to); 
            //from
            email.setFrom(this.from);          
             //subject   
            email.setSubject(this.subject);                 //标题        
            //setting Charset         
            email.setCharset("UTF-8");                     
             //content    
            email.setMsg(this.content);           
            //send     
            email.send();  
            System.out.println("发送完毕");
                 
        } catch (EmailException e) {     
            e.printStackTrace();     
        }      
    }

	/**
	 * @return the sMTPHost
	 */
	public String getSMTPHost() {
		return SMTPHost;
	}

	/**
	 * @param host the sMTPHost to set
	 */
	public void setSMTPHost(String host) {
		SMTPHost = host;
	}

	/**
	 * @return the user
	 */
	public String getUser() {
		return user;
	}

	/**
	 * @param user the user to set
	 */
	public void setUser(String user) {
		this.user = user;
	}

	/**
	 * @return the passwd
	 */
	public String getPasswd() {
		return passwd;
	}

	/**
	 * @param passwd the passwd to set
	 */
	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	/**
	 * @return the from
	 */
	public String getFrom() {
		return from;
	}

	/**
	 * @param from the from to set
	 */
	public void setFrom(String from) {
		this.from = from;
	}

	/**
	 * @return the to
	 */
	public String getTo() {
		return to;
	}

	/**
	 * @param to the to to set
	 */
	public void setTo(String to) {
		this.to = to;
	}

	/**
	 * @return the subject
	 */
	public String getSubject() {
		return subject;
	}

	/**
	 * @param subject the subject to set
	 */
	public void setSubject(String subject) {
		this.subject = subject;
	}

	/**
	 * @return the content
	 */
	public String getContent() {
		return content;
	}

	/**
	 * @param content the content to set
	 */
	public void setContent(String content) {
		this.content = content;
	}     
}    

 

另外还有发送html的邮件和包含附件的邮件,在此只谈一下发纯文本的邮件

你可能感兴趣的:(javaEmail)