使用java底层实现邮件的发送(含测试,源码)

     直接上代码:3个类,两个主要的类,一个测试类:

   主类(Mail):

 /**  
* @Title: Mail.java
* @Package org.service.impl
* @Description: TODO该方法的主要作用:
* @author A18ccms A18ccms_gmail_com  
* @date 2017-5-30 下午10:20:35
* @version V1.0  
*/
package org.service.impl;

import java.util.Date;
import java.util.Properties;

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

 /**   
 *    
 * 项目名称:spring_Schop8   
 * 类名称:Mail   
 * 类描述:   不用spring实现邮件发送
 * 创建人:Mu Xiongxiong  
 * 创建时间:2017-5-30 下午10:20:35   
 * 修改人:Mu Xiongxiong   
 * 修改时间:2017-5-30 下午10:20:35   
 * 修改备注:   
 * @version    
 *    
 */
public class Mail {
	
	private String mailServer,from ,to , mailSubject,mailContent,username,password;

	public Mail(String mailServer, String from, String to, String mailSubject,
			String mailContent, String username, String password) {
		this.mailServer = mailServer;
		this.from = from;
		this.to = to;
		this.mailSubject = mailSubject;
		this.mailContent = mailContent;
		this.username = username;
		this.password = password;
	}

	public Mail() {
		username = "tina";
		password = "123"; 
		mailServer = "mail.com";
		from  = "tina@mail.com";
		to  = "jerry@mail.com";
		mailSubject = "问好";
		mailContent = "好久没见,最近可好!";
		
	}
	
	public void send(){
		//设置服务器
		Properties prop = System.getProperties();
		prop.put("mail.smtp.host", mailServer);
		prop.put("mail.smtp.auth", "true");
		//产生Session服务
		EmailAuthenticator mailauth = new EmailAuthenticator(username, password);
		Session mailSession = Session.getInstance(prop,(Authenticator)mailauth);
		//封装message对象
		Message message = new MimeMessage(mailSession);
		try {
			message.setFrom(new InternetAddress(from));    //设置发送人
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); //设置主题
			message.setSubject(mailSubject);                                      //设置标题
			message.setContent(mailContent,"text/html;charset=utf-8");            //设置内容
			message.setSentDate(new Date());                                      //设置日期
			//和粗昂就爱你Transport实例
			Transport tran = mailSession.getTransport("smtp");
			tran.send(message, message.getAllRecipients());
			tran.close();
		} catch (AddressException e) {
			// TODO 异常执行块!
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}    //
		
	}
	
	

}

第二个主类(EmailAuthenticator):

package org.service.impl;


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

import org.hibernate.validator.constraints.Email;

public class EmailAuthenticator extends Authenticator {
	private String username=null;
	private String userpass = null;
	
	
	
	public EmailAuthenticator(String username, String userpass) {
		super();
		this.username = username;
		this.userpass = userpass;
	}


	public  PasswordAuthentication getPasswordAuthentication(){
		return new PasswordAuthentication(username, userpass);
	}
	
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getUserpass() {
		return userpass;
	}
	public void setUserpass(String userpass) {
		this.userpass = userpass;
	}
	

}

测试类:

/**
	 * 
	* @Title: testMail1
	* @Description: 该方法的主要作用:java实现底层的邮件发送
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void testMail1(){
		Mail mail = new Mail();
		mail.send();
		System.out.println("发送成功");
	}

源码上完了,再看一下运行的结果吧:


可以看到发送人,发送时间,还有发送内容!已经发送成功!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

穆雄雄

哎,貌似还没开张来着呢~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值