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

eimct9ow  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(188)

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

"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) "

奇怪,因为在添加附件的步骤中,他发现了它。
如果能得到帮助或指导,我将不胜感激。
我向你问好,先生。
下面是我的代码片段:

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();

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题