springboot 发送表格形式的邮件代码
时间: 2023-10-20 08:09:55 浏览: 109
好的,我可以为您提供相应的代码示例,请您参考以下代码:
```java
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class MailService {
private final String username = "sender@yourdomain.com";
private final String password = "password";
public void sendTableMail() {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("sender@yourdomain.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("receiver@yourdomain.com"));
message.setSubject("Table Example");
StringBuilder builder = new StringBuilder();
builder.append("<html><body>");
builder.append("<table border=1><tr><th>Name</th><th>Age</th></tr>");
builder.append("<tr><td>John Doe</td><td>28</td></tr>");
builder.append("<tr><td>Jane Smith</td><td>32</td></tr>");
builder.append("</table></body></html>");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(builder.toString(), "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
System.out.println("Table mail sent successfully!");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
```
请将 `sender@yourdomain.com` 更改为您自己的发件人邮箱,`password` 更改为您的邮箱密码,`receiver@yourdomain.com` 更改为您的收件人邮箱。
此代码片段可以通过电子邮件发送带有表格形式的HTML消息。
希望对您有所帮助!
阅读全文