http://Bj83.de

Mail Handle in Java

javamail api: https://java.net/projects/javamail/pages/Home

import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailMgt {
    private String slHost;
	private Integer ilPort;
	private String slUser;
	private String slPass;
	private String slFrom;

	private Transport tTransport;
	private Session sSession;
	private Properties pProp;

	public MailMgt(String sHost, int iPort, String sUser, String sPass, String sFrom){
		slHost = sHost;
		ilPort = iPort;
		slUser = sUser;
		slPass = sPass;
		slFrom = sFrom;
	}

	private void Connect() throws MessagingException{
		pProp = new Properties();
		pProp.put("mail.smtp.auth","true");
		pProp.put("mail.smtp.starttls.enable","true");
		sSession = Session.getInstance(pProp);
		tTransport = sSession.getTransport("smtp");
		tTransport.connect(slHost, ilPort, slUser, slPass);
	}

	private void Disconnect() throws MessagingException{
		tTransport.close();
	}
	public void SendMail(String sTo, String sSubject, String sText) throws MessagingException{
		Connect();
		Address[] aTo = InternetAddress.parse(sTo);
		Message mMessage = new MimeMessage(sSession);
		mMessage.setFrom(new InternetAddress(slFrom));
		mMessage.setReplyTo(aTo);
		mMessage.setSubject(sSubject);
		mMessage.setText(sText);
		mMessage.setSentDate(new Date());
		tTransport.sendMessage(mMessage, aTo);
		Disconnect();
	}

	public void SendAttachment(String sTo, String sSubject, String sText, String sAttachment) throws MessagingException{
		Connect();
		Address[] aTo = InternetAddress.parse(sTo);
		Message mMessage = new MimeMessage(sSession);
		mMessage.setFrom(new InternetAddress(slFrom));
		mMessage.setReplyTo(aTo);
		mMessage.setSubject(sSubject);
	    // create the message part   
	        MimeBodyPart messageBodyPart = new MimeBodyPart();  
	    //fill message  
	        messageBodyPart.setText(sText);  
	        Multipart multipart = new MimeMultipart();  
	        multipart.addBodyPart(messageBodyPart);  
	     // Part two is attachment  
	        messageBodyPart = new MimeBodyPart();  
	        DataSource source = new FileDataSource(sAttachment);  
	        messageBodyPart.setDataHandler(new DataHandler(source));  
	        messageBodyPart.setFileName(sAttachment);  
	        multipart.addBodyPart(messageBodyPart);  
	    // Put parts in message  
	        mMessage.setContent(multipart);
	        mMessage.setSentDate(new Date());
		tTransport.sendMessage(mMessage, aTo);
		Disconnect();	        
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: