Web邮件发送工具类(直接用*珍藏)

来源:互联网 发布:2017云计算龙头股 编辑:程序博客网 时间:2024/05/17 23:41
 
此程序用于向外部邮箱发送邮件


package mailtest;

/**
 * <p>Title: 此程序用于向外部邮箱发送邮件</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author pqds@sina.com
 * @version 1.0
 */
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class SendMail {
    public SendMail() {
    }

    public static void main(String[] args) throws Exception {
        SendMail send = new SendMail();
        send.sendMail();
    }

    public void sendMail() throws Exception {
        Properties prop = new Properties();
        prop.setProperty("mail.smtp.host", "smtp.163.com");
        prop.setProperty("mail.smtp.auth", "true");
        MyAuthenticator auth = new MyAuthenticator();
        Session session = Session.getDefaultInstance(prop,auth);

        MimeMessage message = new MimeMessage(session);
        Address from = new InternetAddress("hlq@163.com");
        Address to = new InternetAddress("hlq@163.com");
        message.setSubject("Common Send");
        message.setText("just common text");
        message.setFrom(from);
        message.setRecipient(Message.RecipientType.TO,to);
        message.saveChanges();

        Transport trans = session.getTransport("smtp");
        trans.connect("smtp.163.com","hlq_1211","123456");
        trans.send(message);
        trans.close();
        System.out.println("send ok!");
    }

    class MyAuthenticator extends Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("hlq","123456");
        }
    }
}

 

接收邮件

package mailtest;


/**
 * <p>Title: 接受邮件</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author pqds@sina.com
 * @version 1.0
 */
import javax.mail.PasswordAuthentication;
import javax.mail.Authenticator;
import java.util.*;
import javax.mail.*;
import java.io.*;

public class ReceiveMail{
    public ReceiveMail() {
    }

    public static void main(String[] args) throws Exception {
        ReceiveMail receivemail = new ReceiveMail();
        receivemail.receive();
    }

    public void receive() throws Exception {
        Properties prop = new Properties();
        prop.setProperty("mail.pop3.host", "127.0.0.1");
        prop.setProperty("mail.pop3.auth", "true");

        MyAuthenticator auth = new MyAuthenticator();
        Session session = Session.getDefaultInstance(prop, auth);

        Store store = session.getStore("pop3");
        store.connect("127.0.0.1", "pqds@accp.com", "000000");
        Folder defaultFolder = store.getDefaultFolder();
        Folder folder = defaultFolder.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);
        Message[] message = folder.getMessages();
        for (int i = 0; i < message.length; i++) {
            System.out.println(
                    "---------------------------------------------------------------");
            Message msg = message[i];
            System.out.println("邮件标题:" + msg.getSubject());
            System.out.println("邮件征文:" + msg.getContent());
            if (msg.getContent() instanceof Multipart) {
                Multipart mp = (Multipart) msg.getContent();
                for (int t = 0; t < mp.getCount(); t++) {
                    BodyPart part = mp.getBodyPart(t);
                    String fileName = part.getFileName();
                    if(fileName==null)
                    {
                        System.out.println(part.getContent());
                    }else
                    {
                        InputStream in=part.getInputStream();
                        byte[] date = new byte[in.available()];
                        in.read(date);
                        FileOutputStream out = new FileOutputStream("c://"+fileName);
                        out.write(date);
                        System.out.println("文件 "+fileName +"保存在c://");
                    }
                }

            }

        }
    }

    public class MyAuthenticator extends Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("pqds", "000000");
        }
    }

}

 

发送带附件的邮件

package mailtest;


/**
 * <p>Title: 发送带附件的邮件</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author pqds@sina.com
 * @version 1.0
 */

import javax.mail.*;
import java.util.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendMailAddFile {
    public SendMailAddFile() {
    }

    public static void main(String[] args) throws Exception {
        SendMailAddFile send = new SendMailAddFile();
        send.sendFile();
    }

    public void sendFile() throws Exception {
        Properties prop = new Properties();
        prop.setProperty("mail.smtp.host","127.0.0.1");
// prop.setProperty("mail.smtp.auth","true");
        MyAuthenticator authenticator = new MyAuthenticator();
        Session session = Session.getInstance(prop,authenticator);

        MimeMessage message = new MimeMessage(session);
        Address from = new InternetAddress("pqds@accp.com");
        Address to = new InternetAddress("pqds@accp.com");
        message.setFrom(from);
        message.setRecipient(Message.RecipientType.TO,to);
        message.setSubject("addition File");

                Multipart mpart = new MimeMultipart();

        MimeBodyPart body = new MimeBodyPart();
        body.setText("this is addition file");

        mpart.addBodyPart(body);

        body = new MimeBodyPart();
        DataSource ds = new FileDataSource("c://1.jpg");
        DataHandler dh = new DataHandler(ds);
        body.setDataHandler(dh);
        body.setFileName("readme.jpg");
        mpart.addBodyPart(body);

        message.setContent(mpart);

        Transport trans = session.getTransport("smtp");
        trans.connect("127.0.0.1","pqds","000000");
        trans.send(message);
        trans.close();
        System.out.println("send ok!");
    }

    public class MyAuthenticator extends Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("pqds","000000");
        }
    }

}

 

