Home
Login
Contact
Articles
Newsletter
Bookmark and Share
XSLT
CSS
Javascript
Java
Linux
Username
Password
Annuler
Connexion

Recent Articles

javascript print partial pages

In developping web applications and designing websites, you've probably come accross a situation where you wanted the user to be able to press a print...

Marquee in javascript

The Marquee Element has been deprecated by the W3C and is commonly ill-advised but nevertheless, if you really want to do it, then javascript is the w...

Sending an email in Java

Sending an email in Java is actually quite simple, as always, there is an API that will do most of the work for you and it becomes just a matter of im...

Opacity in Firefox 3.5

If you've upgraded to Firefox 3.5 and you've been using -moz-opacity in your CSS, then you will see that the transparency or opacity (depending on ho...

Installing Tomcat on Linux in a few minutes

Installing tomcat is actually very quick and easy. Assuming you already have the JDK installed, this will only take a few minutes. In my years of exp...

Sending an email in Java

Matt Castonguay, 06 September at 01:12PM 

Sending an email in Java is actually quite simple, as always, there is an API that will do most of the work for you and it becomes just a matter of implementation. You will also require access to an SMTP server in order to send the mail.

First of all, if you don't have the JavaMail API, then download it. Add the jar to your classpath or your lib directory.

Before we do anything, ideally, you should create a new class which handles the method of sending emails for your application. You'll also need your SMTP server information, but for simplicity, we will use 127.0.0.1 in this example.

In order to set the SMTP server, you must add it to Java's System Properties, this is done by simply calling System.getProperties() and adding to the properties the key mail.smtp.host with the value being your SMTP server ip.

Afterwards all there is left to do is building the message. You'll see that on the javax.mail.Message there are quite a few methods which you can explore depending on what you want to do (ie. adding a bcc) but for this example, we'll simply set the sender, the recepient, the subject and the content (supporting html content).

 

public static void send(String subject, String recepient, String sender, String content) throws Exception {

//Set the properties and build the necessary objects.

final Properties props = System.getProperties();
props.put("mail.smtp.host", "127.0.0.1");
final Session session = Session.getDefaultInstance(props);
final Message message = new MimeMessage(session);
//You can change the encoding here.

final String CHARSET = "UTF-8";
message.setSubject(MimeUtility.encodeText(subject, CHARSET, null));
//Build the addresses and set them on the message.

final InternetAddress add = new InternetAddress(sender);
add.setPersonal(MimeUtility.encodeText(sender, CHARSET, null));
message.setFrom(add);
message.setSentDate(new Date());
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recepient));
final MimeMultipart htmlMultipart = new MimeMultipart("related");
//Add the HTML supported content to the email's body.

final BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(content, "text/html");
messageBodyPart.addHeader("charset", CHARSET);
messageBodyPart.addHeader("Content-Transfer-Encoding", "quoted-printable");
//Package the email and send it.

htmlMultipart.addBodyPart(messageBodyPart);
message.setContent(htmlMultipart);
Transport.send(message);
}

 

All you have to do in order to have all the imports is to include javax.mail.* or if you're using an IDE, a fix import will take care of it for you if you've included the JavaMail jar file in your project's library. Feel free to copy and paste the code above and modify it to your needs.

 

Category:   Java

Tags:   javamail, email

Random Tags

wysiwyg    template    firefox    offsettop    memory    library    marquee    clear    xsl:if    function    mode    xml    email    iframe    apply-templates    uniques    javamail    position    caching    offsetleft    tinymce    ant    bash    ie6    offsettop    float    svnant    apache    css classes    custom css    math    svn    apache    offsetleft    opacity    weakhashmap    print    hashmap    xsl:import    double negation   
MG2 Media Inc. - A Web Development Company
Top