2010-03-11 1 views

답변

4

사용 JavaMail API.

예제 코드 : 당신의 개찰구 메일 양식에서

import javax.mail.*; 
import javax.mail.internet.*; 
import javax.activation.*; 
import java.io.*; 
import java.util.Properties; 
public class MailClient 
{ 


    public void sendMail(String mailServer, String from, String to, 
          String subject, String messageBody, 
          String[] attachments) throws 
MessagingException, AddressException 
    { 
     // Setup mail server 
     Properties props = System.getProperties(); 
     props.put("mail.smtp.host", mailServer); 

     // Get a mail session 
     Session session = Session.getDefaultInstance(props, null); 

     // Define a new mail message 
     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress(from)); 
     message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
     message.setSubject(subject); 

     // Create a message part to represent the body text 
     BodyPart messageBodyPart = new MimeBodyPart(); 
     messageBodyPart.setText(messageBody); 

     //use a MimeMultipart as we need to handle the file attachments 
     Multipart multipart = new MimeMultipart(); 

     //add the message body to the mime message 
     multipart.addBodyPart(messageBodyPart); 

     // add any file attachments to the message 
     addAtachments(attachments, multipart); 

     // Put all message parts in the message 
     message.setContent(multipart); 

     // Send the message 
     Transport.send(message); 


    } 

    protected void addAtachments(String[] attachments, Multipart multipart) 
        throws MessagingException, AddressException 
    { 
     for(int i = 0; i<= attachments.length -1; i++) 
     { 
      String filename = attachments[i]; 
      MimeBodyPart attachmentBodyPart = new MimeBodyPart(); 

      //use a JAF FileDataSource as it does MIME type detection 
      DataSource source = new FileDataSource(filename); 
      attachmentBodyPart.setDataHandler(new DataHandler(source)); 

      //assume that the filename you want to send is the same as the 
      //actual file name - could alter this to remove the file path 
      attachmentBodyPart.setFileName(filename); 

      //add the attachment 
      multipart.addBodyPart(attachmentBodyPart); 
     } 
    } 

    public static void main(String[] args) 
    { 
     try 
     { 
      MailClient client = new MailClient(); 
      String server="pop3.mydomain.com"; 
      String from="[email protected]"; 
      String to = "[email protected]"; 
      String subject="Test"; 
      String message="Testing"; 
      String[] filenames = 
{"c:\somefile.txt"}; 

      client.sendMail(server,from,to,subject,message,filenames); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(System.out); 
     } 

    } 
} 
+1

... 그리고 센드 메일 전화() 메소드를 제출합니다. 위젯은 POJO와 매우 비슷하기 때문에 전자 메일을 객체로 캡슐화하는 것이 더 나을 것입니다. 그런 다음 필드를 직접 개찰 코드의 양식 구성 요소에 바인딩 할 수 있습니다. – ireddick