The Java Mail API provides a platform-independent framework to develop mail and messaging applications in Java technology. Java Mail API is also protocol-independent making it more useful. You can use many different protocols to work with mails and messages.
Java mail api is designed to provide functionality such as read, compose and send from within your java programs.
Possible Uses
1. Send Email from any type of java application
2. Composing, reading and sending electronic mail
3. Send Email from java stored procedure.
4. Create a GUI Email client.
5. Dealing with sending and receiving attachments with Email.
6. To search for messages.
How does Email works
Email client Program sends the message to Email server.
Email server contact to the Recipient’s Email server provided in the Email address.
Email server checks that user name is valid or not.
If found valid send email to the address’s email server.
When recipient log on his mail account, gets his Email.
Mailing protocols
Basically, a protocol represents standard method used at each end of a communication channel, in order to properly transmit information.
Generally four protocols are used to send and receive Emails:
1. Simple Mail Transfer Protocol (SMTP)
2. POP (Post Office Protocol 3)
3. IMAP (Internet Message Access Protocol)
4. MAPI (Messaging Application Program Interface)
package com.itech;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendMail {
public static void main(String args[]) throws Exception {
String host = "96.31.77.38";
String from = "karthik.kannan@iopextech.com";
String to = "karthik.kannan@iopextech.com";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set the RFC 822 "From" header field using the
// value of the InternetAddress.getLocalAddress method.
message.setFrom(new InternetAddress(from));
// Add the given addresses to the specified recipient type.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set the "Subject" header field.
message.setSubject("sample test mail..!");
// Sets the given String as this part's content,
// with a MIME type of "text/plain".
message.setText("Hi karthik....../n how are you?");
// Send message
Transport.send(message);
System.out.println("Message Send.....");
}
}
No comments:
Post a Comment