-
javaMail邮件发送功能(多收件人,多抄送人,多密送人,多附件)
- private Session session;
- private Transport transport;
-
- private String mailHost = "";
- private String sender_username = "";
- private String sender_password = "";
- private String contentPart_Type = "";
-
- private Properties properties = new Properties();
-
-
- public JavaMailWithAttachment() {
- try {
- FileInputStream is=new FileInputStream("E:/workspace1.6/ICRM-H5/src/mailServer.properties");
- properties.load(is);
- this.mailHost = properties.getProperty("mail.smtp.host");
- this.sender_username = properties.getProperty("mail.sender.username");
- this.sender_password = properties.getProperty("mail.sender.password");
- this.contentPart_Type = properties.getProperty("mail.contentPart.type");
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- session = Session.getInstance(properties);
- session.setDebug(true);
- message = new MimeMessage(session);
- }
-
-
-
- public void doSendHtmlEmail(String subject, String sendHtml,
- String toUser, String ccUser, String bccUser, File [] attachment){
- try {
-
- InternetAddress from = new InternetAddress(sender_username);
- message.setFrom(from);
-
-
- if(null != toUser && !toUser.isEmpty()){
- @SuppressWarnings("static-access")
- InternetAddress[] internetAddressTo = new InternetAddress().parse(toUser);
- message.setRecipients(Message.RecipientType.TO, internetAddressTo);
- }
-
-
- if(null != ccUser && !ccUser.isEmpty()){
- @SuppressWarnings("static-access")
- InternetAddress[] internetAddressCC = new InternetAddress().parse(ccUser);
- message.setRecipients(Message.RecipientType.CC, internetAddressCC);
- }
-
-
- if(null != bccUser && !bccUser.isEmpty()){
- @SuppressWarnings("static-access")
- InternetAddress[] internetAddressBCC = new InternetAddress().parse(bccUser);
- message.setRecipients(Message.RecipientType.BCC, internetAddressBCC);
- }
-
-
- message.setSentDate(new Date());
-
-
- message.setSubject(subject);
-
-
- Multipart multipart = new MimeMultipart();
-
-
- BodyPart contentPart = new MimeBodyPart();
- contentPart.setContent(sendHtml, contentPart_Type);
- multipart.addBodyPart(contentPart);
-
- BodyPart attachmentBodyPart = null;
-
- if (null != attachment && attachment.length != 0) {
- for (File file : attachment) {
- attachmentBodyPart = new MimeBodyPart();
-
- DataSource source = new FileDataSource(file);
- attachmentBodyPart.setDataHandler(new DataHandler(source));
-
- attachmentBodyPart.setFileName(MimeUtility.encodeWord(file.getName()));
- multipart.addBodyPart(attachmentBodyPart);
- }
- }
-
-
- message.setContent(multipart);
-
-
- message.saveChanges();
-
-
- transport = session.getTransport("smtp");
- transport.connect(mailHost, sender_username, sender_password);
-
-
- transport.sendMessage(message, message.getAllRecipients());
- System.out.println("发送成功!");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (transport != null) {
- try {
- transport.close();
- } catch (MessagingException e) {
- e.printStackTrace();
- }
- }
- }
- }
-
相关阅读:
fastdfs 外网映射问题记录
fastdfs-nginx下载报错 400
nginx 代理 websocket
Jenkins 安装
实验四.2
实验四.1
实验三
shiyan2
shiyan1
作业
-
原文地址:https://www.cnblogs.com/liubaihui/p/8458457.html
Copyright © 2020-2023
润新知