yangyangdamie

protected static String host = "true";
protected static String auth = "smtp.163.com";
protected static String user = "gonggao_1@163.com"; //发件人账号
protected static String password = "adccatarc2015"; //发件人密码

/**
* 申请通过发送邮件通知
* yangyang
* 2017-12-18

protected static String host = "true";
	protected static String auth = "smtp.163.com";
	protected static String user = "gonggao_1@163.com"; //发件人账号
	protected static String password = "adccatarc2015";  //发件人密码
	
	/**
	 * 申请通过发送邮件通知
	 * yangyang
	 * 2017-12-18
	 */
	public static void EmailSendInfo(String addresseeNum,String loginNum,String passwords,String enterpriseCode){
       
        final Properties props = new Properties(); // 配置发送邮件的环境属性
        
        props.put("mail.smtp.auth", host);// 表示SMTP发送邮件,需要进行身份验证
        props.put("mail.smtp.host", auth);
        props.put("mail.user", user); // 发件人的账号
        props.put("mail.password", password);  // 访问SMTP服务时需要提供的密码
        
        // 构建授权信息,用于进行SMTP进行身份验证
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // 用户名、密码
                String userName = props.getProperty("mail.user");
                String password = props.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };
        
        // 使用环境属性和授权信息,创建邮件会话
        Session mailSession = Session.getInstance(props, authenticator);
        // 创建邮件消息
        MimeMessage message = new MimeMessage(mailSession);
        // 设置发件人
        InternetAddress form;
		try {
			form = new InternetAddress(props.getProperty("mail.user"));
			
			message.setFrom(form);

	        // 设置收件人
	        InternetAddress to = new InternetAddress(addresseeNum);
	        message.setRecipient(RecipientType.TO, to);

	        // 设置邮件标题
	        message.setSubject("厂商代码申请通过");

	        // 设置邮件的内容体
	        message.setContent("企业用户您好!<br/>您的登录账号:"+loginNum+"密码:"+passwords+"厂商代码:"+enterpriseCode, "text/html;charset=UTF-8");

	        // 发送邮件
	        Transport.send(message);
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
      
    }

以上是一个简单的邮件发送的方法  需要引入javax.mail

 

<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>

 

 

 

分类:

技术点:

相关文章: