java简易版email邮箱发送邮件

  1. 首先是引入jar包,activation.jar和mail.jar(在资源中已上传了这两个包)。
  2. 紧接着是发送用户的邮箱和开通邮箱smtp的密钥,关于开通邮箱smtp的方法自己去百度一下哈。

  3. public static class MailAuthenticator extends Authenticator {
            private String strUser;
            private String strPwd;
    
            public MailAuthenticator() {
                super();
            }
    
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                String username = this.strUser;
                String password = this.strPwd;
                if ((username != null) && (username.length() > 0) && (password != null) && (password.length() > 0)) {
    
                    return new PasswordAuthentication(username, password);
                }
    
                return null;
            }
    
            public MailAuthenticator(String user, String password) {
                this.strUser = user;
                this.strPwd = password;
            }
        }

     

  4. 紧接着是调用方法,我将电子信箱的一些配置做在数据库中,这样可以灵活的进行配置。参数to是指接收邮件的邮箱,参数content是指邮箱所要发送的内容。关于信箱的一些配置,你们可以自己进行替换补充。

    public String sendMail(String to,String content)throws Exception{
    		if (to !=null){
                String host = (String) CHSApp.getSysParamBykey("email.host"); //发件人使用发邮件的电子信箱服务器
                String from = (String) CHSApp.getSysParamBykey("email.user"); //发邮件的出发地(发件人的信箱)
                String password = (String) CHSApp.getSysParamBykey("email.pwd"); //发邮件的密码
                String subject = (String) CHSApp.getSysParamBykey("email.subject"); //发邮件的主题
                String port = (String) CHSApp.getSysParamBykey("email.smtp.port"); //电子信箱服务器的端口号
                try {
                    Properties properties = System.getProperties();
                    properties.put("mail.smtp.host", host);
                    properties.put("mail.smtp.auth", "true");
                    properties.put("mail.user", from);
                    properties.put("mail.password", password);
                    properties.put("mail.smtp.port", port);
                    properties.put("mail.smtp.starttls.enable", "true");
                    MailAuthenticator mailAuthenticator = new MailAuthenticator(from,password);
                    Session session = Session.getInstance(properties, mailAuthenticator);
                    session.setDebug(true);
                    MimeMessage mimeMessage = new MimeMessage(session);
                    mimeMessage.setFrom(new InternetAddress(from));
                    mimeMessage.addRecipient(Message.RecipientType.TO,
                            new InternetAddress(to));
                    mimeMessage.setSubject(subject);
                    MimeBodyPart mimeBodyPart = new MimeBodyPart();
                    mimeBodyPart.setContent(content, "text/html;charset=utf-8");
                    Multipart multipart = new MimeMultipart();
                    multipart.addBodyPart(mimeBodyPart);
                    mimeMessage.setContent(multipart);
                    mimeMessage.setSentDate(new Date());
                    mimeMessage.saveChanges();
                    Transport.send(mimeMessage);
                }catch (Exception e){
                    e.printStackTrace();
                    return "failure";
                }
                return "success";
    		}else {
    		    return "failure";
            }
    	}

     

你可能感兴趣的:(java基础,java,email)