使用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();
- }
-
- }