26 Mayıs 2013 Pazar

package deneme;

import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 *
 * @author Hasan Çelik
 */
public class EmailGonder {

    String filename = "C:\\WordBelgeler\\Word\\wordDokuman.doc"; //dizin şeklinde 
    //String filename = "src/deneme/wordDokuman.doc";            //proje kaynak dosyası içindeki dizini
            
    public static void main(String[] args) {
        EmailGonder e = new EmailGonder();
        e.Gonder("hsnclk1985@hotmail.com", "hsnclk1985@yahoo.com");
    }

    public void Gonder(String alici, String gonderen) {

        final String username = "hsnclk1985@gmail.com";
        final String password = "password"; //kendi şifrenizle deneyiniz...

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {

                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });
        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(gonderen));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(alici));
            message.setSubject("Bilgilendirme Maili :");
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            
            //Mesaj İçerik
            messageBodyPart.setText("Merhaba");
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            
            //Ek Dosya
            messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(filename);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(source.getName());
            multipart.addBodyPart(messageBodyPart);
            message.setContent(multipart);


            Transport.send(message);

            System.out.println("Mail Gönderildi");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

0 yorum :

Yorum Gönderme