Java Mail


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);

}
}

}

Advertisements
About

I born in Good cultural place called India. My family is traditional family. We fallow all Indian traditions.

Tagged with: , ,
Posted in Java Mail
3 comments on “Java Mail
  1. InduPriya says:

    Very Useful Blog I really Like this and refer this blog it’s nice ….

  2. best It Training institute in Hyderabad
    One of the best It Training institute in Hyderabad. 80% of the students are settled after taking training in this institute. I am one of the person in that list. Dedicated Faculty. NareshIT

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 678 other followers

Calendar
July 2009
M T W T F S S
    Apr »
 12345
6789101112
13141516171819
20212223242526
2728293031  
Mallikarjun Gunda

This is Mallikarjun Gunda, I am working as a software professional. Blogging is my Passion. This blog contains the posts on Java related technologies and programming languages. I am happy to share my knowledge through the blogs.

Blog Stats
  • 276,390 hits
IndiBlogger
malliktalksjava.wordpress.c...
70/100