发送带有附件的电子邮件的脚本-找不到文件源

时间:2018-12-06 13:22:18

标签: java katalon-studio

我需要一个脚本,该脚本将从google域发送带有附件的电子邮件。在我看来脚本几乎完成了,但是控制台向我显示了一个奇怪的异常:

"javax.mail.MessagingException: IOException while sending message;
   nested exception is:
java.io.FileNotFoundException: Screenshots_Repository/Headless_LogicVapes/My_account.png (No dry file or directory) "

奇怪,因为他在添加附件的步骤中找到了它。

感谢您的帮助或指导。

我问你 M。

有我的代码段:

import javax.activation.DataHandler
import com.sun.mail.util.MailLogger
import javax.activation.FileDataSource
import javax.mail.Message
import javax.mail.MessagingException
import javax.mail.Multipart
import javax.mail.Session
import javax.mail.Transport
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeBodyPart
import javax.mail.internet.MimeMessage
import javax.mail.internet.MimeMultipart

public class SendEmailWithAttachment {
    public static void main(String[] args) {
        SendEmailWithAttachment demo = new SendEmailWithAttachment();
        demo.sendEmail();
    }

    public void sendEmail() {

        // Defines the E-Mail information.


        String from = "xxxxx@gmail.com";
        String to = "xxxxx@gmail.com";
        String subject = "Important Message";
        String bodyText = "This is a important message with attachment.";
        String password = "pass123"

        // The attachment file name.

        String attachmentName = "⁨Screenshots_Repository⁩/Headless_LogicVapes⁩/My_account.png" 

        // Creates a Session with the following properties.

        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.port", "25");
        Session session = Session.getDefaultInstance(props);

        try {

            InternetAddress fromAddress = new InternetAddress(from);
            InternetAddress toAddress = new InternetAddress(to);

            // Create an Internet mail msg.

            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(fromAddress);
            msg.setRecipient(Message.RecipientType.TO, toAddress);
            msg.setSubject(subject);
            msg.setSentDate(new Date());
            println("created mail msg from: "+ fromAddress)


            // Set the email msg text.

            MimeBodyPart messagePart = new MimeBodyPart();
            messagePart.setText(bodyText);
            println("Set msg text: "+ bodyText)

            // Set the email attachment file

            FileDataSource fileDataSource = new FileDataSource(attachmentName);

            MimeBodyPart attachmentPart = new MimeBodyPart();
            attachmentPart.setDataHandler(new DataHandler(fileDataSource));
            attachmentPart.setFileName(fileDataSource.getName());
            println("Set attachment: "+ attachmentName)


            // Create Multipart E-Mail.

            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messagePart);
            multipart.addBodyPart(attachmentPart);
            println("Created Multipart: "+ messagePart + " + " + attachmentName)

            msg.setContent(multipart);

            // Send the msg. Don't forget to set the username and password
            // to authenticate to the mail server.

            Transport tr = session.getTransport("smtps");
            tr.connect("smtp.gmail.com", from, password);
            tr.sendMessage(msg, msg.getAllRecipients());
            System.out.println("Mail Sent Successfully");
            tr.close();


            } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

SendEmailWithAttachment demo = new SendEmailWithAttachment();
demo.sendEmail();

0 个答案:

没有答案