当前位置:编程学习 > JAVA >>

Java 实现简单的邮件发送

  新项目中需要实现将数据采集结果自动发送到指定邮箱的功能,由于Java不是很懂,网上一番搜索和借鉴之后,弄了一个简单的邮件发送功能,当做是java入门学习吧,初步验证,邮件能正常发送。
代码添加后,由于所选的JDK版本(jdk-7u5-windows-i586)安装后,附带JRE库中没有包含mail.jar,程序运行出错。 mail.jar额外下载之后,在eclipse 中: project -->properties-->java Build Path 下点击 Add External JARs...指定存放路径添加即可。
[java] 
import java.util.Date; 
import java.util.Properties; 
import javax.mail.Address; 
import javax.mail.Authenticator; 
import javax.mail.Message; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
 
 
public class mail_test 

    //mail server 
    private String host = "smtp.126.com"; 
    //mail account  
    private String username = "xxxxxxx"; 
    //password 
    private String password = "xxxxxxx"; 
     
    private String mail_head_name = "Head of test mail"; 
 
    private String mail_head_value = "Head of test mail"; 
 
    private String mail_to = www.zzzyk.com; 
 
    private String mail_from = "www.zzzyk.com"; 
 
    private String mail_subject = "subject of test mail"; 
 
    private String mail_body = "content of of test mail"; 
 
    private String personalName = "test_mail!"; 
 
    public mail_test() 
    { 
    } 
 
    
    public void send() throws Exception 
    { 
        try 
        { 
            Properties props = new Properties(); // 获取系统环境 
            Authenticator auth = new Email_Autherticator(); // 认证 
            props.put("mail.smtp.host", host); 
            props.put("mail.smtp.auth", "true"); 
            Session session = Session.getDefaultInstance(props, auth); 
            //设置session,和邮件服务器进行通讯。 
            MimeMessage message = new MimeMessage(session); 
           
            message.setSubject(mail_subject);  
            message.setText(mail_body); // 设置邮件正文 
            message.setHeader(mail_head_name, mail_head_value); // 设置邮件标题 
            message.setSentDate(new Date()); // 设置邮件发送日期 
            Address address = new InternetAddress(mail_from, personalName); 
            message.setFrom(address); // 设置邮件发送者的地址 
            Address toAddress = new InternetAddress(mail_to); // 设置邮件接收方的地址 
            message.addRecipient(Message.RecipientType.TO, toAddress); 
            Transport.send(message); // 发送邮件 
            System.out.println("send over!"); 
        } catch (Exception ex) 
        { 
            ex.printStackTrace(); 
            throw new Exception(ex.getMessage()); 
        } 
    } 
 
    /*
     * 用来进行服务器对用户的认证
     */ 
    public class Email_Autherticator extends Authenticator 
    { 
        public Email_Autherticator() 
        { 
            super(); 
        } 
 
        public Email_Autherticator(String user, String pwd) 
        { 
            super(); 
            username = user; 
            password = pwd; 
        } 
 
        public PasswordAuthentication getPasswordAuthentication() 
        { 
            return new PasswordAuthentication(username, password); 
        } 
    } 
 
    public static void main(String[] args) 
    { 
        mail_test sendmail = new mail_test(); 
        try 
        { 
            sendmail.send(); 
        } catch (Exception ex) 
        { 
        } 
    } 
 

程序编译运行之后,eclipse里console中显示:send over!表明测试邮件已成功发送。
作者:saloon_yuan
补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,