0

我已经编写了下面提到的代码,以使用 java 在电子邮件中附加 TestNG 的电子邮件报告。现在我想使用 java 将可发送电子邮件报告的详细信息粘贴到电子邮件正文中。有人对此有想法吗?

import java.io.IOException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMailSSL {
    public static void main(String[] args) {

         ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            final Properties props = new Properties();
            try {
                props.load(classLoader.getResourceAsStream("Mail/Credentials.properties"));
                String Username = props.getProperty("username");
                String Password= props.getProperty("password");
            } catch (IOException e) {
                e.printStackTrace();
            }
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", "x");
            props.put("mail.smtp.port", "25");



            Session session = Session.getInstance(props,new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(props.getProperty("username"),props.getProperty("password"));
                }
              });

            try {

                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress("x"));
                message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("x"));
                message.setSubject("Testing Subject");
                message.setText("Dear Mail Crawler,"
                    + "\n\n No spam to my email, please!");

                Transport.send(message);

                System.out.println("Done");

            } catch (MessagingException e) {
                throw new RuntimeException(e);
            }
    }
}
4

0 回答 0