综合例子

package testmail;


import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.Properties;

public class TestMail
{

        public void sendMail_1() throws Exception{

                Properties props=new Properties();

                props.put("mail.transport.protocol","smtp");
        props.put("mail.smtp.host","localhost");
        props.put("mail.smtp.port","25");

            Session session=Session.getInstance(props);


                Message myEmail=new MimeMessage(session);

                myEmail.setFrom(new InternetAddress("hlq@163.com"));

                myEmail.setRecipients(Message.RecipientType.TO,InternetAddress.parse("your@sohu.com"));

                myEmail.setSubject("My Subject 1");

                myEmail.setText("My Message 11111111111111111111111111111");


                Transport.send(myEmail);

                System.out.println("邮件 1 已成功发送");

        }

        public void sendMail_2() throws Exception{

                Properties props=new Properties();

                props.put("mail.smtp.host","localhost");

            Session session=Session.getInstance(props);


                Message myEmail=new MimeMessage(session);

                myEmail.setFrom(new InternetAddress("hlq@163.com"));
                                                          //Message.RecipientType.TO    
                myEmail.setRecipients(Message.RecipientType.TO,InternetAddress.parse("your@sohu.com"));

                //msg.setSentDate(new Date());

                myEmail.setSubject("My Subject 2");

                myEmail.setText("My Message 2222222222222222222222222");


                Transport.send(myEmail);

                System.out.println("邮件 2 已成功发送");

        }

        //带有附件
        public void sendMail_3() throws Exception{

                Properties props=new Properties();

                props.put("mail.smtp.host","localhost");

            Session session=Session.getInstance(props);


                Message myEmail=new MimeMessage(session);

                myEmail.setFrom(new InternetAddress("hlq@163.com"));
                                                          //Message.RecipientType.TO    
                myEmail.setRecipients(Message.RecipientType.TO,InternetAddress.parse("your@sohu.com"));

                myEmail.setSubject("My Subject 3");

                Multipart multipart=new MimeMultipart();


                MimeBodyPart mimeBodyPart=new MimeBodyPart();
                mimeBodyPart.setText("My Message 3333333333333333333333333");


                MimeBodyPart mimeBodyPart2=new MimeBodyPart();
                DataSource fileDataSource=new FileDataSource("C://WINDOWS//winnt.bmp");
                mimeBodyPart2.setDataHandler(new DataHandler(fileDataSource));
                mimeBodyPart2.setFileName("winnt.bmp");


                multipart.addBodyPart(mimeBodyPart);
                multipart.addBodyPart(mimeBodyPart2);


                myEmail.setContent(multipart);

                Transport.send(myEmail);

                System.out.println("邮件 3 已成功发送");

        }

        public void showMail() throws Exception{


                System.out.println(" 邮件列表");
                System.out.println("-------------------------------------------------");
                Properties props=new Properties();


            Session session=Session.getDefaultInstance(props,null);

                Store store=session.getStore("pop3");

                store.connect("localhost","your@sohu.com","a");

                Folder folder=store.getFolder("inbox");

                folder.open(Folder.READ_ONLY);

                Message[] messages=folder.getMessages();

                Message email=null;

                System.out.println(" 主题 来源 发送时间 ");

                for(int i=0;i<messages.length;i++){
                        email=messages[i];
                        System.out.println(" " + String.valueOf(i+1) + " " + email.getSubject() + " " + email.getFrom()[0].toString() + " " + email.getSentDate().toLocaleString());

                }


                System.out.println("-------------------------------------------------");

        }



        public static void main(String[] args) throws Exception{

                TestMail tm=new TestMail();
                tm.sendMail_1();
                tm.sendMail_2();
                tm.sendMail_3();
                tm.showMail();



        }

}

原创粉丝点击