问题描述
- 编码实现gmail中的附件
- android问题:
我的代码已经实现了用gmail发送邮件,现在我需要发送的邮件可以添加附件,没有用户交互。谁能帮忙告诉一下怎么实现啊?
解决方案
直接把代码给你吧
public synchronized void sendMail(String subject String body String sender String recipients File attachment) throws Exception { try{ MimeMessage message = new MimeMessage(session); message.setSender(new InternetAddress(sender)); message.setSubject(subject); MimeBodyPart mbp1 = new MimeBodyPart(); mbp1.setText(body); MimeBodyPart mbp2 = new MimeBodyPart(); FileDataSource fds = new FileDataSource(attachment); mbp2.setDataHandler(new DataHandler(fds)); mbp2.setFileName(fds.getName()); Multipart mp = new MimeMultipart(); mp.addBodyPart(mbp1); mp.addBodyPart(mbp2); message.setContent(mp); if (recipients.indexOf('') > 0) message.setRecipients(Message.RecipientType.TO InternetAddress.parse(recipients)); else message.setRecipient(Message.RecipientType.TO new InternetAddress(recipients)); Transport.send(message); }catch(Exception e){ }}
时间: 2024-09-09 00:38:03