2013-10-11 29 views
1

我很好奇,有沒有什麼辦法可以直接從Java代碼發送帶有附件的郵件。該電子郵件屬於公司mylogin @ companyxxx.com`即。使用Java操縱Outlook?或者,也許我不需要操縱Outlook,並且足以擁有登錄名和密碼以及其他人員...如何直接從Java代碼發送私人公司的電子郵件與attchments?

+0

如果你不大驚小怪使用Outlook,您可以使用JavaMail,[示例](http://stackoverflow.com/questions/16117365/sending-mail-attachment-using-java)和[示例](http://stackoverflow.com/questions/3177616/how-to-附加多個文件到電子郵件使用javamail)和[示例](http://stackoverflow.com/questions/8973026/java-mail-attachments-inline-images) – MadProgrammer

+0

謝謝你...唯一的問題是這是一種機密的電子郵件系統oi不知道如何達到它,除了使用Outlook ...這些例子主要針對普通的gmail等 - - - 公共郵箱 –

+0

郵件仍然需要發送到smtp或imap服務器進行分發,所以即使它是一個內部郵件服務器,過程將是相同的...不知道有關交換,雖然... – MadProgrammer

回答

1

是的,您可以使用javamail來做到這一點,您可以從here下載jar。對於後市,你只需要設置屬性如下:

Properties props = new Properties(); 
    props.put("mail.smtp.user", username); 
    props.put("mail.smtp.host", "smtp.live.com"); 
    props.put("mail.smtp.port", "25"); 
    props.put("mail.debug", "true"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.EnableSSL.enable", "true"); 
    props.setProperty("mail.smtp.port", "587"); 
    props.setProperty("mail.smtp.socketFactory.port", "587"); 

或者,如果你想比較完整的例子,使用下面的方法:

public int sendMailWithAttachment(String to, String subject, String body, String filepath, String sendFileName) { 
    final String username = "YOUR EMAIL"; 
    final String password = "YOUR PWD"; 
    Properties props = new Properties(); 
    props.put("mail.smtp.user", username); 
    props.put("mail.smtp.host", "smtp.live.com"); 
    props.put("mail.smtp.port", "25"); 
    props.put("mail.debug", "true"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.EnableSSL.enable", "true"); 
    props.setProperty("mail.smtp.port", "587"); 
    props.setProperty("mail.smtp.socketFactory.port", "587"); 
    Session session = Session.getInstance(props, 
      new javax.mail.Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(username, password); 
     } 
    }); 
    try { 
     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress(username)); 
     message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); 
     message.setSubject(subject); 
     message.setText(body); 
     BodyPart messageBodyPart = new MimeBodyPart(); 
     Multipart multipart = new MimeMultipart(); 
     BodyPart htmlPart = new MimeBodyPart(); 
     htmlPart.setContent("<html><body>HELLO</body></html>", "text/html"); 
     DataSource source = new FileDataSource(filepath); 
     messageBodyPart.setDataHandler(new DataHandler(source)); 
     messageBodyPart.setFileName(sendFileName); 
     multipart.addBodyPart(messageBodyPart); 
     multipart.addBodyPart(htmlPart); 
     message.setContent(multipart); 
     Transport.send(message); 
     return 1; 
    } catch (Exception e) { 
     return 0; 
    } 
}