android中使用javamail发送邮件附件

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值