javaMail 邮件发送

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;


public class SmtpAuthenticator extends Authenticator
{
        private String user;
        private String password;

        public SmtpAuthenticator(String user, String password)
        {
                this.user = user;
                this.password = password;
        }

        public PasswordAuthentication getPasswordAuthentication()
        {
                return new PasswordAuthentication(this.user, this.password);
        }
}
 
 
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.SendFailedException;
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;


public class SendMailUtil {

	 public static void sendMail(MailModel mail) throws SendFailedException{ 
		  try { 
		      Properties props = new Properties();//获取系统环境 
		      Authenticator auth = new SmtpAuthenticator("邮箱名称","邮箱密码");//进行邮件服务用户认证 
		      props.put("mail.smtp.host","smtp.163.com"); 
		      props.put("mail.smtp.auth","true"); 
		      Session session = Session.getDefaultInstance(props,auth); 
		      //设置session,和邮件服务器进行通讯 
		      MimeMessage message = new MimeMessage(session); 
		      message.setContent(mail.getContent(),"text/plain");//设置邮件格式 
		      message.setSubject(mail.getSubject());//设置邮件主题 
		      
		      // 后面的bodypart将加入到此处创建的multipart中  
		      Multipart mp = new MimeMultipart();  
		      MimeBodyPart mbp = new MimeBodyPart();  
		      mbp.setContent(mail.getText(),"text/html;charset=UTF-8");// 将信件内容加入到BodyPart对象中  
		      mp.addBodyPart(mbp); // 将含有信件内容的BodyPart加入到MimeMulitipart对象中  
		      
		      if(mail.getFilename() != null)//有附件   
		      {
                mbp=new MimeBodyPart();  
                FileDataSource fds=new FileDataSource(mail.getFilename()); //得到数据源  
                mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart  
                mbp.setFileName(fds.getName());  //得到文件名同样至入BodyPart  
                mp.addBodyPart(mbp);     
	           }   
		      
		      // multipart加入到信件  
		      message.setContent(mp);  
		      message.saveChanges();
		      
		      message.setHeader(mail.getHeader(),mail.getHeader());//设置邮件标题 
		      message.setSentDate(new Date());//设置邮件发送时期 
		      Address address = new InternetAddress(mail.getAddress1(),mail.getAddress2()); 
		      message.setFrom(address);//设置邮件发送者的地址 
		      Address toaddress = new InternetAddress(mail.getReveiver());//设置邮件接收者的地址 
		      message.addRecipient(Message.RecipientType.TO,toaddress); 
		      Transport.send(message); 
		  } 
		  catch (Exception e) { 
		      e.printStackTrace(); 
		  } 
	 }
	 public static String htmlModel(String name,String code)
	 {
		 String html="";
		 html += "<html><head></head><body><h1>尊敬的"+name+",您好!恭喜您支付成功。激活码附赠如下:</h1>"+
			"<div><b>激活码:</b>"+code+"</div></body></html>";
		 return html;
	 }
}
public class MailModel {
	private String content; //设置邮件格式
	private String subject; //邮件主题 
	private String header; //邮件标题 
	private String address1; //邮件发送人的email
	private String address2; //邮件发送人的名称
	private String reveiver; //邮件接受人的email
	private String text; //邮件正文
	private String filename; //附件名称
	
	public String getFilename() {
		return filename;
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public String getReveiver() {
		return reveiver;
	}
	public void setReveiver(String reveiver) {
		this.reveiver = reveiver;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getHeader() {
		return header;
	}
	public void setHeader(String header) {
		this.header = header;
	}
	public String getAddress1() {
		return address1;
	}
	public void setAddress1(String address1) {
		this.address1 = address1;
	}
	public String getAddress2() {
		return address2;
	}
	public void setAddress2(String address2) {
		this.address2 = address2;
	}
	
}






简单的应用了一下、感觉javaMail发送时间花费的比较长。
对了,还有 mail.jar  、 activation.jar  包 



   

你可能感兴趣的:(String,properties,session,header,javamail,邮件服务器)