JAVA MAIL

 
JAVA MAIL
需要JAVAMAIL包
MyAuthenticator论证类

public class MyAuthenticator extends Authenticator {  
    protected PasswordAuthentication getPasswordAuthentication(){
           return new PasswordAuthentication("weasel100@163.com", "password");
    }
}


SendMessage发送简单的单个邮件

public class SendMessage {
    public static void main(String[] args) {
           Properties props = new Properties();
           props.put("mail.smtp.host", "smtp.163.com");           
           props.put("mail.smtp.auth", "true");            
           props.put("mail.smtp.user", "weasel100");
           props.put("mail.smtp.from", "weasel100@163.com");       
           props.put("mail.debug", "true");          
           MyAuthenticator auth = new MyAuthenticator();       
           Session session = Session.getInstance(props, auth);
           session.setDebug(true);        
           Message msg = new MimeMessage(session);
           try {
                  msg.setFrom(new InternetAddress("weasel100.163.com"));                   
            InternetAddress[] address = {
new InternetAddress("yrzu@sina.com"), new InternetAddress("weasel100@163.com")
};
               msg.setRecipients(Message.RecipientType.TO, address);                
                  //msg.setRecipient(Message.RecipientType.TO, new InternetAddress("weasel100@163.com"));
                  msg.setSentDate(new Date());
                  msg.setSubject("Java API Test!!!!!!!!!!!!!!!!");                
                  msg.setText("Dear Mr. Wang /n" +
                                "     Hello World!/n" +
                                "Best" +
                                "Li Si 2006.7.19");               
                  Transport.send(msg);                 
           } catch (MessagingException mex) {                
                  //System.out.println("/n--Exception handling in msgsendsample.java");
               mex.printStackTrace();            
               /*System.out.println();
               Exception ex = mex;
               do {
                  if (ex instanceof SendFailedException) {
                      SendFailedException sfex = (SendFailedException)ex;
                      Address[] invalid = sfex.getInvalidAddresses();
                      if (invalid != null) {
                         System.out.println("    ** Invalid Addresses");
                         if (invalid != null) {
                             for (int i = 0; i < invalid.length; i++)
                                System.out.println("         " + invalid[i]);
                         }
                      }
                      Address[] validUnsent = sfex.getValidUnsentAddresses();
                      if (validUnsent != null) {
                         System.out.println("    ** ValidUnsent Addresses");
                         if (validUnsent != null) {
                             for (int i = 0; i < validUnsent.length; i++)
                                System.out.println("         "+validUnsent[i]);
                         }
                      }
                      Address[] validSent = sfex.getValidSentAddresses();
                      if (validSent != null) {
                         System.out.println("    ** ValidSent Addresses");
                         if (validSent != null) {
                             for (int i = 0; i < validSent.length; i++)
                                System.out.println("         "+validSent[i]);
                         }
                      }
                  }
                  System.out.println();
                  if (ex instanceof MessagingException)
                      ex = ((MessagingException)ex).getNextException();
                  else
                      ex = null;
               } while (ex != null);*/
           }
           System.out.println("End.");
    }
}
SendFiles发送我个地址,及附件功能
public class SendFiles {
    public static void main(String[] args) {
           Properties props = new Properties();
           props.put("mail.smtp.host", "smtp.163.com");           
           props.put("mail.smtp.auth", "true");            
           props.put("mail.smtp.user", "weasel100");
           props.put("mail.smtp.from", "weasel100@163.com");       
           //props.put("mail.debug", "true");        
           MyAuthenticator auth = new MyAuthenticator();       
           Session session = Session.getInstance(props, auth);
           //session.setDebug(true);             
           Message msg = new MimeMessage(session);
           try {
                  msg.setFrom(new InternetAddress("weasel100@163.com"));                 
                  msg.setRecipient(Message.RecipientType.TO, new InternetAddress("weasel100@163.com"));
                  msg.setSentDate(new Date());
                  msg.setSubject("Java API Test!!!!!!!!!!!!!!!!");                
                  MimeBodyPart mbp1 = new MimeBodyPart();
               mbp1.setText("Dear Mr. Wang /n" +
                                "     Hello World!/n" +
                                "Best" +
                                "Li Si 2006.7.19");
               // create the second message part
               MimeBodyPart mbp2 = new MimeBodyPart();
               FileDataSource fds = new FileDataSource("F:/J2EE_yu/POM.jar");
               mbp2.setDataHandler(new DataHandler(fds));
               mbp2.setFileName(fds.getName());                
               MimeBodyPart mbp3 = new MimeBodyPart();
               FileDataSource fds2 = new FileDataSource("F:/J2EE_yu/sqlnet.log");
               mbp3.setDataHandler(new DataHandler(fds2));
               mbp3.setFileName(fds2.getName());              
               Multipart mp = new MimeMultipart();
               mp.addBodyPart(mbp1);
               mp.addBodyPart(mbp2);
               mp.addBodyPart(mbp3);                 
               msg.setContent(mp);                                       
                  Transport.send(msg);                 
           } catch (MessagingException mex) {  
                  mex.printStackTrace();
           }
    System.out.println("End.");
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值