java

位置:IT落伍者 >> java >> 浏览文章

JavaMail 发送附件的例子


发布日期:2023年04月08日
 
JavaMail 发送附件的例子

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 );

}

}

               

上一篇:谈谈JAVA的反编译

下一篇:RSA算法的实现——java版