1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > javax.mail 阿里云服务器使用163邮箱发送邮件

javax.mail 阿里云服务器使用163邮箱发送邮件

时间:2019-02-27 03:10:05

相关推荐

javax.mail  阿里云服务器使用163邮箱发送邮件

阿里云默认禁用25端口,官网建议使用465端口发送邮件

代码

public static void main(String[] args) throws Exception {try {// 邮件主题String subject = "邮箱验证";//1. 用于存放 SMTP 服务器地址等参数Properties properties = new Properties();Resource resource = new ClassPathResource("config.properties");File file = resource.getFile();properties.load(new FileInputStream(file));// 使用JavaMail发送邮件的5个步骤// 2. 创建sessionSession session = Session.getDefaultInstance(properties, new EmailAccountVo(properties.getProperty("mail.account"), properties.getProperty("mail.password")));// 开启Session的debug模式,这样就可以查看到程序发送Email的运行状态//session.setDebug(true);// 3. 创建邮件// 创建邮件对象MimeMessage message = new MimeMessage(session);// 邮件的标题message.setSubject(subject);// 邮件发送日期message.setSentDate(new Date());// 指明邮件的发件人message.setFrom(new InternetAddress(properties.getProperty("mail.address")));// 指明邮件的收件人message.setRecipient(Message.RecipientType.TO, new InternetAddress("xxx@", "xxx@"));// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件Multipart multipart = new MimeMultipart();// 添加邮件正文BodyPart contentBodyPart = new MimeBodyPart();// 邮件内容String result = "尊敬的用户,您好:</br>" +"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;您正在进行公司邮箱验证,本次邮箱验证码为:121212,验证码" + Constants.EMAIL_CODE_EXPIRES_HOUR + "小时内有效。" +"如果您未做过此操作并认为有未经授权的人员访问了您的帐户。请您尽快更改Z13平台登录密码。\n";contentBodyPart.setContent(result, "text/html;charset=UTF-8");multipart.addBodyPart(contentBodyPart);message.setContent(multipart);for (int i = 0; i < 2; i++) {// 4. 发送邮件,Transport每次发送成功程序帮忙关闭Transport.send(message, message.getAllRecipients());System.out.println("send mail result:success");}} catch (Exception e) {e.printStackTrace();}}

配置文件config.properties

#邮箱配置mail.smtp.host=mail.transport.protocol=smtpmail.smtp.auth=truemail.smtp.ssl.enable=truemail.smtp.socketFactory.fallback=true#163邮箱账号mail.account=xxxxx@#163邮箱授权码(不是密码 是授权码)mail.password=xxxxxxmail.address=xxxxx@mail.port=465mail.socketFactory.class=.ssl.SSLSocketFactorymail.socketFactory.fallback=falsemail.socketFactory.port=465

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。