基于netbean使用javamail的发送邮件的教程


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hzx.util;



import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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;
import javax.mail.internet.MimeUtility;

import java.util.Vector;

/**
 *
 * @author zhang
 */
public class EmailUtils {
    //公司邮箱
    private static final String EMAIL="zhang@sina.com";
    //公司邮箱帐号
    private static final String EMAILTOP="zhang";
    //邮箱别名
    private static final String EMAILNAME="***";
    //公司邮箱密码
    private static final String EMAILKEY="***";
    //邮箱的host
    private static final String HOST="smtp.sina.com";
    //找回邮箱的主题
    private static final String EMAILTITLE="——找回密码";
   
    
   /*
    *  功能:找回密码
    */
    public static void sendKeyUtil(String key,String toEmail) throws Exception{
       //System.out.println("com.hzx.util.EmailUtils.sendKeyUtil():"+key+toEmail+HOST);
    	//启动邮箱
        Session session=EmailUtils.init();
        //创建邮件
        Message msg = EmailUtils.createSimpleMail(session,toEmail,key);
        //发送邮件
        EmailUtils.send(session,msg);
    }
    
    
    /*
    连接邮箱
    */
    private static Session init() {
        Properties prop = new Properties();
        prop.setProperty("mail.smtp.auth","true" );
        prop.setProperty("mail.transport.protocol", "smtp");
        Session session=Session.getInstance(prop);
        //session.setDebug(true);
        return session;
    }
    

    /*
    发送消息
    */
    private static void send(Session session,Message msg) throws Exception {
        Transport transport=session.getTransport();
        System.out.print(HOST+EMAILTOP+EMAILKEY);
        
      //  transport.connect(host,25, emailTop, emailKey);
       transport.connect(HOST,25, EMAILTOP,EMAILKEY);
        transport.sendMessage(msg, msg.getAllRecipients());
        transport.close();
    }
     
    /*
    创建找回密码的邮件
    */
     public static MimeMessage createSimpleMail(Session session,String toEmail,String key)
	            throws Exception {
	          //创建邮件对象
	          MimeMessage message = new MimeMessage(session);
	         //指明邮件的发件人
	          message.setFrom(new InternetAddress(EMAIL,EMAILNAME));
	       //  message.setFrom(new InternetAddress("gacl@sohu.com"));
	         //指明邮件的收件人
	          message.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
	          //邮件的标题
	          message.setSubject(EMAILTITLE);
	         //邮件的文本内容
	          message.setContent("你的密码是 :"+key, "text/html;charset=UTF-8");
	          //返回创建好的邮件对象
	          return message;
	      }
 
  
} 
   
    
    

首先引用mail.tar(在官方网下载即可),再将以上的代码引入就可以发送邮件给其他人。

你登录自己邮箱是看不到已经发送出去的邮箱。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值