将文件附加到电子邮件
import java.io.IOException; import java.util.Properties; /*时 代 J a v a 提供*/ 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 Main { public static void main(String[] args) { String host = "smtp.somewhere.com"; String from = "someone@somewhere.com"; String to = "anotherone@somewhere.com"; /* nowjava.com - 时代Java 提 供 */ Properties properties = new Properties(); properties.put("mail.smtp.host", host); Session session = Session.getDefaultInstance(properties, null); MimeMessage message = new MimeMessage(session); try { message.setFrom(new InternetAddress(from)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject("Subject Test"); // Create Mime Content MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent("This is a test message", "text/plain"); MimeBodyPart fileBodyPart = new MimeBodyPart(); fileBodyPart.attachFile("<path-to-attachment>/attach.txt"); MimeBodyPart fileBodyPart2 = new MimeBodyPart(); fileBodyPart2.attachFile("<path-to-attachment>/attach2.txt"); Multipart multipart =