本文整理匯總了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);
}