dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: Java
Expertise: Intermediate
Nov 13, 2002

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Sending HTML and ASCII Formatted E-mails


To do this, you need the javax.mail package. It's a part of J2EE and available from java.sun.com.

Sending a simple e-mail is pretty easy:
 
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class Mail {

static public void main(String[] args) {
String to = "your@email.com";
String subj = "Testing Email";
String body = "This is a test of the
good times broadcasting system";

Properties props = new Properties();
// supply the location of a
props.put
("mail.smtp.host", "10.10.10.119");

try {
Session session = Session.getDefaultInstance
(props, null);
Message msg = new MimeMessage
(session);
session.setDebug(true);

msg.setFrom(new InternetAddress
("ramneekh@programmer.net"));
msg.addRecipient( Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subj);

msg.setType("plain/text");

msg.setContent(mp);

Transport.send(msg);
} catch(Throwable t) {
t.printStackTrace();
}
}

}

To send multiple formats in a single e-mail, use Parts. An e-mail has a Content; that Content can be a simple String or it can be a Multipart. A Multipart is made up of many BodyParts. A BodyPart may contain a String or a Multipart.

This example sends e-mails that contain Multipart, with two BodyParts--plaintext and HTML.

Note that there are different types of Multipart. The two most common are multipart/mixed and multipart/alternative. One shows both on the same page, while the other selects the best format for the user. Here's the example code:
 
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class Mail {

static public void main(String[] args) {
String to = "ramneekh@programmer.net";
String subj = "Testing";
String body =
"This is a test of the good times broadcasting system";
String htmlbody =
"<h2>This is a test of the good times broadcasting system</h2>";

Properties props = new Properties();
// supply the location of a
props.put("mail.smtp.host", "10.10.10.119");

try {
Session session = Session.getDefaultInstance
(props, null);
Message msg = new MimeMessage(session);
session.setDebug(true);

msg.setFrom(new InternetAddress
("laptop@generationjava.com"));
msg.addRecipient( Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subj);

MimeMultipart mp = new MimeMultipart();
BodyPart tp = new MimeBodyPart();
tp.setText(body);
mp.addBodyPart(tp);

tp = new MimeBodyPart();
tp.setContent(htmlbody, "text/html");
mp.addBodyPart(tp);

mp.setSubType("alternative");

msg.setContent(mp);

Transport.send(msg);
} catch(Throwable t) {
t.printStackTrace();
}
}

}
Ramneek Handa
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date