javamial的使用

一、接收邮件

1、创建邮件会话session: Session session = Session.getInstance(System.getProperties());

2、获取相关协议(一般是pop3)的邮件储存访问对象store: Store store = session.getStore("pop3");

3、通过store获取邮件目录对象folder: Folder folder  = store.getFolder("INBOX");

4、通过folder获取邮件messages: Message message[] = folder.getMessages();

二、创建发送邮件

1、创建邮件会话session: Session session = Session.getInstance(new Properties());

2、创建并设置消息对象MimeMessage:

MimeMessage message = new MimeMessage(session);

message.setFrom(new InternetAddress(from));

message.setRecipient(Message.RecipientType.TO,new InternetAddress(to));

message.setSubject(subject);

message.setSentDate(new Date());

3、创建MimeMultipart对象存放BodyPart对象:Multipart mp = new MineMultipart();

4、创建BodyPart存放正文:

BodyPart mdp = new MultiBodyPart();

mdp.setContent(content,"text/html;charset=GBK");

5、把BodyPart添加到Multipart上:mp.addBodyPart(mdp);

6、创建一个BodyPart存放附件:

mdp = new MimeBodyPart();

FileDataSource fds = new FileDataSource("c:/xxx.zip");

dh = new DataHandler(fds);

mdp.setFileName("xxx.zip");

mdp.setDataHandler(dh);

mp.addBodyPart(mdp);

7、把Multipart对象添加到MimeMessage对象中:

message.setContent(mp);

message.save();

8、创建Transport对象:Transport transport = session.getTransport("smtp");

9、连接邮件服务器:transport.connect("smtp.163.com","myName","myPassword");

10、发送邮件:transport.sendMessage(message,message.getAllRecipients());

11、关闭发送对象:transport.close();

你可能感兴趣的:(html,C++,c,C#)