熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Javascript >> 正文

JavaMail 發送附件的例子

2022-06-13   來源: Javascript 

  
  
  Here is the code to send an attachment:
  import javautilProperties;
  import javaxmail*;
  import javaxmailinternet*;
  import javaxactivation*; public class AttachExample {
  public static void main (String args[])
  throws Exception {
  String host = args[];
  String from = args[];
  String to = args[];
  String fileAttachment = args[];
  
  // Get system properties
  Properties props = SystemgetProperties();
  
  // Setup mail server
  propsput(mailsmtphost host);
  
  // Get session
  Session session =
  SessiongetInstance(props null);
  
  // Define message
  MimeMessage message =
  new MimeMessage(session);
  messagesetFrom(
  new InternetAddress(from));
  messageaddRecipient(
  MessageRecipientTypeTO
  new InternetAddress(to));
  messagesetSubject(
  Hello JavaMail Attachment);
  
  // create the message part
  MimeBodyPart messageBodyPart =
  new MimeBodyPart();
  
  //fill message
  messageBodyPartsetText(Hi);
  
  Multipart multipart = new MimeMultipart();
  multipartaddBodyPart(messageBodyPart);
  
  // Part two is attachment
  messageBodyPart = new MimeBodyPart();
  DataSource source =
  new FileDataSource(fileAttachment);
  messageBodyPartsetDataHandler(
  new DataHandler(source));
  messageBodyPartsetFileName(fileAttachment);
  multipartaddBodyPart(messageBodyPart);
  
  // Put parts in message
  messagesetContent(multipart);
  
  // Send the message
  Transportsend( message );
  }
  }
  
  
  

From:http://tw.wingwit.com/Article/program/Java/Javascript/201311/25330.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.