جاوا میل

کد و توضیحات

JavaMail

Code and Explanation

Steps to send email using JavaMail API
1. Get the session object that stores all the information of host like host name, username, password etc.
2. Compose the message
3. Send the message

1) Get the session object
The javax.mail.Session class provides two methods to get the object of session, 
Session.getDefaultInstance() method and Session.getInstance() method. 
The  Session.getDefaultInstancemethod creates a new Session the first time it's called, using the Properties that are passed. 
Subsequent calls will return that original Session and ignore any Properties you pass in. 
If you want to create different Sessions with different properties, Session.getDefaultInstance won't do that. 
If some other code in the same JVM (e.g., in the same app server) has already created the default Session with their properties,
you may end up using their Session and your properties will be ignored.
Always use Session.getInstance to avoid this problem.



//Since the testing of the mail is on the PC without connecting to mail servers
String port = "localhost";     
Properties properties = new Properties();
props.put("mail.smtp.host", port);
Session session = Session.getInstance(properties, null);



2) Compose the message The javax.mail.Message class provides methods to compose the message. But it is an abstract class so its subclass javax.mail.internet.MimeMessage class is mostly used. To create the message, you need to pass session object in MimeMessage class constructor. For example: MimeMessage messagen=nnew MimeMessage(session); Now message object has been created but to store information in this object MimeMessage class provides many methods. 1 public void setFrom(Address address) is used to set the from header field. 2 public void addRecipient(Message.RecipientType type, Address address) is used to add the given address to the recipient type. 3 public void addRecipients(Message.RecipientType type, Address[] addresses) is used to add the given addresses to the recipient type. 4 public void setSubject(String subject) is used to set the subject header field. 5 public void setText(String textmessage) is used to set the text as the message content using text/plain MIME type. 6 public void setContent(Object msg, String contentType) is used to set the content as the message content using given MIME type. message.setFrom(new InternetAddress("test@gmail.com")); message.addRecipient(Message.RecipientType.To, new InternetAddress("kool@yahoo.com")); message.setHeader("Hello, JavaMail World"); message.setText("Hello, This mail is for testing only "); 3) Send the message SMTP server To send emails, you must have SMTP server that is responsible to send mails. You can use one of the following techniques to get the SMTP server: Install and use any SMTP server such as Postfix server (for Ubuntu), Apache James server (Java Apache Mail Enterprise Server) etc. I have used MailSlurper a small SMTP mail server that slurps mail into oblivion!. MailSlurper is perfect for individual developers or small teams writing mail-enabled applications that wish to test email functionality without the risk or hassle of installing and configuring a full blown email server. Use the SMTP server provided by the host provider for eg: free SMTP provide by JangoSMTP site is relay.jangosmtp.net (or) Use the SMTP Server provided by companies e.g. gmail, yahoo, etc. The javax.mail.Transport class provides method to send the message . 1 public static void send(Message message) is used to send the message. 2 public static void send(Message message, Address[] address) is used to send the message to the given addresses. Transport.send(message);
Example of sending email in Java import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SendEmail { public static void main(String [] args){ String to = "tom@gmail.com";// String from = "test@gmail.com"; String host = "localhost";//or IP address //Get the session object Properties properties = System.getProperties(); properties.setProperty("mail.smtp.host", host); Session session = Session.getInstance(properties); //compose the message try{ MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); message.setSubject("Test"); message.setText("Hello, this is a testimg email "); // Send message Transport.send(message); System.out.println("message sent successfully...."); }catch (MessagingException mex) {mex.printStackTrace();} } }