use of javax.mail.BodyPart in project jdk8u_jdk by JetBrains.
the class MailTest method sendMail.
void sendMail() {
try {
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props);
session.setDebug(true);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipients(Message.RecipientType.TO, to);
message.setSubject("this is a multipart test");
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText("please send also this Content\n ciao!");
multipart.addBodyPart(messageBodyPart1);
BodyPart messageBodyPart2 = new MimeBodyPart();
messageBodyPart2.setContent("<b>please</b> send also this Content <br>ciao!", "text/html; charset=UTF-8");
multipart.addBodyPart(messageBodyPart2);
message.setContent(multipart);
/*
Transport tr = session.getTransport("smtp");
tr.connect(host,user, password);
tr.sendMessage(message,InternetAddress.parse(to));
tr.close();
*/
ByteArrayOutputStream baos = new ByteArrayOutputStream();
message.writeTo(baos);
String output = baos.toString();
System.out.println("output = " + output);
if (output.contains("also this Content")) {
System.out.println("Test PASSED.");
} else {
System.out.println("Test FAILED, missing content.");
throw new IllegalStateException("Test FAILED, missing content.");
}
} catch (MessagingException ignored) {
} catch (IOException ignored) {
}
}
use of javax.mail.BodyPart in project nhin-d by DirectProject.
the class NotificationTest method testFailedNotification.
public void testFailedNotification() throws Exception {
Notification noti = new Notification(NotificationType.Failed);
System.out.println(noti);
MimeMultipart mm = noti.getAsMultipart();
assertNotNull(mm);
assertEquals(2, mm.getCount());
BodyPart part = mm.getBodyPart(0);
assertTrue(part.getContentType().startsWith("text/plain"));
assertEquals(Notification.DefaultExplanationFailed, part.getContent().toString());
part = mm.getBodyPart(1);
assertTrue(part.getContentType().startsWith("message/disposition-notification"));
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
part.writeTo(outStream);
String content = new String(outStream.toByteArray());
assertTrue(content.contains("automatic-action/MDN-sent-automatically;failed"));
}
use of javax.mail.BodyPart in project SpringStepByStep by JavaProgrammerLB.
the class SendHTMLEmailWithTemplate method main.
public static void main(String[] args) throws Exception {
Properties props = new Properties();
try {
props.load(new FileInputStream(new File("settings.properties")));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
Session session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "******");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to@gmail.com"));
message.setSubject("Testing Subject");
BodyPart body = new MimeBodyPart();
// freemarker stuff.
Configuration cfg = new Configuration();
Template template = cfg.getTemplate("html-mail-template.ftl");
Map<String, String> rootMap = new HashMap<String, String>();
rootMap.put("to", "liubei");
rootMap.put("body", "Sample html email using freemarker");
rootMap.put("from", "liubei");
Writer out = new StringWriter();
template.process(rootMap, out);
// freemarker stuff ends.
/* you can add html tags in your text to decorate it. */
body.setContent(out.toString(), "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(body);
body = new MimeBodyPart();
String filename = "hello.txt";
DataSource source = new FileDataSource(filename);
body.setDataHandler(new DataHandler(source));
body.setFileName(filename);
multipart.addBodyPart(body);
message.setContent(multipart, "text/html;charset=utf-8");
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println("Done....");
}
use of javax.mail.BodyPart in project SpringStepByStep by JavaProgrammerLB.
the class SendAttachmentInEmail method main.
public static void main(String[] args) {
// Recipient's email ID needs to be mentioned.
String to = "destinationemail@gmail.com";
// Sender's email ID needs to be mentioned
String from = "fromemail@gmail.com";
//change accordingly
final String username = "manishaspatil";
//change accordingly
final String password = "******";
// Assuming you are sending email through relay.jangosmtp.net
String host = "relay.jangosmtp.net";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "25");
// Get the Session object.
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// Create a default MimeMessage object.
Message message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
// Set Subject: header field
message.setSubject("Testing Subject");
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Now set the actual message
messageBodyPart.setText("This is message body");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = "/home/manisha/file.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart);
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
use of javax.mail.BodyPart in project zm-mailbox by Zimbra.
the class Invite method getDescription.
/**
* Returns the meeting notes. Meeting notes is the text/plain part in an
* invite. It typically includes CUA-generated meeting summary as well as
* text entered by the user.
*
* @return null if notes is not found
* @throws ServiceException
*/
public static String getDescription(Part mmInv, String mimeType) throws ServiceException {
if (mmInv == null)
return null;
try {
// If top-level is text/calendar, parse the iCalendar object and return
// the DESCRIPTION of the first VEVENT/VTODO encountered.
String mmCtStr = mmInv.getContentType();
if (mmCtStr != null) {
ContentType mmCt = new ContentType(mmCtStr);
if (mmCt.match(MimeConstants.CT_TEXT_CALENDAR)) {
boolean wantHtml = MimeConstants.CT_TEXT_HTML.equalsIgnoreCase(mimeType);
Object mmInvContent = mmInv.getContent();
InputStream is = null;
try {
String charset = MimeConstants.P_CHARSET_UTF8;
if (mmInvContent instanceof InputStream) {
charset = mmCt.getParameter(MimeConstants.P_CHARSET);
if (charset == null)
charset = MimeConstants.P_CHARSET_UTF8;
is = (InputStream) mmInvContent;
} else if (mmInvContent instanceof String) {
String str = (String) mmInvContent;
charset = MimeConstants.P_CHARSET_UTF8;
is = new ByteArrayInputStream(str.getBytes(charset));
}
if (is != null) {
ZVCalendar iCal = ZCalendarBuilder.build(is, charset);
for (Iterator<ZComponent> compIter = iCal.getComponentIterator(); compIter.hasNext(); ) {
ZComponent component = compIter.next();
ICalTok compTypeTok = component.getTok();
if (compTypeTok == ICalTok.VEVENT || compTypeTok == ICalTok.VTODO) {
if (!wantHtml)
return component.getPropVal(ICalTok.DESCRIPTION, null);
else
return component.getDescriptionHtml();
}
}
}
} finally {
ByteUtil.closeStream(is);
}
}
}
Object mmInvContent = mmInv.getContent();
if (!(mmInvContent instanceof MimeMultipart)) {
if (mmInvContent instanceof InputStream) {
ByteUtil.closeStream((InputStream) mmInvContent);
}
return null;
}
MimeMultipart mm = (MimeMultipart) mmInvContent;
// If top-level is multipart, get description from text/* part.
int numParts = mm.getCount();
String charset = null;
for (int i = 0; i < numParts; i++) {
BodyPart part = mm.getBodyPart(i);
String ctStr = part.getContentType();
try {
ContentType ct = new ContentType(ctStr);
if (ct.match(mimeType)) {
charset = ct.getParameter(MimeConstants.P_CHARSET);
if (charset == null)
charset = MimeConstants.P_CHARSET_DEFAULT;
byte[] descBytes = ByteUtil.getContent(part.getInputStream(), part.getSize());
return new String(descBytes, charset);
}
// If part is a multipart, recurse.
if (ct.getBaseType().matches(MimeConstants.CT_MULTIPART_WILD)) {
String str = getDescription(part, mimeType);
if (str != null) {
return str;
}
}
} catch (javax.mail.internet.ParseException e) {
ZimbraLog.calendar.warn("Invalid Content-Type found: \"" + ctStr + "\"; skipping part", e);
}
}
} catch (IOException e) {
throw ServiceException.FAILURE("Unable to get calendar item notes MIME part", e);
} catch (MessagingException e) {
throw ServiceException.FAILURE("Unable to get calendar item notes MIME part", e);
}
return null;
}
Aggregations