Java邮件发送:Java邮件发送的10个经典案例与代码实现技巧
蜂-邮



【蜂邮EDM】:邮件群发系统,EDM邮件营销平台,邮件代发服务。 查看价格
【AokSend邮件API】:触发式邮件API,15元/万封,99%送达率。 查看价格
【烽火邮箱】:新人领取免费域名邮箱,可用作企业邮箱公司邮箱。 查看价格
Java邮件发送:Java邮件发送的10个经典案例与代码实现技巧
在很多企业的后台系统中,Java邮件发送功能是一项非常常见的需求。通过Java发送邮件,我们可以实现自动化的邮件通知功能。本文将分享10个经典的Java邮件发送案例,并提供相关代码实现技巧。
1. 使用JavaMail发送基本邮件
JavaMail是一个非常强大的邮件发送库,使用JavaMail可以非常方便地发送邮件。以下是一个基本的邮件发送代码:
import javax.mail.*;import javax.mail.internet.*;import java.util.Properties;public class SimpleEmail {public static void main(String[] args) throws MessagingException {Properties props = new Properties();props.put("mail.smtp.host", "smtp.example.com");props.put("mail.smtp.auth", "true");Session session = Session.getDefaultInstance(props, new Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication("username", "password");}});MimeMessage message = new MimeMessage(session);message.setFrom(new InternetAddress("from@example.com"));message.addRecipient(Message.RecipientType.TO, new InternetAddress("to@example.com"));message.setSubject("Test Email");message.setText("This is a test email");Transport.send(message);}}2. 发送带附件的邮件
除了基本的邮件内容,Java还支持发送带附件的邮件。下面是一个发送带附件的邮件的代码示例:

import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;import java.io.File;import java.util.Properties;public class EmailWithAttachment {public static void main(String[] args) throws MessagingException {Properties props = new Properties();props.put("mail.smtp.host", "smtp.example.com");props.put("mail.smtp.auth", "true");Session session = Session.getDefaultInstance(props, new Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication("username", "password");}});MimeMessage message = new MimeMessage(session);message.setFrom(new InternetAddress("from@example.com"));message.addRecipient(Message.RecipientType.TO, new InternetAddress("to@example.com"));message.setSubject("Test Email with Attachment");MimeBodyPart messageBodyPart = new MimeBodyPart();messageBodyPart.setText("This is an email with an attachment.");Multipart multipart = new MimeMultipart();multipart.addBodyPart(messageBodyPart);MimeBodyPart attachmentPart = new MimeBodyPart();attachmentPart.attachFile(new File("file.txt"));multipart.addBodyPart(attachmentPart);message.setContent(multipart);Transport.send(message);}}3. 发送HTML格式邮件
使用HTML格式的邮件,可以让邮件的内容更加生动和丰富。下面是一个发送HTML邮件的代码示例:
Powered By 蜂.邮.EDMimport javax.mail.*;import javax.mail.internet.*;import java.util.Properties;public class HtmlEmail {public static void main(String[] args) throws MessagingException {Properties props = new Properties();props.put("mail.smtp.host", "smtp.example.com");props.put("mail.smtp.auth", "true");Session session = Session.getDefaultInstance(props, new Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication("username", "password");}});MimeMessage message = new MimeMessage(session);message.setFrom(new InternetAddress("from@example.com"));message.addRecipient(Message.RecipientType.TO, new InternetAddress("to@example.com"));message.setSubject("Test HTML Email");String htmlContent = "This is a HTML email
Hello, this is a sample HTML email.
";message.setContent(htmlContent, "text/html");Transport.send(message);}}4. 发送带嵌入图片的邮件
你还可以通过Java发送带有嵌入图片的邮件,这对于需要展示图像的营销邮件非常有用。以下是一个带嵌入图片的邮件示例:
import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;import java.util.Properties;public class EmbeddedImageEmail {public static void main(String[] args) throws MessagingException {Properties props = new Properties();props.put("mail.smtp.host", "smtp.example.com");props.put("mail.smtp.auth", "true");Session session = Session.getDefaultInstance(props, new Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication("username", "password");}});MimeMessage message = new MimeMessage(session);message.setFrom(new InternetAddress("from@example.com"));message.addRecipient(Message.RecipientType.TO, new InternetAddress("to@example.com"));message.setSubject("Test Email with Embedded Image");MimeBodyPart messageBodyPart = new MimeBodyPart();messageBodyPart.setContent("Test
", "text/html");Multipart multipart = new MimeMultipart();multipart.addBodyPart(messageBodyPart);MimeBodyPart imagePart = new MimeBodyPart();DataSource fds = new FileDataSource("image.jpg");imagePart.setDataHandler(new DataHandler(fds));imagePart.setHeader("Content-ID", "");multipart.addBodyPart(imagePart);message.setContent(multipart);Transport.send(message);}} 5. 发送带SSL加密的邮件
为了提高邮件的安全性,可以使用SSL加密发送邮件。下面是使用SSL加密发送邮件的示例代码:
import javax.mail.*;import javax.mail.internet.*;import java.util.Properties;public class SslEmail {public static void main(String[] args) throws MessagingException {Properties props = new Properties();props.put("mail.smtp.host", "smtp.example.com");props.put("mail.smtp.ssl.enable", "true");props.put("mail.smtp.port", "465");props.put("mail.smtp.auth", "true");Session session = Session.getDefaultInstance(props, new Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication("username", "password");}});MimeMessage message = new MimeMessage(session);message.setFrom(new InternetAddress("from@example.com"));message.addRecipient(Message.RecipientType.TO, new InternetAddress("to@example.com"));message.setSubject("Test SSL Email");message.setText("This is an SSL encrypted email");Transport.send(message);}}6. 使用Java发送HTML模板邮件
在邮件营销中,经常需要根据HTML模板发送邮件。Java通过使用模板引擎(如FreeMarker或Velocity)可以动态生成HTML内容并发送邮件。以下是一个简单的HTML模板邮件示例:
import javax.mail.*;import javax.mail.internet.*;import java.util.Properties;public class TemplateEmail {public static void main(String[] args) throws MessagingException {Properties props = new Properties();props.put("mail.smtp.host", "smtp.example.com");props.put("mail.smtp.auth", "true");Session session = Session.getDefaultInstance(props, new Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication("username", "password");}});MimeMessage message = new MimeMessage(session);message.setFrom(new InternetAddress("from@example.com"));message.addRecipient(Message.RecipientType.TO, new InternetAddress("to@example.com"));message.setSubject("Test Email with Template");String template = "${name}
Welcome to our service!
";String personalizedContent = template.replace("${name}", "John Doe");message.setContent(personalizedContent, "text/html");Transport.send(message);}}7. 使用Java发送HTML模板邮件并附带附件
通过将HTML模板与附件结合,可以发送更为丰富的邮件内容,适合各种复杂的邮件需求。
通过以上Java邮件发送的经典案例,你可以实现多种功能,包括发送带附件、HTML邮件、加密邮件等。掌握这些技巧,将大大提升你在邮件开发方面的能力。

