BaymaxSky / selenium-webdriver-serenity

Các hướng dẫn cho cộng đồng
0 stars 0 forks source link

Send Email #26

Open BaymaxSky opened 1 year ago

BaymaxSky commented 1 year ago

SMTP

https://github.com/maildev/maildev

exec-maven-plugin

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <configuration>
                    <mainClass>com.baymax.sky.SendMailLocalDevMail</mainClass>
                </configuration>
            </plugin>
        </plugins>

Java code

package com.baymaxsky;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class SendMailLocalDevMail {

    // https://github.com/maildev/maildev

    public static final String HOST_MAIL = "0.0.0.0";
    public static final String PORT = "1025";
    public static final String user_auth = "maildev@dev.com";
    public static final String password = "maildev";
    public static final String _from = "support.services@baymaxsky.com";
    public static final String _to = "community@baymaxsky.com";

    public static void main(String[] args) {

        Properties props = new Properties();
        props.put("mail.smtp.host", HOST_MAIL);
        props.put("mail.smtp.port", PORT);

        Session session = Session.getInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user_auth, password);
            }
        });

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(_from));
            message.setRecipients(
                    Message.RecipientType.TO,
                    InternetAddress.parse(_to));
            message.setSubject("[BaymaxSky] Automation Team Report Daily");

            BufferedReader br = new BufferedReader(
                    new FileReader("./target/site/serenity/serenity-summary.html"));
            StringBuilder content = new StringBuilder();
            String line;

            while ((line = br.readLine()) != null) {
                content.append(line);
            }
            br.close();

            message.setContent(content.toString(), "text/html");
            Transport.send(message);
        } catch (MessagingException | IOException e) {
            e.printStackTrace();
        }
    }
}

command line

mvn exec:java
BaymaxSky commented 1 year ago
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.5.5</version>
        </dependency>
BaymaxSky commented 1 year ago

fixed: Cần move java class file vào phần src/main/java với package là com.baymax.sky

image
Jacobvu84 commented 1 year ago

Video Hướng dẫn

BaymaxSky commented 1 year ago

Resolve Gmail /Google SMTP less secure apps connectivity Issue