我可以用Java发送和发送电子邮件,并使用MimeBodyPart附加文件。我发送的附件正在转换为电子邮件正文,文件中的内容正在正文中显示。我的代码中是否有任何错误将附件转换为正文?
// http://www.codejava.net/java-ee/javamail/send-e-mail-with-attachment-in-java
package axisSharePoint;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class EmailAttachmentSender {
public static void sendEmailWithAttachments(String host, String port,
final String userName, final String password, String toAddress,
String subject, String message, String[] attachFiles)
throws AddressException, MessagingException {
// sets SMTP server properties
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
//properties.put("mail.smtp.auth", "true");
//properties.put("mail.smtp.starttls.enable", "true");
//properties.put("mail.user", userName);
//properties.put("mail.password", password);
// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
};
//Session session = Session.getInstance(properties, auth);
Session session = Session.getInstance(properties, null);
// creates a new e-mail message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(userName));
InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/html");
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// adds attachments
if (attachFiles != null && attachFiles.length > 0) {
for (String filePath : attachFiles) {
MimeBodyPart attachPart = new MimeBodyPart();
System.out.println(filePath);
//DataSource source = new FileDataSource(new
File(filePath));
//attachPart.setDataHandler(new DataHandler(source));
//attachPart.setFileName(filePath);
try {
attachPart.attachFile(new File(filePath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
multipart.addBodyPart(attachPart);
}
}
// sets the multi-part as e-mail's content
msg.setContent(multipart);
// sends the e-mail
Transport.send(msg);
}
/**
* Test sending e-mail with attachments
*/
public static void main(String[] args) {
// SMTP info
String host = "*******";
String port = "25";
String mailFrom = "*********";
String password = "";
// message info
String mailTo = "*****";
String subject = "New email with attachments";
String message = "I have some attachments for you.";
// attachments
String[] attachFiles = new String[1];
attachFiles[0]="SampleFile1.txt";
try {
sendEmailWithAttachments(host, port, mailFrom, password, mailTo,
subject, message, attachFiles);
System.out.println("Email sent.");
} catch (Exception ex) {
System.out.println("Could not send email.");
ex.printStackTrace();
}
}
}
使用以下Maven
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4.1</version>
</dependency>
以下列格式收到输出:
------=_Part_0_363771819.1499875061651
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
I have some attachments for you.
------=_Part_0_363771819.1499875061651
Content-Type: text/plain; name=SampleFile1.txt
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=SampleFile1.txt
This is External File
------=_Part_0_363771819.1499875061651--