码迷,mamicode.com
首页 > 编程语言 > 详细

Java发送邮件

时间:2017-08-21 18:51:58      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:default   type   ops   text   date()   host   div   []   oid   

package javaSocket;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class Email {
    
    /** ?????? */
    private String from;
    /** ?????? */
    private String to;
    /** ???? */
    private String subject;
    /** ???? */
    private String body;
    
    public Email () { 
        
    }
        /** 
         * ???????.
         * @return boolean - ?????? 
         */
        public boolean sendMail() {
            if (getBody() == null || getTo() == null || getFrom() == null
                    || getSubject() == null) { return false; }
            try {
                Properties props = new Properties();
                props.put("username", "15755502569@163.com"); 
                props.put("password", "aa892475"); 
                props.put("mail.transport.protocol", "smtp" );
                props.put("mail.smtp.host", "smtp.163.com");
                props.put("mail.smtp.port", "25" );
    
                Session mailSession = Session.getDefaultInstance(props);
                Message msg = new MimeMessage(mailSession); 
    
                msg.setFrom(new InternetAddress(getFrom()));
                msg.addRecipients(Message.RecipientType.TO, InternetAddress
                        .parse(getTo()));
                msg.setSentDate(new Date());
                msg.setSubject(getSubject()); 
    
                msg.setText(getBody());
                msg.saveChanges();
                System.out.println("?????????????????????");
                Transport transport = mailSession.getTransport("smtp");
                transport.connect(props.getProperty("mail.smtp.host"), props
                        .getProperty("username"), props.getProperty("password")); 
                
                System.out.println("??????????????????");
                transport.sendMessage(msg, msg.getAllRecipients());
                transport.close(); 
                System.out.println("??????????????");
    
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(e);
                return false;
            }
            return true;
        } 
    
        /**
         * @return Returns the body.
         */
        public String getBody() {
            return body;
        } 
    
        /**
         * @param body
         *            The body to set.
         */
        public void setBody(String body) {
            this.body = body;
        } 
    
        /**
         * @return Returns the from.
         */
        public String getFrom() {
            return from;
        } 
    
        /**
         * @param from
         *            The from to set.
         */
        public void setFrom(String from) {
            this.from = from;
        } 
    
        /**
         * @return Returns the subject.
         */
        public String getSubject() {
            return subject;
        } 
    
        /**
         * @param subject
         *            The subject to set.
         */
        public void setSubject(String subject) {
            this.subject = subject;
        } 
    
        /**
         * @return Returns the to.
         */
        public String getTo() {
            return to;
        } 
    
        /**
         * @param to
         *            The to to set.
         */
        public void setTo(String to) {
            this.to = to;
        } 
        
        public static void main(String[] args) {
            Email sender = new Email(); 
            sender.setFrom("15755502569@163.com");
            sender.setTo("18500408772@163.com");
            sender.setSubject("???????");
            sender.setBody("?????????????!"); 
            sender.sendMail();
        }
}

 

Java发送邮件

标签:default   type   ops   text   date()   host   div   []   oid   

原文地址:http://www.cnblogs.com/fangpengchengbupter/p/7405938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!