下面列出了javax.mail.BodyPart#setFileName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void addAttachment(String filepath, String filename) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
javax.activation.DataSource source = new FileDataSource(filepath);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
_multipart.addBodyPart(messageBodyPart);
}
private static BodyPart getBodyPart(String fileLoc, FilenameFilter fexFilter) throws MessagingException, IOException {
BodyPart messageBodyPart = new MimeBodyPart();
File fileName = new File(fileLoc);
if (fileName.isDirectory()) {
fileName = zipFolder(fileLoc, fexFilter);
}
DataSource source = new FileDataSource(fileName);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName.getName());
return messageBodyPart;
}
protected MimeMessage appendIcsBody(MimeMessage msg, MailMessage m) throws Exception {
log.debug("setMessageBody for iCal message");
// -- Create a new message --
Multipart multipart = new MimeMultipart();
Multipart multiBody = new MimeMultipart("alternative");
BodyPart html = new MimeBodyPart();
html.setDataHandler(new DataHandler(new ByteArrayDataSource(m.getBody(), "text/html; charset=UTF-8")));
multiBody.addBodyPart(html);
BodyPart iCalContent = new MimeBodyPart();
iCalContent.addHeader("content-class", "urn:content-classes:calendarmessage");
iCalContent.setDataHandler(new DataHandler(new ByteArrayDataSource(new ByteArrayInputStream(m.getIcs()),
"text/calendar; charset=UTF-8; method=REQUEST")));
multiBody.addBodyPart(iCalContent);
BodyPart body = new MimeBodyPart();
body.setContent(multiBody);
multipart.addBodyPart(body);
BodyPart iCalAttachment = new MimeBodyPart();
iCalAttachment.setDataHandler(new DataHandler(new ByteArrayDataSource(new ByteArrayInputStream(m.getIcs()),
"application/ics")));
iCalAttachment.removeHeader("Content-Transfer-Encoding");
iCalAttachment.addHeader("Content-Transfer-Encoding", "base64");
iCalAttachment.removeHeader("Content-Type");
iCalAttachment.addHeader("Content-Type", "application/ics");
iCalAttachment.setFileName("invite.ics");
multipart.addBodyPart(iCalAttachment);
msg.setContent(multipart);
return msg;
}
private void sendIcalMessage() throws Exception {
log.debug("sendIcalMessage");
// Building MimeMessage
MimeMessage mimeMessage = mailHandler.getBasicMimeMessage();
mimeMessage.setSubject(subject);
mimeMessage.addRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients, false));
// -- Create a new message --
BodyPart msg = new MimeBodyPart();
msg.setDataHandler(new DataHandler(new ByteArrayDataSource(htmlBody,
"text/html; charset=\"utf-8\"")));
Multipart multipart = new MimeMultipart();
BodyPart iCalAttachment = new MimeBodyPart();
iCalAttachment.setDataHandler(new DataHandler(
new javax.mail.util.ByteArrayDataSource(
new ByteArrayInputStream(iCalMimeBody),
"text/calendar;method=REQUEST;charset=\"UTF-8\"")));
iCalAttachment.setFileName("invite.ics");
multipart.addBodyPart(iCalAttachment);
multipart.addBodyPart(msg);
mimeMessage.setSentDate(new Date());
mimeMessage.setContent(multipart);
// -- Set some other header information --
// mimeMessage.setHeader("X-Mailer", "XML-Mail");
// mimeMessage.setSentDate(new Date());
// Transport trans = session.getTransport("smtp");
Transport.send(mimeMessage);
}
@Override
protected void addAttachment(String name, DataHandler data) throws MessagingException {
BodyPart attachment = new MimeBodyPart();
attachment.setDataHandler(data);
attachment.setFileName(name);
attachment.setHeader("Content-ID", "<" + name + ">");
iBody.addBodyPart(attachment);
}
public void addAttachment(String filename) throws Exception
{
if(filename!=null)
{
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
}
}
public void addAttachment(String filename) throws Exception
{
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
_multipart.addBodyPart(messageBodyPart);
}
private void setAttachments(MailSession mailSession, MimeMessage msg, String messageTypeWithCharset) throws MessagingException {
List<MailAttachmentStream> attachmentList = mailSession.getAttachmentList();
String message = mailSession.getMessage();
if (attachmentList == null || attachmentList.size() == 0) {
log.debug("no attachments found to attach to mailSession");
msg.setContent(message, messageTypeWithCharset);
} else {
if(log.isDebugEnabled()) log.debug("found ["+attachmentList.size()+"] attachments to attach to mailSession");
Multipart multipart = new MimeMultipart();
BodyPart messagePart = new MimeBodyPart();
messagePart.setContent(message, messageTypeWithCharset);
multipart.addBodyPart(messagePart);
int counter = 0;
for (MailAttachmentStream attachment : attachmentList) {
counter++;
String name = attachment.getName();
if (StringUtils.isEmpty(name)) {
name = getDefaultAttachmentName() + counter;
}
log.debug("found attachment [" + attachment + "]");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setFileName(name);
try {
ByteArrayDataSource bads = new ByteArrayDataSource(attachment.getContent(), attachment.getMimeType());
bads.setName(attachment.getName());
messageBodyPart.setDataHandler(new DataHandler(bads));
} catch (IOException e) {
log.error("error attaching attachment to MailSession", e);
}
multipart.addBodyPart(messageBodyPart);
}
msg.setContent(multipart);
}
}
/**
* Attach a file specified as a DataSource interface.
*
* @param ds A DataSource interface for the file.
* @param name The name field for the attachment.
* @param description A description for the attachment.
* @param disposition Either mixed or inline.
* @return A MultiPartEmail.
* @throws EmailException see javax.mail.internet.MimeBodyPart
* for definitions
* @since 1.0
*/
public MultiPartEmail attach(
final DataSource ds,
String name,
final String description,
final String disposition)
throws EmailException
{
if (EmailUtils.isEmpty(name))
{
name = ds.getName();
}
final BodyPart bodyPart = createBodyPart();
try
{
bodyPart.setDisposition(disposition);
bodyPart.setFileName(MimeUtility.encodeText(name));
bodyPart.setDescription(description);
bodyPart.setDataHandler(new DataHandler(ds));
getContainer().addBodyPart(bodyPart);
}
catch (final UnsupportedEncodingException uee)
{
// in case the file name could not be encoded
throw new EmailException(uee);
}
catch (final MessagingException me)
{
throw new EmailException(me);
}
setBoolHasAttachments(true);
return this;
}
public void sendMail (
String server,
String from, String to,
String subject, String text,
final String login,
final String password,
String filename
)
{
Properties properties = new Properties();
properties.put ("mail.smtp.host", server);
properties.put ("mail.smtp.socketFactory.port", "465");
properties.put ("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put ("mail.smtp.auth", "true");
properties.put ("mail.smtp.port", "465");
try {
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(login, password);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new InternetAddress (to));
message.setSubject(subject);
Multipart multipart = new MimeMultipart ();
BodyPart bodyPart = new MimeBodyPart ();
bodyPart.setContent (text, "text/html; charset=utf-8");
multipart.addBodyPart (bodyPart);
if (filename != null) {
bodyPart.setDataHandler (new DataHandler (new FileDataSource (filename)));
bodyPart.setFileName (filename);
multipart.addBodyPart (bodyPart);
}
message.setContent(multipart);
Transport.send(message);
} catch (Exception e) {
Log.e ("Send Mail Error!", e.getMessage());
}
}