繁体   English   中英

无法在ExtentReport中查看使用JavaMail API发送到其他电子邮件ID的嵌入式图像?

[英]Unable to view inline images in ExtentReport that was sent to a different email id using JavaMail API?

对于我的硒Web驱动程序测试,我在框架中使用扩展报告,该报告存储在服务器的共享文件夹中。 测试完成后,将使用JAVAMAIL API将报告作为附件发送到各种电子邮件ID。 除了我之外,不是每个人都可以在报告中看到嵌入式图像。 我不知道该怎么打。 请找到我创建的电子邮件的附件代码。 等待快速响应

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class EmailAttachmentSender {

    public static void sendEmailWithAttachments(Map<String, String> map, List<String> attachFiles)
            throws AddressException, MessagingException {
        // sets SMTP server properties
        Properties properties = new Properties();
        properties.put("mail.smtp.host", map.get("host"));
        properties.put("mail.smtp.port", map.get("port"));
        properties.put("mail.smtp.auth", "true");
        //properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.socketFactory.class","java.net.ssl.SSLSocketFactory");
        properties.put("mail.user", map.get("mailFrom"));
        properties.put("mail.password", map.get("password"));

        try
        {
        // creates a new session with an authenticator
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(map.get("mailFrom"), map.get("password"));
            }
        };
        Session session = Session.getInstance(properties, auth);

        // creates a new e-mail message
        Message msg = new MimeMessage(session);

        msg.setFrom(new InternetAddress(map.get("mailFrom")));


        InternetAddress[] toAddresses = new InternetAddress[map.get("mailTo").split(",").length];
        for (int i =0;i<map.get("mailTo").split(",").length;i++)
        {
            toAddresses[i]=new InternetAddress(map.get("mailTo").split(",")[i]);
        }

        if(map.get("mailTo").split(",").length!=0)
        {
            msg.setRecipients(Message.RecipientType.TO, toAddresses);
        }


        InternetAddress[] ccAddresses = new InternetAddress[map.get("mailCc").split(",").length];

        for (int i =0;i<map.get("mailCc").split(",").length;i++)
        {
            ccAddresses[i]=new InternetAddress(map.get("mailCc").split(",")[i]);
        }

        if(map.get("mailCc").split(",").length!=0)
        {
            msg.setRecipients(Message.RecipientType.CC, ccAddresses);
        }



        msg.setSubject(map.get("subject"));
        msg.setSentDate(new Date());

        // creates message part
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent(map.get("message"), "text/plain");

        // creates multi-part
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);

        // adds attachments
        if (attachFiles != null && attachFiles.size() > 0) {
            for (String filePath : attachFiles) {
                MimeBodyPart attachPart = new MimeBodyPart();

                try {
                     //attachPart.attachFile(filePath);
                    DataSource source = new FileDataSource(filePath);

                    attachPart.setDataHandler(new DataHandler(source));

                    attachPart.setFileName(filePath);



                } catch (Exception ex) {
                    ex.printStackTrace();
                }

                multipart.addBodyPart(attachPart);
            }
        }

        // sets the multi-part as e-mail's content
        msg.setContent(multipart);

        // sends the e-mail
        Transport.send(msg);

    }

    catch(Exception e)
    {
        System.out.println(" You might have entered wrong email information in the Configuration.properties file. Please recheck");
    }
    }

    /**
     * Test sending e-mail with attachments
     */
  //public static void main(String[] args) {
        public static void sendEmail(String reportPath) {

       Map<String,String> map = new HashMap<String,String>();
        // SMTP info
        String host = UtilityClass.getPropertyData("host");
        String port = UtilityClass.getPropertyData("port");
        String mailFrom = UtilityClass.getPropertyData("mailFrom");
        String password = UtilityClass.getPropertyData("password");


        map.put("host", host);
        map.put("port", port);
        map.put("mailFrom", mailFrom);
        map.put("password", password);



        // message info
        String mailTo = UtilityClass.getPropertyData("mailTo");
        String mailCc = UtilityClass.getPropertyData("mailCc");
        String subject = UtilityClass.getPropertyData("subject");
        String message = UtilityClass.getPropertyData("message");

        map.put("mailTo", mailTo);
        map.put("mailCc", mailCc);
        map.put("subject", subject);
        map.put("message", message);

        // attachments
       //String attachFile = "E:/LeEco/Automation/Framework_India/LeMall/test-output/ExtentReport/LeMallIndia-PC-16122016_034807.html";
      System.out.println(reportPath);
        String attachFile =  reportPath;
        List<String> attachFiles = new ArrayList<String>();
        attachFiles.add(attachFile);

        try {
            if(!map.get("mailTo").equals("")||map.get("mailTo")!=null)
            {
                 sendEmailWithAttachments(map, attachFiles);
                        System.out.println("Email sent.");
            }

        } catch (Exception ex) {
            System.out.println("Could not send email.");
            ex.printStackTrace();
        }
    }
}

如果附加的html文件仅引用本地磁盘上的图像(通过使用显式“ file:” URL或通过使用与html文件在本地磁盘上存储的位置有关的相对URL),则没有其他对象将能够看到他们。

您要么需要将图像放置在Web服务器上,并确保html文件使用Web服务器的“ http:” URL对其进行引用,要么需要将html文件和图像打包到一个多部分/相关的MIME部分中并确保html文件使用“ cid:” URL引用图像。

JavaMail FAQ包含更多信息。

另外,您可能需要修复其中一些常见的JavaMail错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM