Results 1 to 3 of 3

Thread: Send Email With Java

  1. #1
    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.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

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

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    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="[email protected]"; 
                 String to = "[email protected]"; 
                 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); 
             } 
              
         } 
     }

Similar Threads

  1. Replies: 4
    Last Post: 23-08-2011, 08:04 AM
  2. Replies: 4
    Last Post: 27-02-2011, 04:32 AM
  3. How to send an automatic email in java?
    By Parvati in forum Software Development
    Replies: 6
    Last Post: 27-07-2010, 02:42 PM
  4. Windows live mail couldn't send email but can receive email
    By danielyen in forum Windows Software
    Replies: 1
    Last Post: 15-07-2010, 12:17 AM
  5. Replies: 2
    Last Post: 14-02-2008, 09:16 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,240,273.82643 seconds with 16 queries