how to send mail


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: how to send mail

  1. #1
    Join Date
    Sep 2005
    Posts
    55

    how to send mail

    hi all
    how can i send mail by java useing smtp server (SSL)?
    thax
    Rafeek

  2. #2
    Join Date
    Jul 2007
    Posts
    1
    Try this

    Code:
    public static synchronized void sendMail() {	
    		
    		
    		//		 SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!
    		String to = SelectedEmail;
    
    		String from = "fromAddress";
    		// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
    		String host = "Host IP";
    
    		// Create properties, get Session
    		Properties props = new Properties();
    
    		// If using static Transport.send(),
    		// need to specify which host to send it to
    		props.put("mail.smtp.host", host);
    		// To see what is going on behind the scene
    		props.put("mail.debug", "true");
    		Session session = Session.getInstance(props);
    
    		try {
    			// Instantiatee a message
    			Message msg = new MimeMessage(session);
    			
    			//Set message attributes
    			msg.setFrom(new InternetAddress(from));
    			InternetAddress[] address = { new InternetAddress(to) };
    			msg.setRecipients(Message.RecipientType.TO, address);
    			msg.setSubject("Your Subject");
    			msg.setSentDate(new Date());
    
    			//for attachment
    
    			BodyPart messageBodyPart = new MimeBodyPart();
    			String body = "Body part here";
    			
    			messageBodyPart.setText("any body text here");
    			
    			Multipart multipart = new MimeMultipart();
    			multipart.addBodyPart(messageBodyPart);
    			messageBodyPart = new MimeBodyPart();
    			DataSource source = new FileDataSource(new File(path, fileName));
    			messageBodyPart.setDataHandler(new DataHandler(source));
    			messageBodyPart.setFileName(source.getName());
    			multipart.addBodyPart(messageBodyPart);
    			msg.setContent(multipart);
    			
    			//Send the message
    			
    			Transport.send(msg);
    			
    
    		} catch (MessagingException mex) {
    			// Prints all nested (chained) exceptions as well
    			mex.printStackTrace();
    		}
    
    	}

Similar Threads

  1. Replies: 0
    Last Post: 04-11-2006, 02:26 AM
  2. Need Code to send mail from Excel File
    By vks_online in forum Java
    Replies: 0
    Last Post: 03-20-2006, 11:29 AM
  3. How to write VB code to send mail
    By saradhi in forum Enterprise
    Replies: 13
    Last Post: 12-07-2005, 07:51 AM
  4. Replies: 1
    Last Post: 05-29-2002, 12:04 AM
  5. Send advertise mail question?
    By TQ in forum Enterprise
    Replies: 0
    Last Post: 08-07-2001, 03:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center