jsp,java项目邮箱注册发送邮件

由于项目需要用户邮箱注册首先要导入javaMail包,然后在项目中加上下面的程序即可。

package com.bikehui.DB.daoImpls;

import java.util.Properties;

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 songwenju
 * 发邮件
 *
 */
public class SendMail {
	private String from = ***@163.com"; 				        //你本人要用的邮箱地址,必须是163邮箱
	private String user = "***";						//发件人称号,保证和上一条@之前的内容相同
	String password = "***";					        //发件人邮箱
	
	//构造函数
	public SendMail(){
	}
	
	/**
	 * 
	 * @param to
	 * @param text
	 * @param title
	 */
	/*发送验证信息的邮件*/
	public void sendMail(String to,String text,String title){
		Properties props = new Properties();
		props.setProperty("mail.smtp.host", "smtp.163.com");    // 设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器) 
		props.put("mail.smtp.host","smtp.163.com");  		// 需要经过授权,也就是有户名和密码的校验,这样才能通过验证(一定要有这一条) 
		props.put("mail.smtp.auth", "true");  			// 用刚刚设置好的props对象构建一个session 
		Session session = Session.getDefaultInstance(props);    // 有了这句便可以在发送邮件的过程中在console处显示过程信息,供调试使  用(你可以在控制台(console)上看到发送邮件的过程)  
		session.setDebug(true);  				// 用session为参数定义消息对象  
		MimeMessage message = new MimeMessage(session);  	// 加载发件人地址   
		try{
			message.setFrom(new InternetAddress(from));   		 
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 加载收件人地址    
			message.setSubject(title);   		    // 加载标题      
			Multipart multipart = new MimeMultipart();  // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
			BodyPart contentPart = new MimeBodyPart();  // 设置邮件的文本内容  
			contentPart.setContent(text,"text/html;charset=utf-8");  
			multipart.addBodyPart(contentPart);
			message.setContent(multipart);  
			message.saveChanges();   				//保存变化 
			Transport transport = session.getTransport("smtp");   	// 连接服务器的邮箱  
			transport.connect("smtp.163.com", user, password);   	// 把邮件发送出去  
			transport.sendMessage(message, message.getAllRecipients());   
			transport.close();
		} catch(MessagingException e){
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 
	 * @param to
	 * @param userName
	 * @return
	 */
	public String sendRegistMail(String to,String userName){
		String registerId = "" + Math.random() * Math.random();
		String url = "http://***/RegisterMailBackServlet?registerId=" + registerId+"&userName="+userName;//点击该链接回到项目中
		String text = "<p>亲爱的用户,您好!感谢您注册bike汇自行车网,请点击下面的链接完成邮箱验证:</p>" +
				"<a href='"+url+"'>"+url+"</a><br /><p></p>如链接点击无效,您可以将链接复制到浏览器中直接打开。<p>bike汇自行车网</p>";	
                                                                //发送html格式的邮件到邮箱
		String title = "[bike汇]网站注册";
		sendMail(to,text,title);			//待会用户点在邮箱中点击这个链接回到你的网站
		return registerId;	
	}
        
	public static void main(String []args){                //做测试用
		SendMail s = new SendMail();
		s.sendMail("1565270590@qq.com", "你好,云水", "[bike汇]找回您的帐户密码");
	}
}



补充点内容,现在使用163邮箱登陆要用授权码了,而不是密码登陆,否则会出现

535 Error: authentication failed

授权码设置位置:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值