Java发用邮件

Java有自己的发送邮件的API,但是这个不在标准的API中,需要下载个javamail-1.4.3.zip,这个可以再sun的官网上找到,或者直接google。

下载后解压有个mail.jar,需要加载为Referenced Libraries

 

具体做法:选择“项目”-->Build Path->"Add external Archieves" 选择你需要加载的jar

 

Java发送邮件的实例:

 

public void sendEmail(String from,String[] recipients,String subject,String content )
 {
     this.from = from;
     this.recipients = recipients;
     this.subjet = subject;
     this.content = content;
     String host = "your email host";
     
     try{
         InternetAddress[] addressTo = new InternetAddress[recipients.length];
         for(int i=0;i< recipients.length;i++)
         {
             addressTo[i] = new InternetAddress(recipients[i]);
             
         }
   
         Properties properties = System.getProperties();
         // Setup mail server
         properties.setProperty("mail.smtp.host", host);
       
         // Get the default Session object.
         Session session = Session.getDefaultInstance(properties);
       
        // Create a default MimeMessage object.
        MimeMessage message = new MimeMessage(session);
       
        // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));
       
        // Set To: header field of the header.
        message.setRecipients(Message.RecipientType.TO,addressTo);
       
        // Set Subject: header field
        message.setSubject(subjet);
       
        // Now set the actual message
        message.setText(content);
        // Send message
        Transport.send(message);
        System.out.println("Sent message successfully....");
     }catch (MessagingException mex) {
        mex.printStackTrace();
     }
     
     
 }

 

其中recipients可以设置多个收件人。

 

http://www.tutorialspoint.com/java/java_sending_email.htm

更多应用可以参考。

 

 

你可能感兴趣的:(java,properties,String,session,api,header)