用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

java 使用FileDataSource类发送带附件的邮件

2015-01-11 作者: java源代码大全举报

[java]代码库

package cn.outofmemory.snippets.enterprise;

import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
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;

public class SendFileViaEmail {

    /*
     * In this example we are using Gmail as our smtp server. This requires that
     * you have a Google account. You may use any smtp server you
     * want by changing the host properties
     */

    public static void main(String[] args) {
        //the path of the file e.g. : "c:/Users/nikos7/Desktop/myFile.txt"
        String file = "<FILE PATH>";

        //destination email address
        String to = "<DESTINATION EMAIL ADDRESS>";

        //source email address
        String from = "<YOUR EMAIL ADDRESS>";

        //Your gmail password
        String password = "<YOUR PASSWORD>"; 

        String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

        //hostname of the machine that has smtp server
        String host = "smtp.gmail.com";

        //either turn on or turns off debugging during sending
        boolean sessioDebug = true;

        String messageText = "Sending a file with FileDataSource\n";
        String messageSubject = "Sending a file using FileDataSource!";

        // To use a smtp host other than Gmail, simply change the following properties
        // according to the specifications of your host

        // set the smtp host properties
        Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.transport.protocol.", "smtp");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.put("mail.smtp.socketFactory.class", SSL_FACTORY);

        Session mailSession = Session.getInstance(props, null);
        mailSession.setDebug(sessioDebug);

        try {
            // create a message
            MimeMessage message = new MimeMessage(mailSession);

            //set message source
            message.setFrom(new InternetAddress(from));

            InternetAddress[] address = {new InternetAddress(to)};

            //set message recipients
            message.setRecipients(Message.RecipientType.TO, address);

            message.setSubject(messageSubject);

            // create and fill the first message part
            MimeBodyPart messageBodyPart1 = new MimeBodyPart();
            messageBodyPart1.setText(messageText);

            // create the second message part
            MimeBodyPart messageBodyPart2 = new MimeBodyPart();

            // attach the file to the message
            FileDataSource fdatasource = new FileDataSource(file);

            messageBodyPart2.setDataHandler(new DataHandler(fdatasource));
            messageBodyPart2.setFileName(fdatasource.getName());

            // create the Multipart and add its parts to it
            Multipart mpart = new MimeMultipart();
            mpart.addBodyPart(messageBodyPart1);
            mpart.addBodyPart(messageBodyPart2);

            // add the Multipart to the message
            message.setContent(mpart);

            // set the Date: header
            message.setSentDate(new Date());

            // send the message
            Transport transport = mailSession.getTransport("smtp");

            transport.connect(host, from, password);
            transport.sendMessage(message, message.getAllRecipients());

        } catch (MessagingException mex) {
            mex.printStackTrace();
            Exception ex = null;
            if ((ex = mex.getNextException()) != null) {
                ex.printStackTrace();
            }
        }

    }
}

//源代码片段来自云代码http://yuncode.net
			


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...