Java Mail 收发邮件

阅读更多

import javax.mail.Address;
import javax.mail.internet.InternetAddress;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import com.sun.mail.smtp.SMTPMessage;
   
public class Send2 
{    
    public Send2()    
    {    
            
    }    
        
    public static void main(String[] args)    
    {    
        send();    
    }    
        
    public static void send()    
    {    
        SimpleEmail email = new SimpleEmail();       
        email.setTLS(true); //是否TLS校验,,某些邮箱需要TLS安全校验,同理有SSL校验  
          
        email.setHostName("smtp.gmail.com");             
        email.setAuthentication("[email protected]", "password");   //用户名和密码    
   
       
       
      
       
        try     
        {    
         System.out.println("开始发送邮件");
          email.addTo("[email protected]"); //接收方  
          email.setFrom("lvjinling");       //发送方
         
    
         
            email.addCc("[email protected]"); //抄送方      
            email.addBcc("[email protected]"); //秘密抄送方  
            email.setSubject("Java Mail Test");         //标题    
           
           email.setMsg("Just a simple send test .");   //内容       
            email.send();     
            System.out.println("邮件发送成功");
                
        } catch (EmailException e) {    
            e.printStackTrace();    
            System.out.println("邮件发送失败");
        }     
    }    

  • activation.jar (55 KB)
  • 下载次数: 4
  • commons-email-1.1.jar (29.9 KB)
  • 下载次数: 2
  • mail.jar (378.6 KB)
  • 下载次数: 6

你可能感兴趣的:(Java,Gmail,Apache,SUN)