Android JavaMail发送邮件(可发送附件)


Android JavaMail发送邮件并且可以发送附件。

下载下面的资源,但只能发送普通邮件,不能发送附件。想要Android JavaMail发送邮件(可发送附件)。只要在GMailSender.java文件里加入以下函数即可实现:

public void send_mail_file(String str_title, String str_body,
   String str_from_mail, String str_to_mail, String str_file_path) {
  MimeMessage message = new MimeMessage(session); // Define message
  DataHandler handler = new DataHandler(new ByteArrayDataSource(
    str_body.getBytes(), "text/plain"));
  try {
   message.setFrom(new InternetAddress(str_from_mail)); // Set the from address
   message.addRecipient(Message.RecipientType.TO, new InternetAddress(
     str_to_mail));// Set the to address
   message.setSubject(str_title);// Set the subject
   //message.setText(str_body);// Set the content
   message.setDataHandler(handler);
  } catch (Exception e) {
  }

  MimeBodyPart attachPart = new MimeBodyPart();
  FileDataSource fds = new FileDataSource(str_file_path); // 打开要发送的文件
  try {
   attachPart.setDataHandler(new DataHandler(fds)); 
   attachPart.setFileName(fds.getName());
  } catch (MessagingException e) {
   e.printStackTrace();
  }

  MimeMultipart allMultipart = new MimeMultipart("mixed"); // 附件
  try {
   allMultipart.addBodyPart(attachPart);// 添加 
   message.setContent(allMultipart); 
   message.saveChanges();
   Transport.send(message);// 开始发送
  } catch (MessagingException e) {
   e.printStackTrace();
  }

 }

调用此函数即可实现发送附件。亲测可用。

**************************************************************

下载在帮客之家的1号FTP服务器里,下载地址:

FTP地址:ftp://www.bkjia.com

用户名:www.bkjia.com

密码:www.muu.cc

在 2013年LinuxIDC.com\3月\Android JavaMail发送邮件(可发送附件)

下载方法见 http://www.bkjia.net/thread-1187-1-1.html

**************************************************************

相关内容