Here is an example of sending an email with multiple parts using JavaMail:

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class EmailSender {
    public static void main(String[] args) throws MessagingException {
        // Set up properties for SMTP server
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.starttls.enable", "true");

        // Set up session with username and password
        Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("yourusername@gmail.com", "yourpassword");
            }
          });

        // Create message
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("yourusername@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("recipient@example.com"));
        message.setSubject("Example Email");

        // Create multipart message
        Multipart multipart = new MimeMultipart();

        // Create text part
        MimeBodyPart textPart = new MimeBodyPart();
        String textContent = "Hello, this is an example email.";
        textPart.setText(textContent);

        // Add text part to multipart message
        multipart.addBodyPart(textPart);

        // Create image part
        MimeBodyPart imagePart = new MimeBodyPart();
        String imagePath = "C:/exampleImage.jpg";
        DataSource source = new FileDataSource(imagePath);
        imagePart.setDataHandler(new DataHandler(source));
        imagePart.setFileName("exampleImage.jpg");

        // Add image part to multipart message
        multipart.addBodyPart(imagePart);

        // Set multipart message as the message content
        message.setContent(multipart);

        // Send message
        Transport.send(message);

        System.out.println("Email sent successfully!");
    }
}

In this example, we first set up the properties for the SMTP server, including the host, authentication, port, and TLS encryption. We then set up a session with the username and password for the email account.

Next, we create the message and set the from, to, and subject fields. We then create a multipart message using a MimeMultipart object.

We create a text part using a MimeBodyPart object and set its content to a string. We add this text part to the multipart message.

We also create an image part using another MimeBodyPart object and set its content to an image file. We add this image part to the multipart message.

Finally, we set the multipart message as the content of the message and send it using the Transport.send() method.

Note that in this example, we are sending the email from a Gmail account. If you are using a different email provider, you may need to adjust the properties accordingly

javamail multipart example

原文地址: http://www.cveoy.top/t/topic/fKUy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录