本文整理汇总了Java中javax.mail.internet.MimeBodyPart.attachFile方法的典型用法代码示例。如果您正苦于以下问题:Java MimeBodyPart.attachFile方法的具体用法?Java MimeBodyPart.attachFile怎么用?Java MimeBodyPart.attachFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.mail.internet.MimeBodyPart
的用法示例。
在下文中一共展示了MimeBodyPart.attachFile方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createMimeMessageWithAttachment
import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
public static MimeMessage createMimeMessageWithAttachment(Session session, File attachment) throws MessagingException, AddressException, IOException {
MimeMessage message = createMimeMessage(session);
Multipart multiPart = new MimeMultipart();
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText("text");
multiPart.addBodyPart(textPart);
MimeBodyPart filePart = new MimeBodyPart();
filePart.attachFile(attachment);
multiPart.addBodyPart(filePart);
message.setContent(multiPart);
return message;
}
示例2: addAttachFile
import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
/**
*
* 发送邮件时添加附件列表
* @param mp
* @param attachFiles
* @return
* @throws MailException
* @throws IOException
* @throws MessagingException
*/
private Multipart addAttachFile(Multipart mp, Set<String> attachFiles) throws MailException, IOException, MessagingException{
if(mp == null){
throw new MailException("bean Multipart is null .");
}
//没有附件时直接返回
if(attachFiles == null || (attachFiles.size())== 0){
return mp;
}
Iterator<String> iterator = attachFiles.iterator();
while (iterator.hasNext()) {
String fileName = iterator.next();
MimeBodyPart mbp_file = new MimeBodyPart();
mbp_file.attachFile(fileName);
mp.addBodyPart(mbp_file);
//防止乱码
String encode = MimeUtility.encodeText(mbp_file.getFileName());
mbp_file.setFileName(encode);
}
return mp;
}
示例3: createAttachment
import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
private MimeBodyPart createAttachment(File file, String filename, String desc) throws IOException, MessagingException {
MimeBodyPart mbp = new MimeBodyPart();
mbp.attachFile(file);
FileDataSource fds = new FileDataSource(file);
mbp.setDataHandler(new DataHandler(fds));
mbp.setDescription(desc);
mbp.setFileName(MimeUtility.encodeText(filename));
return mbp;
}
示例4: sendMultipartMessage
import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
public void sendMultipartMessage(String subject, String[] to, String text, String attach)
throws MessagingException, IOException {
MimeMessage message = new MimeMessage(senderSession);
message.setFrom(new InternetAddress(pManager.get_SENDER_From())); // FROM
for(int i=0; i < to.length; i++) {
if(!to[i].equals("")) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i])); // TO
}
}
message.setSubject(subject); //SUBJECT
Multipart mp = new MimeMultipart();
BodyPart textPart = new MimeBodyPart();
textPart.setText(text);
mp.addBodyPart(textPart); // TEXT
MimeBodyPart attachPart = new MimeBodyPart();
attachPart.attachFile(attach);
mp.addBodyPart(attachPart); // ATTACH
message.setContent(mp);
transport.sendMessage(message, message.getAllRecipients());
}
示例5: addMtomPart
import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
private void addMtomPart(MimeMultipart mp, RequestableHttpVariable variable, Object httpVariableValue) throws IOException, MessagingException {
String stringValue = ParameterUtils.toString(httpVariableValue);
String filepath = Engine.theApp.filePropertyManager.getFilepathFromProperty(stringValue, getProject().getName());
String cid = variable.getMtomCid(stringValue);
Engine.logBeans.debug("(HttpConnector) Prepare the MTOM attachment with cid: " + cid + ". Converting the path '" + stringValue + "' to '" + filepath + "'");
MimeBodyPart bp = new MimeBodyPart();
bp.attachFile(filepath);
bp.setContentID(cid);
mp.addBodyPart(bp);
}
示例6: createMessageContent
import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
protected void createMessageContent(Message message, SendMailRequest request) throws MessagingException, IOException {
if (isTextOnlyMessage(request)) {
message.setText(request.getText());
} else {
Multipart multiPart = new MimeMultipart();
if (request.getText() != null) {
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText(request.getText());
multiPart.addBodyPart(textPart);
}
if (request.getHtml() != null) {
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(request.getHtml(), MailContentType.TEXT_HTML.getType());
multiPart.addBodyPart(htmlPart);
}
if (request.getFileNames() != null) {
for (String fileName : request.getFileNames()) {
MimeBodyPart part = new MimeBodyPart();
part.attachFile(fileName);
multiPart.addBodyPart(part);
}
}
message.setContent(multiPart);
}
}
示例7: sendEmail
import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
/**
* 用来发送邮件
*
* @param session 与发送邮件的主机的连接(会话)
* @param email 写好的邮件
* @throws Exception 出现异常
*/
public static void sendEmail(Session session, Email email) throws Exception {
// 获取发送邮件的信息类
MimeMessage message = new MimeMessage(session);
// 设置发送方邮件地址
message.setFrom(new InternetAddress(email.getFrom()));
// 设置发送类型和被发送方的邮件地址
if (!email.getTo().isEmpty()) {
message.setRecipients(RecipientType.TO, email.getTo());
}
if (!email.getCc().isEmpty()) {
message.setRecipients(RecipientType.CC, email.getCc());
}
if (!email.getBcc().isEmpty()) {
message.setRecipients(RecipientType.BCC, email.getBcc());
}
// 设置邮件主题
message.setSubject(email.getSubject(), "utf-8");
// 设置邮件内容
MimeMultipart content = new MimeMultipart();
// 邮件正文
MimeBodyPart text = new MimeBodyPart();
text.setContent(email.getContent(), email.getType());
content.addBodyPart(text);
// 设置附件
if (email.getAttachments() != null) {
for (AttachmentBean attachment : email.getAttachments()) {
MimeBodyPart part = new MimeBodyPart();
part.attachFile(attachment.getFile());
part.setFileName(MimeUtility.encodeText(attachment
.getFileName()));
if (attachment.getCid() != null) {
part.setContentID(attachment.getCid());
}
content.addBodyPart(part);
}
}
// 将邮件内容添加到信息中
message.setContent(content);
// 发送邮件
Transport.send(message);
}
示例8: sendEmail
import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
/**
* <p>Send email.</p>
* @param pAddParam additional param
* @param pMsg msg to mail
* @throws Exception - an exception
**/
@Override
public final void sendEmail(final Map<String, Object> pAddParam,
final EmailMsg pMsg) throws Exception {
Properties props = new Properties();
for (EmailStringProperty esp
: pMsg.getEmailConnect().getStringProperties()) {
props.put(esp.getPropertyName(), esp.getPropretyValue());
}
for (EmailIntegerProperty eip
: pMsg.getEmailConnect().getIntegerProperties()) {
props.put(eip.getPropertyName(), eip.getPropretyValue());
}
Session sess = Session.getInstance(props);
Message msg = new MimeMessage(sess);
msg.setFrom(new InternetAddress(pMsg.getEmailConnect().getUserEmail()));
if (pMsg.getErecipients().size() == 1) {
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress(pMsg.getErecipients().get(0).getItsEmail()));
} else {
InternetAddress[] address =
new InternetAddress[pMsg.getErecipients().size()];
for (int i = 0; i < pMsg.getErecipients().size(); i++) {
address[i] = new InternetAddress(pMsg.getErecipients().get(i)
.getItsEmail());
}
msg.setRecipients(Message.RecipientType.TO, address);
}
msg.setSubject(pMsg.getEsubject());
if (pMsg.getEattachments().size() > 0) {
MimeBodyPart mbpt = new MimeBodyPart();
mbpt.setText(pMsg.getEtext());
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbpt);
for (Eattachment attch : pMsg.getEattachments()) {
MimeBodyPart mbp = new MimeBodyPart();
mbp.attachFile(attch.getItsPath());
mp.addBodyPart(mbp);
}
msg.setContent(mp);
} else {
msg.setText(pMsg.getEtext());
}
msg.setSentDate(new Date());
Transport.send(msg, pMsg.getEmailConnect().getUserEmail(),
pMsg.getEmailConnect().getUserPassword());
}
示例9: sendEmailWithAttachments
import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
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);
// 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();
try {
attachPart.attachFile(filePath);
} catch (IOException ex) {
ex.printStackTrace();
}
multipart.addBodyPart(attachPart);
}
}
// sets the multi-part as e-mail's content
msg.setContent(multipart);
// sends the e-mail
Transport.send(msg);
}
示例10: sendEmailWithAttachment
import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
/**
* Sends an e-mail message from a SMTP host with a list of attached files.
*
* @param subject Mail subject
* @param message Mail content
* @param attachedFiles Attached files
*/
public static void sendEmailWithAttachment(String subject, String message, List<File> attachedFiles)
throws MessagingException {
// sets SMTP server properties
Properties properties = new Properties();
properties.put("mail.smtp.host", ApplicationUtils.getSmtpHost());
properties.put("mail.smtp.port", ApplicationUtils.getSmtpPort());
properties.put("mail.smtp.auth", ApplicationUtils.getSmtpAuth());
properties.put("mail.smtp.starttls.enable", ApplicationUtils.getSmtpStarttlsEnable());
properties.put("mail.user", ApplicationUtils.getSmtpUser());
properties.put("mail.password", ApplicationUtils.getSmtpPass());
// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(ApplicationUtils.getSmtpUser(), ApplicationUtils.getSmtpPass());
}
};
Session session = Session.getInstance(properties, auth);
// creates a new e-mail message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(ApplicationUtils.getSmtpUser()));
InternetAddress[] toAddresses = { new InternetAddress(ApplicationUtils.getAdminAddress()) };
msg.setRecipients(Message.RecipientType.TO, toAddresses);
((MimeMessage)msg).setSubject(subject,"UTF-8");
msg.setSentDate(new Date());
msg.setHeader("Content-Transfer-Encoding", "7bit");
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/html;charset=UTF-8");
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// adds attachments
if (attachedFiles != null && !attachedFiles.isEmpty()) {
for (File aFile : attachedFiles) {
MimeBodyPart attachPart = new MimeBodyPart();
try {
attachPart.attachFile(aFile);
} catch (IOException e) {
log.error("IOException occurs: ", e);
}
multipart.addBodyPart(attachPart);
}
}
// sets the multi-part as e-mail's content
msg.setContent(multipart);
// sends the e-mail
Transport.send(msg);
}