使用Java语言,写出便利邮件Content内容的代码
时间: 2024-03-25 16:37:49 浏览: 52
以下是使用Java语言,便利邮件Content内容的代码示例:
```java
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Base64;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class EmailContentBuilder {
private MimeMessage message;
private Multipart multipart;
public EmailContentBuilder(MimeMessage message) throws MessagingException {
this.message = message;
this.multipart = new MimeMultipart();
this.message.setContent(this.multipart);
}
public void setText(String text) throws MessagingException {
BodyPart bodyPart = new MimeBodyPart();
bodyPart.setText(text);
this.multipart.addBodyPart(bodyPart);
}
public void setHtml(String html) throws MessagingException {
BodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent(html, "text/html");
this.multipart.addBodyPart(bodyPart);
}
public void addAttachment(String filePath) throws IOException, MessagingException {
File file = new File(filePath);
DataSource dataSource = new FileDataSource(file);
BodyPart bodyPart = new MimeBodyPart();
bodyPart.setDataHandler(new DataHandler(dataSource));
bodyPart.setFileName(file.getName());
this.multipart.addBodyPart(bodyPart);
}
public void addInlineImage(String filePath, String contentId) throws IOException, MessagingException {
File file = new File(filePath);
DataSource dataSource = new FileDataSource(file);
BodyPart bodyPart = new MimeBodyPart();
bodyPart.setDataHandler(new DataHandler(dataSource));
bodyPart.setHeader("Content-ID", "<" + contentId + ">");
this.multipart.addBodyPart(bodyPart);
}
public void addBase64EncodedImage(String base64Content, String contentId) throws MessagingException {
byte[] bytes = Base64.getDecoder().decode(base64Content);
DataSource dataSource = new ByteArrayDataSource(bytes, "image/png");
BodyPart bodyPart = new MimeBodyPart();
bodyPart.setDataHandler(new DataHandler(dataSource));
bodyPart.setHeader("Content-ID", "<" + contentId + ">");
this.multipart.addBodyPart(bodyPart);
}
private static class ByteArrayDataSource implements DataSource {
private byte[] bytes;
private String contentType;
public ByteArrayDataSource(byte[] bytes, String contentType) {
this.bytes = bytes;
this.contentType = contentType;
}
@Override
public InputStream getInputStream() {
return new ByteArrayInputStream(this.bytes);
}
@Override
public OutputStream getOutputStream() {
throw new UnsupportedOperationException("Not supported");
}
@Override
public String getContentType() {
return this.contentType;
}
@Override
public String getName() {
return null;
}
}
}
```
上述代码实现了一个邮件内容构造器,支持添加文本、HTML、附件、内联图片和Base64编码图片。使用示例:
```java
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("sender@example.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com"));
message.setSubject("My Email Subject");
EmailContentBuilder contentBuilder = new EmailContentBuilder(message);
contentBuilder.setText("Hello world!");
contentBuilder.setHtml("<h1>Hello world!</h1>");
contentBuilder.addAttachment("/path/to/myfile.pdf");
contentBuilder.addInlineImage("/path/to/myimage.png", "myimage");
contentBuilder.addBase64EncodedImage("iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AA...", "mybase64image");
Transport.send(message);
```
上述代码会发送一封邮件,包含文本、HTML、附件、内联图片和Base64编码图片。
阅读全文