JavaMail实现发送邮件实测可用

package com.weixin.action;
import java.io.IOException;  
import java.io.UnsupportedEncodingException;  
import java.util.Date;  
import java.util.Properties;  
import javax.activation.DataHandler;  
import javax.activation.FileDataSource;  
import javax.mail.MessagingException;  
import javax.mail.Session;  
import javax.mail.Transport;  
import javax.mail.Message.RecipientType;  
import javax.mail.internet.AddressException;  
import javax.mail.internet.InternetAddress;  
import javax.mail.internet.MimeBodyPart;  
import javax.mail.internet.MimeMessage;  
import javax.mail.internet.MimeMultipart;  
import javax.mail.internet.MimeUtility; 
public class TestAciton {
    private static Session getSession(String protocol){  
        Properties mailProps=new Properties();  
        mailProps.put("mail.smtp.auth", "true");//向SMTP服务器提交用户认证  
        mailProps.put("mail.transport.protocol", protocol);//指定发送邮件协议  
          
        //getInstance每次都会拿一个新的session,而getDefaultInstance拿的是同一个session  
        Session session=Session.getDefaultInstance(mailProps);  
        //session.setDebug(true);//调试模式  
        return session;  
    }  
     
     
    private static void sendEmail(MimeMessage message,String protocol) throws MessagingException{  
        String host="";//连接发送方的SMTP服务器  
        String user="";//用户名  
        String password="";//密码  
       //从session中取mail.smtp.protocol指定协议的Transport  
        Transport transport=getSession(protocol).getTransport();  
        //建立与指定的SMTP服务器的连接  
        transport.connect(host, user, password);  
       //发给所有指定的收件人,若使用message.getAllRecipients()则还包括抄送和暗送的人  
        transport.sendMessage(message, message.getRecipients(RecipientType.TO));  
        //关闭连接  
        transport.close();  
          
        /** 
         * Transport的send静态方法包括了connect,saveChanges,sendMessage,close等一系列操作, 
         * 但它连接同一个SMTP服务器每发一封邮件给服务器都得重新建立连接和断开连接, 
         * 虽然使用较方便,但开销较大,不值得推荐。 
         */  
       // Transport.send(message, message.getRecipients(RecipientType.TO));  
    }  
      
     
    private static MimeMessage getTextMessage(Session session) throws AddressException,   
           MessagingException, UnsupportedEncodingException{  
         MimeMessage message=new MimeMessage(session);  
         String from="";//发送方邮件地址  
         String to="";//接收方邮件地址  
         String nick = ""; 
         nick=javax.mail.internet.MimeUtility.encodeText(""); //设置发送方昵称
         String subject="晚上开会啦";//邮件主题,注意是中文的  
           
         String content="与晚上八点集合开会请与晚上八点集合开";
         message.setFrom(new InternetAddress(nick+" <"+from+">"));  
         message.setRecipient(RecipientType.TO, new InternetAddress(to));  
         message.setSubject(subject);  
         message.setSentDate(new Date());//发送时间  
        // message.setContent("请与晚上八点集合开会", "text/html;charset=UTF-8");  
         MimeBodyPart attached1BodyPart=getAttachedBodyPart("");//注意附件名是中文的  
         MimeBodyPart contented=new MimeBodyPart();  
         contented.setContent(content,"text/html;charset=gb2312");//因正文内容中有中文  
         MimeMultipart mmp=new MimeMultipart("mixed");//MIME消息头组合类型是mixed(html+附件)  
         mmp.addBodyPart(contented); 
         mmp.addBodyPart(attached1BodyPart);  
          
           
         message.setContent(mmp);  
         message.saveChanges();  
           
         return message;  
       
    }  
      
    /** 
     * 处理文件名 
     * 此处是针对Window下的。 
     * @param filePath 
     * @return 
     */  
    private static String doHandlerFileName(String filePath){  
         String fileName=filePath;  
         if(null !=filePath && !"".equals(filePath)){  
          fileName=filePath.substring(filePath.lastIndexOf("//")+1);  
         }  
         return fileName;  
    }  
      
      
    private static MimeBodyPart getAttachedBodyPart(String filePath) throws MessagingException,  
           UnsupportedEncodingException{  
         MimeBodyPart attached=new MimeBodyPart();  
         FileDataSource fds=new FileDataSource(filePath);  
         attached.setDataHandler(new DataHandler(fds));  
         String fileName=doHandlerFileName(filePath);  
         attached.setFileName(MimeUtility.encodeWord(fileName));//处理附件文件的中文名问题  
         return attached;  
    }  
      
 
      
    public static void main(String[] args) throws Exception {  
        String protocol="smtp";  
        sendEmail(getTextMessage(getSession(protocol)),protocol);  
       
    } 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值