1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Java腾讯企业邮箱发送邮件

Java腾讯企业邮箱发送邮件

时间:2019-01-23 14:59:31

相关推荐

Java腾讯企业邮箱发送邮件

##Java通过腾讯企业邮箱发送邮件(多人发送)

企业邮箱需要使用ssl

private static String account = "企业邮箱账户";// 登录账户private static String password = "企业邮箱密码";// 登录密码private static String host = "smtp.";// 服务器地址private static String port = "465";// 端口private static String protocol = "smtp";// 协议//初始化参数public static Session initProperties() {Properties properties = new Properties();properties.setProperty("mail.transport.protocol", protocol);properties.setProperty("mail.smtp.host", host);properties.setProperty("mail.smtp.port", port);// 使用smtp身份验证properties.put("mail.smtp.auth", "true");// 使用SSL,企业邮箱必需 start// 开启安全协议MailSSLSocketFactory mailSSLSocketFactory = null;try {mailSSLSocketFactory = new MailSSLSocketFactory();mailSSLSocketFactory.setTrustAllHosts(true);} catch (GeneralSecurityException e) {e.printStackTrace();}properties.put("mail.smtp.enable", "true");properties.put("mail.smtp.ssl.socketFactory", mailSSLSocketFactory);properties.put("mail.smtp.socketFactory.class", ".ssl.SSLSocketFactory");properties.put("mail.smtp.socketFactory.fallback", "false");properties.put("mail.smtp.socketFactory.port", port);Session session = Session.getDefaultInstance(properties, new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(account, password);}});// 使用SSL,企业邮箱必需 end// TODO 显示debug信息 正式环境注释掉session.setDebug(true);return session;}// @param sender 发件人别名// @param subject 邮件主题//@param content 邮件内容//@param receiverList 接收者列表,多个接收者之间用","隔开//@param fileSrc 附件地址public void send(String sender, String subject, String content, String receiverList, String fileSrc) {try {Session session = initProperties();MimeMessage mimeMessage = new MimeMessage(session);mimeMessage.setFrom(new InternetAddress(account, sender));// 发件人,可以设置发件人的别名// 收件人,多人接收InternetAddress[] internetAddressTo = new InternetAddress().parse(receiverList);mimeMessage.setRecipients(Message.RecipientType.TO, internetAddressTo);// 主题mimeMessage.setSubject(subject);// 时间mimeMessage.setSentDate(new Date());// 容器类 附件MimeMultipart mimeMultipart = new MimeMultipart();// 可以包装文本,图片,附件MimeBodyPart bodyPart = new MimeBodyPart();// 设置内容bodyPart.setContent(content, "text/html; charset=UTF-8");mimeMultipart.addBodyPart(bodyPart);// 添加图片&附件bodyPart = new MimeBodyPart();bodyPart.attachFile(fileSrc);mimeMultipart.addBodyPart(bodyPart);mimeMessage.setContent(mimeMultipart);mimeMessage.saveChanges();Transport.send(mimeMessage);} catch (MessagingException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}

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