• 使用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("发送成功");
    	}

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


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

  • 相关阅读:
    ScheduledExecutorService 定时任务运行原理(三)
    ThreadPoolExecutor详解(二)
    Future模式详细讲解及实例分析
    @ApiParam @PathVariable @RequestParam三者区别
    协议及首次使用必读
    关于webmagic的依赖问题:ava.lang.NoClassDefFoundError: org/jsoup/helper/StringUtil
    空指针错误悼念与分析
    原创:idea启动项目的是否报错:java.lang.ClassNotFoundException: javax.servlet.ServletContext
    聚合工程中 context:component-scan 与数据源的思考,的使用说明
    关于component-scan中base-package包含通配符的问题探究
  • 原文地址:https://www.cnblogs.com/a1111/p/12816263.html
Copyright © 2020-2023  润新知