Archive

Posts Tagged ‘Java Mail send receive’

Java Mail

July 9, 2009 1 comment

SEND MAIL EXAMPLE

import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class TestMail {
public static void main(String args[]) throws Exception {
String host = “localhost”;
String from = “nani@gmail.com”;
String to = “nani@gmail.com”;
String user = “mallik”;
String password = “mallik”;
String content = “Change your password.”;
Properties properties = System.getProperties();
properties.setProperty(“mail.smtp.host”, host);
Session session =Session.getDefaultInstance(properties);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(“Test Mail”);
message.setText(content);

Transport.send(message);
System.out.println(“Message Send Successfully…” + content);

}
}

READING MAIL EXAMPLE

import java.io.*;
import java.util.*;
import javax.mail.*;

public class ReadMail {
public static void main(String args[]) throws Exception {
String host = “localhost”;
String user = “nani”;
String password = “nani”;
Properties properties = System.getProperties();
Session session = Session.getDefaultInstance (properties);
Store store = session.getStore(“pop3″);
store.connect(host, user, password);
Folder folder = store.getFolder(“inbox”);
folder.open(Folder.READ_ONLY);

Message[] message = folder.getMessages();

for (int i = 0; i < message.length; i++) {
System.out.println(“—– Message ” + (i + 1) + ” ————“);
System.out.println(“SentDate : ” + message[i]. getSentDate());
System.out.println(“From : ” + message[i]. getFrom()[0]);
System.out.println(“Subject : ” + message[i]. getSubject());
System.out.print(“Message : “);
InputStream stream = message[i]. getInputStream();
while (stream.available() != 0) {
System.out.print((char) stream.read());
}
}
folder.close(true);
store.close();
}
}

SENDIG MAIL WITH AN ATTACHMENT

import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class AttachExample {
public static void main (String args[]) throws Exception {
String host = “localhost”;
String from = “nani@gmail.com”;
String to[] = new String[]{“nani@gmail.com”,”gopikanthk”};
String filename = “testAttach.txt”;
// Get system properties
Properties props = System.getProperties();
props.put(“mail.smtp.host”, host);
Session session = Session.getInstance(props, null);
System.out.println(session.getProperties());
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));

InternetAddress[] toAddress = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++){
toAddress[i] = new InternetAddress(to[i]);
System.out.println(“to address are——–“+toAddress[i]);
}
message.setRecipients(Message.RecipientType.TO, toAddress);
String a=null;
System.out.println(“recipent type to—-“);
message.setSubject(“Hello JavaMail Attachment”);
System.out.println(“After the subject———“);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(“Here’s the file”);
System.out.println(“After the content of the message—-“);
Multipart multipart = new MimeMultipart();

multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();

DataSource source = new FileDataSource(filename);

messageBodyPart.setDataHandler(new DataHandler(source));
System.out.println(“After the data handler—-“);
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
try{
System.out.println(“In try block——-“);
Transport.send(message);
System.out.println(“messages sent successfully………”);
}
catch(SendFailedException sfe)
{
message.setRecipients(Message.RecipientType.TO, sfe.getValidUnsentAddresses());
Transport.send(message);

}
}

}

About these ads
Follow

Get every new post delivered to your Inbox.

Join 315 other followers