JavaMail API 发送电子邮件带有附件
下面是一个例子从你的机器上选择附件发送电子邮件。放置到 /home/manisha/ 在本地机器上的 file.txt文件。在这里,我们使用JangoSMPT服务器通过该电子邮件被发送到我们的目标电子邮件地址。
要发送电子邮件,其中包含一个内嵌的图像,遵循的步骤是:
-
获得一个会话 Session
-
创建一个默认 MimeMessage 对象,并设置发件人,收件人,主题(From, To, Subject)在消息中。
-
实际的消息如下:
messageBodyPart.setText("This is message body");
-
创建一个MimeMultipart的对象。添加上述messageBodyPart与实际的消息在其设置,这个多重对象。
-
接下来创建一个DataHandler的如下添加附件:
messageBodyPart = new MimeBodyPart(); String filename = "/home/manisha/file.txt"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart);
-
接下来设置多重如下消息中:
message.setContent(multipart);
-
发送使用传输对象的消息。
创建Java类
创建一个Java类文件SendAttachmentInEmail,其内容如下:
package com.yiibai; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; 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 SendAttachmentInEmail { public static void main(String[] args) { // Recipient's email ID needs to be mentioned. String to = "destinationemail@gmail.com"; // Sender's email ID needs to be mentioned String from = "fromemail@gmail.com"; final String username = "manishaspatil";//change accordingly final String password = "******";//change accordingly // Assuming you are sending email through relay.jangosmtp.net String host = "relay.jangosmtp.net"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", "25"); // Get the Session object. Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { // Create a default MimeMessage object. Message message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); // Set Subject: header field message.setSubject("Testing Subject"); // Create the message part BodyPart messageBodyPart = new MimeBodyPart(); // Now set the actual message messageBodyPart.setText("This is message body"); // Create a multipar message Multipart multipart = new MimeMultipart(); // Set text message part multipart.addBodyPart(messageBodyPart); // Part two is attachment messageBodyPart = new MimeBodyPart(); String filename = "/home/manisha/file.txt"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); // Send the complete message parts message.setContent(multipart); // Send message Transport.send(message); System.out.println("Sent message successfully...."); } catch (MessagingException e) { throw new RuntimeException(e); } } }
由于我们使用的是由主机提供商JangoSMTP提供的SMTP服务器,我们需要验证用户名和密码。javax.mail.PasswordAuthentication类用于验证的密码。
编译并运行
现在,我们班是准备好了,让我们编译上面的类。我已经保存了类SendAttachmentInEmail.java到目录:/home/manisha/JavaMailAPIExercise.我们需要的jar文件:javax.mail.jar和activation.jar文件在classpath。执行下面的命令从命令提示符编译类(两个jar被放置在/home/manisha/ 目录下):
javac -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: SendAttachmentInEmail.java
现在,这个类被编译,执行下面的命令来运行:
java -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: SendAttachmentInEmail
验证输出
你应该看到下面的消息命令控制台上:
Sent message successfully....
因为我通过JangoSMTP发送邮件到我的Gmail地址,下面的邮件会在我的Gmail帐户的收件箱中接收: