本文整理汇总了Java中javax.mail.internet.MimeMultipart.addBodyPart方法的典型用法代码示例。如果您正苦于以下问题:Java MimeMultipart.addBodyPart方法的具体用法?Java MimeMultipart.addBodyPart怎么用?Java MimeMultipart.addBodyPart使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.mail.internet.MimeMultipart
的用法示例。
在下文中一共展示了MimeMultipart.addBodyPart方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setText
import javax.mail.internet.MimeMultipart; //导入方法依赖的package包/类
/**
* Set the given plain text and HTML text as alternatives, offering
* both options to the email client. Requires multipart mode.
* <p><b>NOTE:</b> Invoke {@link #addInline} <i>after</i> {@code setText};
* else, mail readers might not be able to resolve inline references correctly.
* @param plainText the plain text for the message
* @param htmlText the HTML text for the message
* @throws MessagingException in case of errors
*/
public void setText(String plainText, String htmlText) throws MessagingException {
Assert.notNull(plainText, "Plain text must not be null");
Assert.notNull(htmlText, "HTML text must not be null");
MimeMultipart messageBody = new MimeMultipart(MULTIPART_SUBTYPE_ALTERNATIVE);
getMainPart().setContent(messageBody, CONTENT_TYPE_ALTERNATIVE);
// Create the plain text part of the message.
MimeBodyPart plainTextPart = new MimeBodyPart();
setPlainTextToMimePart(plainTextPart, plainText);
messageBody.addBodyPart(plainTextPart);
// Create the HTML text part of the message.
MimeBodyPart htmlTextPart = new MimeBodyPart();
setHtmlTextToMimePart(htmlTextPart, htmlText);
messageBody.addBodyPart(htmlTextPart);
}
示例2: addBodyPart
import javax.mail.internet.MimeMultipart; //导入方法依赖的package包/类
/**
* Method addBodyPart
*
* @param mp
* @param dh
*/
private static void addBodyPart(MimeMultipart mp, DataHandler dh) {
MimeBodyPart messageBodyPart = new MimeBodyPart();
try {
messageBodyPart.setDataHandler(dh);
String contentType = dh.getContentType();
if ((contentType == null) || (contentType.trim().length() == 0)) {
contentType = "application/octet-stream";
}
System.out.println("Content type: " + contentType);
messageBodyPart.setHeader(HEADER_CONTENT_TYPE, contentType);
messageBodyPart.setHeader(
HEADER_CONTENT_TRANSFER_ENCODING,
"binary"); // Safe and fastest for anything other than mail
mp.addBodyPart(messageBodyPart);
} catch (javax.mail.MessagingException e) {
}
}
示例3: buildEmailBodyPart
import javax.mail.internet.MimeMultipart; //导入方法依赖的package包/类
private MimeBodyPart buildEmailBodyPart() throws MessagingException {
final MimeMultipart emailContent = new MimeMultipart("alternative");
// add from low fidelity to high fidelity
if (StringUtils.isNotBlank(text)) {
final MimeBodyPart textBodyPart = buildTextBodyPart();
emailContent.addBodyPart(textBodyPart);
}
if (StringUtils.isNotBlank(html)) {
final MimeBodyPart htmlBodyPart = buildHtmlBodyPart();
emailContent.addBodyPart(htmlBodyPart);
}
final MimeBodyPart emailBodyPart = new MimeBodyPart();
emailBodyPart.setContent(emailContent);
return emailBodyPart;
}
示例4: addToMimeMultipart
import javax.mail.internet.MimeMultipart; //导入方法依赖的package包/类
public void addToMimeMultipart (@Nonnull final MimeMultipart aMimeMultipart) throws MessagingException
{
ValueEnforcer.notNull (aMimeMultipart, "MimeMultipart");
final MimeBodyPart aMimeBodyPart = new MimeBodyPart ();
aMimeBodyPart.setHeader (CHttpHeader.CONTENT_ID, getId ());
// !IMPORTANT! DO NOT CHANGE the order of the adding a DH and then the last
// headers
// On some tests the datahandler did reset content-type and transfer
// encoding, so this is now the correct order
aMimeBodyPart.setDataHandler (new DataHandler (_getAsDataSource ()));
// After DataHandler!!
aMimeBodyPart.setHeader (CHttpHeader.CONTENT_TYPE, getMimeType ());
aMimeBodyPart.setHeader (CHttpHeader.CONTENT_TRANSFER_ENCODING, getContentTransferEncoding ().getID ());
aMimeMultipart.addBodyPart (aMimeBodyPart);
}
示例5: sendEmail
import javax.mail.internet.MimeMultipart; //导入方法依赖的package包/类
public void sendEmail() throws MessagingException {
checkSettings();
Properties props = new Properties();
if (_usesAuth) {
props.put("mail." + protocol + ".auth", "true");
props.put("mail.user", _mailUser);
props.put("mail.password", _mailPassword);
} else {
props.put("mail." + protocol + ".auth", "false");
}
props.put("mail." + protocol + ".host", _mailHost);
props.put("mail." + protocol + ".timeout", _mailTimeout);
props.put("mail." + protocol + ".connectiontimeout", _connectionTimeout);
props.put("mail.smtp.starttls.enable", _tls);
props.put("mail.smtp.ssl.trust", _mailHost);
Session session = Session.getInstance(props, null);
Message message = new MimeMessage(session);
InternetAddress from = new InternetAddress(_fromAddress, false);
message.setFrom(from);
for (String toAddr : _toAddress)
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
toAddr, false));
message.setSubject(_subject);
message.setSentDate(new Date());
if (_attachments.size() > 0) {
MimeMultipart multipart =
this._enableAttachementEmbedment ? new MimeMultipart("related")
: new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(_body.toString(), _mimeType);
multipart.addBodyPart(messageBodyPart);
// Add attachments
for (BodyPart part : _attachments) {
multipart.addBodyPart(part);
}
message.setContent(multipart);
} else {
message.setContent(_body.toString(), _mimeType);
}
// Transport transport = session.getTransport();
SMTPTransport t = (SMTPTransport) session.getTransport(protocol);
try {
connectToSMTPServer(t);
} catch (MessagingException ste) {
if (ste.getCause() instanceof SocketTimeoutException) {
try {
// retry on SocketTimeoutException
connectToSMTPServer(t);
logger.info("Email retry on SocketTimeoutException succeeded");
} catch (MessagingException me) {
logger.error("Email retry on SocketTimeoutException failed", me);
throw me;
}
} else {
logger.error("Encountered issue while connecting to email server", ste);
throw ste;
}
}
t.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
t.close();
}
示例6: send
import javax.mail.internet.MimeMultipart; //导入方法依赖的package包/类
/**
* 发送邮件
* @param receiver 接收者邮箱地址
* @param subject 邮件主题
* @param content 邮件内容
*/
public static void send(String receiver, String subject, String content) {
try {
//设置SSL连接、邮件环境
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = System.getProperties();
props.setProperty("mail.smtp.host", SMTP_HOST);
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", SMTP_PORT);
props.setProperty("mail.smtp.socketFactory.port", SMTP_SOCKET_FACTORY_PORT);
props.setProperty("mail.smtp.auth", "true");
//建立邮件会话
Session session = Session.getDefaultInstance(props, new Authenticator() {
//身份认证
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(SENDER, PASSWORD);
}
});
//建立邮件对象
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(SENDER, SENDER_NAME));
message.setRecipients(Message.RecipientType.TO, receiver);
message.setSubject(subject);
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(content, "text/html;charset=UTF-8");
MimeMultipart mimeMultipart = new MimeMultipart();
mimeMultipart.addBodyPart(mimeBodyPart);
message.setContent(mimeMultipart);
message.saveChanges();
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
示例7: addAlternativePart
import javax.mail.internet.MimeMultipart; //导入方法依赖的package包/类
/**
* Add a multipart/alternative part to message body
*
* @param plainContent
* the content of the text/plain sub-part
* @param htmlContent
* the content of the text/html sub-part
* @param charset
* the character set for the part
* @throws PackageException
*/
@PublicAtsApi
public void addAlternativePart(
String plainContent,
String htmlContent,
String charset ) throws PackageException {
MimeMultipart alternativePart = new MimeMultipart("alternative");
try {
// create a new text/plain part
MimeBodyPart plainPart = new MimeBodyPart();
plainPart.setText(plainContent, charset, PART_TYPE_TEXT_PLAIN);
plainPart.setDisposition(MimeBodyPart.INLINE);
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setText(htmlContent, charset, PART_TYPE_TEXT_HTML);
htmlPart.setDisposition(MimeBodyPart.INLINE);
alternativePart.addBodyPart(plainPart, 0);
alternativePart.addBodyPart(htmlPart, 1);
MimeBodyPart mimePart = new MimeBodyPart();
mimePart.setContent(alternativePart);
addPart(mimePart, PART_POSITION_LAST);
} catch (MessagingException me) {
throw new PackageException(me);
}
}
示例8: sendEmail
import javax.mail.internet.MimeMultipart; //导入方法依赖的package包/类
public static void sendEmail(String host, String port,
final String userName, final String password, String toAddress,
String subject, String message, String nombreArchivoAdj, String urlArchivoAdj)
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");
// 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);
//Se crea la parte del cuerpo del mensaje.
BodyPart texto = new MimeBodyPart();
texto.setText(message);
BodyPart adjunto = new MimeBodyPart();
if(nombreArchivoAdj != null ){
adjunto.setDataHandler(new DataHandler(new FileDataSource(urlArchivoAdj)));
adjunto.setFileName(nombreArchivoAdj);
}
//Juntar las dos partes
MimeMultipart multiParte = new MimeMultipart();
multiParte.addBodyPart(texto);
if (nombreArchivoAdj != null) multiParte.addBodyPart(adjunto);
// creates a new e-mail message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(userName));
InternetAddress[] toAddresses = null;
toAddresses = InternetAddress.parse(toAddress, false);
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
//msg.setText(message);
msg.setContent(multiParte);
// sends the e-mail
Transport.send(msg);
}
示例9: addMtomPart
import javax.mail.internet.MimeMultipart; //导入方法依赖的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);
}
示例10: createMimeMultiparts
import javax.mail.internet.MimeMultipart; //导入方法依赖的package包/类
/**
* Determine the MimeMultipart objects to use, which will be used
* to store attachments on the one hand and text(s) and inline elements
* on the other hand.
* <p>Texts and inline elements can either be stored in the root element
* itself (MULTIPART_MODE_MIXED, MULTIPART_MODE_RELATED) or in a nested element
* rather than the root element directly (MULTIPART_MODE_MIXED_RELATED).
* <p>By default, the root MimeMultipart element will be of type "mixed"
* (MULTIPART_MODE_MIXED) or "related" (MULTIPART_MODE_RELATED).
* The main multipart element will either be added as nested element of
* type "related" (MULTIPART_MODE_MIXED_RELATED) or be identical to the root
* element itself (MULTIPART_MODE_MIXED, MULTIPART_MODE_RELATED).
* @param mimeMessage the MimeMessage object to add the root MimeMultipart
* object to
* @param multipartMode the multipart mode, as passed into the constructor
* (MIXED, RELATED, MIXED_RELATED, or NO)
* @throws MessagingException if multipart creation failed
* @see #setMimeMultiparts
* @see #MULTIPART_MODE_NO
* @see #MULTIPART_MODE_MIXED
* @see #MULTIPART_MODE_RELATED
* @see #MULTIPART_MODE_MIXED_RELATED
*/
protected void createMimeMultiparts(MimeMessage mimeMessage, int multipartMode) throws MessagingException {
switch (multipartMode) {
case MULTIPART_MODE_NO:
setMimeMultiparts(null, null);
break;
case MULTIPART_MODE_MIXED:
MimeMultipart mixedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_MIXED);
mimeMessage.setContent(mixedMultipart);
setMimeMultiparts(mixedMultipart, mixedMultipart);
break;
case MULTIPART_MODE_RELATED:
MimeMultipart relatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED);
mimeMessage.setContent(relatedMultipart);
setMimeMultiparts(relatedMultipart, relatedMultipart);
break;
case MULTIPART_MODE_MIXED_RELATED:
MimeMultipart rootMixedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_MIXED);
mimeMessage.setContent(rootMixedMultipart);
MimeMultipart nestedRelatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED);
MimeBodyPart relatedBodyPart = new MimeBodyPart();
relatedBodyPart.setContent(nestedRelatedMultipart);
rootMixedMultipart.addBodyPart(relatedBodyPart);
setMimeMultiparts(rootMixedMultipart, nestedRelatedMultipart);
break;
default:
throw new IllegalArgumentException("Only multipart modes MIXED_RELATED, RELATED and NO supported");
}
}
示例11: createAttachMail
import javax.mail.internet.MimeMultipart; //导入方法依赖的package包/类
/**
* @Method: createAttachMail
* @Description: ����һ����������ʼ�
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createAttachMail(String subject, String content, String filePath) throws Exception{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
if(recipients.contains(";")){
List<InternetAddress> list = new ArrayList<InternetAddress>();
String []median=recipients.split(";");
for(int i=0;i<median.length;i++){
list.add(new InternetAddress(median[i]));
}
InternetAddress[] address =list.toArray(new InternetAddress[list.size()]);
message.setRecipients(Message.RecipientType.TO,address);
}else{
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
}
message.setSubject(subject);
MimeBodyPart text = new MimeBodyPart();
text.setContent(content, "text/html;charset=UTF-8");
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource(filePath));
attach.setDataHandler(dh);
attach.setFileName(dh.getName());
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(text);
mp.addBodyPart(attach);
mp.setSubType("mixed");
message.setContent(mp);
message.saveChanges();
return message;
}
示例12: createImageMail
import javax.mail.internet.MimeMultipart; //导入方法依赖的package包/类
/**
* @Method: createImageMail
* @Description: ����һ���ʼ����Ĵ�ͼƬ���ʼ�
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createImageMail(String subject, String content, String imagePath) throws Exception {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
if(recipients.contains(";")){
List<InternetAddress> list = new ArrayList<InternetAddress>();
String []median=recipients.split(";");
for(int i=0;i<median.length;i++){
list.add(new InternetAddress(median[i]));
}
InternetAddress[] address =list.toArray(new InternetAddress[list.size()]);
message.setRecipients(Message.RecipientType.TO,address);
}else{
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
}
message.setSubject(subject);
MimeBodyPart text = new MimeBodyPart();
text.setContent(content, "text/html;charset=UTF-8");
MimeBodyPart image = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource(imagePath));
image.setDataHandler(dh);
image.setContentID("xxx.jpg");
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.setSubType("related");
message.setContent(mm);
message.saveChanges();
return message;
}
示例13: createMixedMail
import javax.mail.internet.MimeMultipart; //导入方法依赖的package包/类
/**
* @Method: createMixedMail
* @Description: ����һ��������ʹ�ͼƬ���ʼ�
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createMixedMail(String subject, String content, String imagePath, String filePath) throws Exception {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
if(recipients.contains(";")){
List<InternetAddress> list = new ArrayList<InternetAddress>();
String []median=recipients.split(";");
for(int i=0;i<median.length;i++){
list.add(new InternetAddress(median[i]));
}
InternetAddress[] address =list.toArray(new InternetAddress[list.size()]);
message.setRecipients(Message.RecipientType.TO,address);
}else{
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
}
message.setSubject(subject);
MimeBodyPart text = new MimeBodyPart();
text.setContent(content,"text/html;charset=UTF-8");
MimeBodyPart image = new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource(imagePath)));
image.setContentID("aaa.jpg");
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource(filePath));
attach.setDataHandler(dh);
attach.setFileName(dh.getName());
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(text);
mp.addBodyPart(image);
mp.setSubType("related");
MimeBodyPart bodyContent = new MimeBodyPart();
bodyContent.setContent(mp);
message.saveChanges();
return message;
}
示例14: sendTextEmail
import javax.mail.internet.MimeMultipart; //导入方法依赖的package包/类
public static void sendTextEmail(String recvEmail) {
try {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.qq.com");
props.setProperty("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.socketFactory.port", "994");
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("463112653", "manllfvunnfwbjhh");
}
});
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setSubject("Hello Vme");
//整个邮件的MultiPart(不能直接加入内容,需要在bodyPart中加入)
Multipart emailPart = new MimeMultipart();
MimeBodyPart attr1 = new MimeBodyPart();
attr1.setDataHandler(new DataHandler(new FileDataSource("E:/workspaces/Archon/src/main/webapp/uploadfile/head_img/2601169057.png")));
attr1.setFileName("tip.pic");
MimeBodyPart attr2 = new MimeBodyPart();
attr2.setDataHandler(new DataHandler(new FileDataSource("E:/workspaces/Archon/src/main/webapp/uploadfile/head_img/1724836491.png")));
attr2.setFileName(MimeUtility.encodeText("哦图像"));
MimeBodyPart content = new MimeBodyPart();
MimeMultipart contentPart = new MimeMultipart();
MimeBodyPart imgPart = new MimeBodyPart();
imgPart.setDataHandler(new DataHandler(new FileDataSource("E:/workspaces/Archon/src/main/webapp/uploadfile/head_img/1724836491.png")));
imgPart.setContentID("pic");
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent("<h1><a href='www.baidu.com'>百度一下</a><img src='cid:pic'/></h1>", "text/html;charset=utf-8");
contentPart.addBodyPart(imgPart);
contentPart.addBodyPart(htmlPart);
content.setContent(contentPart);
emailPart.addBodyPart(attr1);
emailPart.addBodyPart(attr2);
emailPart.addBodyPart(content);
msg.setContent(emailPart);
msg.setFrom(new InternetAddress("[email protected]"));
msg.setRecipients(RecipientType.TO, InternetAddress.parse("[email protected],[email protected]"));
msg.setRecipients(RecipientType.CC, InternetAddress.parse("[email protected],[email protected]"));
Transport.send(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
示例15: enviarMailPoliza
import javax.mail.internet.MimeMultipart; //导入方法依赖的package包/类
public static void enviarMailPoliza(Persona cliente) {
if (cliente.getPersonaMail() != null) {
Mail mail = obtenerMailEmisor();
Properties props = conectarse(mail);
Session session = autentificar(mail, props);
String asunto = "Emisión de Póliza";
String mensaje = "Buenos días " + cliente.toString()
+ ", \r\nSu poliza ya esta emitida, en los proximos días se la estaremos acercando a su domicilio.\r\nSaludos cordiales.\r\n"
+ "Pacinetes S.R.L.";
try {
BodyPart texto = new MimeBodyPart();
// Texto del mensaje
texto.setText(mensaje);
MimeMultipart multiParte = new MimeMultipart();
multiParte.addBodyPart(texto);
MimeMessage message = new MimeMessage(session);
// Se rellena el From
InternetAddress emisor = new InternetAddress(mail.getNombre() + " <" + mail.getMail() + ">");
message.setFrom(emisor);
// Se rellenan los destinatarios
InternetAddress receptor = new InternetAddress();
receptor.setAddress(cliente.getPersonaMail());
message.addRecipient(Message.RecipientType.TO, receptor);
// Se rellena el subject
message.setSubject(asunto);
// Se mete el texto y la foto adjunta.
message.setContent(multiParte);
Transport.send(message);
} catch (MessagingException e) {
messageService.informUser("Poliza creada, falló envío de mail");
}
}
}