使用javamail必须先下载3个JAR包并导入工程 activation.jar additonnal.jar mail.jar 导入方法为: project->properties->java build path->libraries->add external jars 然后在android项目中添加网络访问权限 <uses-permission android:name="android.permission.INTERNET"></uses-permission> 最后在程序中加载如下包 import android.app.Activity; import android.os.Bundle; import android.util.Log; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.*; import javax.mail.PasswordAuthentication; 调用函数代码如下 class MyAuthenticator extends javax.mail.Authenticator { private String strUser; private String strPwd; public MyAuthenticator(String user, String password) { this.strUser = user; this.strPwd = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(strUser, strPwd); } } public void send_mail_file(String str_to_mail,String str_from_mail,String str_smtp,String str_user,String str_pass,String str_title,String str_body,String str_file_path) { Log.v("lengfeng","send_mail_file"); String host = str_smtp; //发件人使用发邮件的电子信箱服务器 String from = str_from_mail; //发邮件的出发地(发件人的信箱) String to = str_to_mail; //发邮件的目的地(收件人信箱) Log.v("lengfeng",str_smtp); Log.v("lengfeng",str_from_mail); Log.v("lengfeng",str_to_mail); Properties props = System.getProperties();// Get system properties props.put("mail.smtp.host", host);// Setup mail server props.put("mail.smtp.auth", "true"); //这样才能通过验证 MyAuthenticator myauth = new MyAuthenticator(str_user, str_pass);// Get session Session session = Session.getDefaultInstance(props, myauth); MimeMessage message = new MimeMessage(session); // Define message try { message.setFrom(new InternetAddress(from)); // Set the from address } catch (AddressException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } try { message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));// Set the to address } catch (AddressException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } try { message.setSubject(str_title);// Set the subject } catch (MessagingException e) { e.printStackTrace(); } try { message.setText(str_body);// Set the content } catch (MessagingException e) { e.printStackTrace(); } MimeBodyPart attachPart = new MimeBodyPart(); FileDataSource fds = new FileDataSource(str_file_path); //打开要发送的文件 try { attachPart.setDataHandler(new DataHandler(fds)); } catch (MessagingException e) { e.printStackTrace(); } try { attachPart.setFileName(fds.getName()); } catch (MessagingException e) { e.printStackTrace(); } MimeMultipart allMultipart = new MimeMultipart("mixed"); //附件 try { allMultipart.addBodyPart(attachPart);//添加 } catch (MessagingException e) { e.printStackTrace(); } try { message.setContent(allMultipart); } catch (MessagingException e) { e.printStackTrace(); } try { message.saveChanges(); } catch (MessagingException e) { e.printStackTrace(); } try { Transport.send(message);//开始发送 } catch (MessagingException e) { e.printStackTrace(); } }
android中使用javamail发送邮件附件
最新推荐文章于 2022-01-20 16:22:55 发布