File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other JSE/JEE APIs and the fly likes sending mail through javamail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "sending mail through javamail" Watch "sending mail through javamail" New topic

sending mail through javamail

vin Hari
Ranch Hand

Joined: Nov 16, 2006
Posts: 161
Hello all,
please tell me how to send e-mail using javamail-api i have mail.jar and activation.jar files i have set them to classpath also and i referred many examples also but no use please tell me how to send and recieve e-mails though javamail-api.
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

Originally posted by vin Hari:
i referred many examples also but no use

As our FAQ says, Tell the Details. What have you tried? What errors/exceptions/problems do you have?

[How To Ask Questions On JavaRanch]
Sirish Kumar Gongal Reddy
Ranch Hand

Joined: Oct 25, 2004
Posts: 109
Please try to send errors/problmes what ever you got it will be helpful to fix the issue ASAP. any way i am sending you working code,

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

* Created by IntelliJ IDEA.
* User: sirishg
* Date: Apr 9, 2007
* Time: 11:58:16 AM
* To change this template use File | Settings | File Templates.
public class SendEmail {

public void postMail(String recipients[], String subject, String message, String from) throws MessagingException {
try {
boolean debug = false;

//Set the host smtp address
Properties props = new Properties();
props.put("", "");
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);

// create a message
Message msg = new MimeMessage(session);

// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);

InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
msg.setRecipients(Message.RecipientType.TO, addressTo);

// Optional : You can also set your custom headers in the Email if you Want
msg.addHeader("MyHeaderName", "myHeaderValue");

// Setting the Subject and Content Type
msg.setContent(message, "text/plain");
} catch (Exception e) {
use above class and pass required parameters.

I agree. Here's the link:
subject: sending mail through javamail
It's not a secret anymore!