发送带有附件的电子邮件至gmail
//来 自 时代Java公众号 import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; 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 sendfile { String from = "aaa@gmail.com"; String to = null; String d_host = "smtp.gmail.com"; String d_port = "465"; String subject = "Important Message"; String bodyText = null; /* from 时 代 Java - nowjava.com */ String filename = null; /** * JavaProgrammingForums.com */ public static void main(String[] args) { } public sendfile(String m1_to, String m1_text, String fl) { to = m1_to; bodyText = m1_text; filename = fl; } public void sendEmail() { Properties properties = new Properties(); properties.put("mail.smtp.host", "smtp.gmail.com"); properties.put("mail.smtp.port", "465"); Session session = Session.getDefaultInstance(properties, null); try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject(subject); message.setSentDate(new Date()); // // Set the email message text. // MimeBodyPart messagePart = new MimeBodyPart(); messagePart.setText(bodyText); // // Set the email attachment file // MimeBodyPart attachmentPart = new MimeBodyPart(); FileDataSource fileDataSource = new FileDataSource(filename) { @Override public String getContentType() {