Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 24-03-2009
Member
 
Join Date: Jan 2009
Posts: 67
Send Email With Java

I am having a small query regarding the java programming language.
I just wanted to know the code that which would help me in sending the email to users periodically such as every month end of the day.

Please let me know the ways that can be apllied using java.
Reply With Quote
  #2  
Old 24-03-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: Send Email With Java

Code:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*; 

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

     //Set the host smtp address
     Properties props = new Properties();
     props.put("mail.smtp.host", "smtp.jcom.net");

    // create some properties and get the default Session
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(debug);

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

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

    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.setSubject(subject);
    msg.setContent(message, "text/plain");
    Transport.send(msg);
}
Reply With Quote
  #3  
Old 24-03-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
Re: Send Email With Java

Code:
import javax.mail.*; 
 import javax.mail.internet.*; 
 import javax.activation.*; 
 import java.io.*; 
 import java.util.Properties; 
 public class MailClient 
 { 
       public void sendMail(String mailServer, String from, String to, 
                             String subject, String messageBody, 
                             String[] attachments) throws 
MessagingException, AddressException 
     { 
         // Setup mail server 
         Properties props = System.getProperties(); 
         props.put("mail.smtp.host", mailServer); 
          
         // Get a mail session 
         Session session = Session.getDefaultInstance(props, null); 
          
         // Define a new mail message 
         Message message = new MimeMessage(session); 
         message.setFrom(new InternetAddress(from)); 
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
         message.setSubject(subject); 
          
         // Create a message part to represent the body text 
         BodyPart messageBodyPart = new MimeBodyPart(); 
         messageBodyPart.setText(messageBody); 
         //use a MimeMultipart as we need to handle the file attachments 
         Multipart multipart = new MimeMultipart(); 
          //add the message body to the mime message 
         multipart.addBodyPart(messageBodyPart); 
         // add any file attachments to the message 
         addAtachments(attachments, multipart); 
          // Put all message parts in the message 
         message.setContent(multipart); 
         // Send the message 
         Transport.send(message); 
       } 
       protected void addAtachments(String[] attachments, Multipart multipart) 
                     throws MessagingException, AddressException 
     { 
         for(int i = 0; i<= attachments.length -1; i++) 
         { 
             String filename = attachments[i]; 
             MimeBodyPart attachmentBodyPart = new MimeBodyPart(); 
             //use a JAF FileDataSource as it does MIME type detection 
             DataSource source = new FileDataSource(filename); 
             attachmentBodyPart.setDataHandler(new DataHandler(source)); 
             //assume that the filename you want to send is the same as the 
             //actual file name - could alter this to remove the file path 
             attachmentBodyPart.setFileName(filename); 
              
             //add the attachment 
             multipart.addBodyPart(attachmentBodyPart); 
         } 
     } 
  
     public static void main(String[] args) 
     { 
         try 
         { 
             MailClient client = new MailClient(); 
             String server="pop3.mydomain.com"; 
             String from="myname@mydomain.com"; 
             String to = "someuser@somewhere.com"; 
             String subject="Test"; 
             String message="Testing"; 
             String[] filenames = 
{"c:\somefile.txt"}; 
          client.sendMail(server,from,to,subject,message,filenames); 
         } 
         catch(Exception e) 
         { 
             e.printStackTrace(System.out); 
         } 
          
     } 
 }
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Send Email With Java"
Thread Thread Starter Forum Replies Last Post
Unable to send email with jpeg's/mp3's attachment to a Verizon phone using Opera's email client Tawannah Technology & Internet 4 23-08-2011 08:04 AM
"send page by email" and "send link by email" disabled in Internet Explorer 8 Bindaas-ER Technology & Internet 4 27-02-2011 04:32 AM
How to send an automatic email in java? Parvati Software Development 6 27-07-2010 02:42 PM
Windows live mail couldn't send email but can receive email danielyen Windows Software 1 15-07-2010 12:17 AM
Cannot send internal email to a external email host like hotmail, gmail, yahoo, gmx etc... Antonio00 Small Business Server 2 14-02-2008 09:16 PM


All times are GMT +5.5. The time now is 01:43 AM.