java发送邮件

import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail {
    /**
     *

      

String uto="xxxx@qq.com";//获取收件人邮箱
String topic="xxxx";//发送主题
String content="xxxx";//发送内容
String sendemail="xxxx@163.com";
String sendname="xxxxx";
String connecthost="smtp.163.com";
String connectemailname="username";//用户名
String connectemailpass="password";//密码 或者 授权码


     * @param uto 获取收件人邮箱
     * @param topic 发送主题
     * @param content 发送内容
     * @param sendemail 发送人的邮箱 
     * @param sendname 发送人姓名   
     * @param connecthost 邮箱主机 
     * @param connectemailname 邮箱用户名  
     * @param connectemailpass  邮箱发送密钥(不是登录密码)
     * @return boolean 
     */
    
    public static boolean sendemail(String uto,String topic,String content,
            String sendemail,String sendname,String connecthost,
            String connectemailname,String connectemailpass,Integer port)
    
    {
        
        
        //验证邮箱格式开始
        boolean flag=true;
        
        String str="^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern regex=Pattern.compile(str);
        Matcher matcher=regex.matcher(uto);
        flag=matcher.matches();
        
        //邮箱验证
        
        try {
        if(flag){
        //设置协议
        Properties pros = new Properties();
        pros.put("mail.transport.protocol","smtp");
        pros.put("mail.smtp.auth", true);
        pros.put("mail.smtp.port", port);  //   qq 
        pros.setProperty("mail.debug", "false");
        //Authenticator authenticator=new Authenticator();
        Session session=Session.getInstance(pros);
        session.setDebug(true);
        
        
        //设置发送的邮件
        
        MimeMessage msg = new MimeMessage(session);
        //设置发送人的邮箱和姓名 
        Address toaddress=new InternetAddress(sendemail,sendname);
        msg.setFrom(toaddress);
        
    //    
    /*        
        Address[] cc={new InternetAddress(""),new InternetAddress("")};
        msg.setRecipients(Message.RecipientType.CC, cc);
        */
            msg.setRecipients(Message.RecipientType.TO, uto);//设置收件人
            msg.setSubject(topic);//主题
            msg.setText(content);//内容
            msg.setContent(content,"text/html;charset=utf-8");//发送html格式的邮件
            msg.saveChanges();//保存发送
            
            //取得发送工具
            Transport ts=session.getTransport();
            //设置连接 邮箱主机  邮箱用户名 邮箱密码
            ts.connect(connecthost,connectemailname,connectemailpass);
            ts.sendMessage(msg, msg.getAllRecipients());//发送
            ts.close();//关闭
            
            return true;
        }else{
            return false;
        }
        }catch(Exception e){
            return false;
        }
    }
    
}

 依赖

<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.6</version>
</dependency>

posted @ 2017-10-27 11:24  冬天不眠  阅读(181)  评论(0编辑  收藏  举报