JavaMail操作的总结(3)

作者:袖梨 2022-07-02
//原理:可以将邮件看成有多个部分组成的,如正文是一个部分,附件也是一个部分,所以用BodyPart来设置邮件的格式
message.addRecipient(Message.RecipientType.TO,new InternetAddress(mail_to));
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Pardon Ideas");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("./20020423/test.zip");//此处设置邮件的附件
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("test.zip");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
System.out.println("send ok!");
}
catch(Exception ex)
{
System.out.println("faild"+ex);
}
}
//此段代码用来发送带有附件的邮件
void jButton6_actionPerformed(ActionEvent e) {
try
{
Properties props = System.getProperties();
Authenticator auth = new Email_Autherticator();
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth","true");
Session session = Session.getDefaultInstance(props,auth); //建立session
Store store = session.getStore("pop3");
store.connect(host,username,password);
//After connecting to the Store,you can get a Folder,which must be opened before you can read messages from it:
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
Message message[] = folder.getMessages();
for (int i=0,n=message.length;i {
//获得邮件的部分信息,如头部信息,送信人的署名,送信人的邮件地址
String out_from_person = ((InternetAddress)message[i].getFrom()[0]).getPersonal();
String out_from_address = ((InternetAddress)message[i].getFrom()[0]).getAddress();
System.out.println("From:"+out_from_person+" ");

相关文章

精彩推荐