تالار گفتمان nCIS.ir

نسخه‌ی کامل: پیوست کردن فایل به ایمیل
شما در حال مشاهده نسخه آرشیو هستید. برای مشاهده نسخه کامل کلیک کنید.
سلام
میخوام به این کد یه تابع اضافه کنم  که یه فایل هم ارسال کنه  چیکار کنم ممنون
کد کلاس جمیل
package com.tranetech.openspace.sendmail;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


public class GMail {
   private Multipart _multipart = new MimeMultipart();
   final String emailPort = "587";// gmail's smtp port
   final String smtpAuth = "true";
   final String starttls = "true";
   final String emailHost = "smtp.gmail.com";
// final String fromUser = "arpitgameszone34@gmail.com";
// final String fromUserEmailPassword = "Patidar009";

   String fromEmail;
   String fromPassword;
   List<String> toEmailList;
   String emailSubject;
   String emailBody;

   Properties emailProperties;
   Session mailSession;
   MimeMessage emailMessage;

   public GMail() {

   }

   public GMail(String fromEmail, String fromPassword,
                List<String> toEmailList, String emailSubject, String emailBody) {
       this.fromEmail = fromEmail;
       this.fromPassword = fromPassword;
       this.toEmailList = toEmailList;
       this.emailSubject = emailSubject;
       this.emailBody = emailBody;

       emailProperties = System.getProperties();
       emailProperties.put("mail.smtp.port", emailPort);
       emailProperties.put("mail.smtp.auth", smtpAuth);
       emailProperties.put("mail.smtp.starttls.enable", starttls);
       //Log.i("GMail", "Mail server properties set.");
   }

   public MimeMessage createEmailMessage() throws AddressException,
           MessagingException, UnsupportedEncodingException {

       mailSession = Session.getDefaultInstance(emailProperties, null);
       emailMessage = new MimeMessage(mailSession);

       emailMessage.setFrom(new InternetAddress(fromEmail, fromEmail));
       for (String toEmail : toEmailList) {
           //Log.i("GMail", "toEmail: " + toEmail);
           emailMessage.addRecipient(Message.RecipientType.TO,
                   new InternetAddress(toEmail));
       }

       emailMessage.setSubject(emailSubject);
       emailMessage.setContent(emailBody, "text/html");// for a html email
       emailMessage.setText(emailBody);// for a text email
       //Log.i("GMail", "Email Message created.");
       return emailMessage;

   }

   public void sendEmail() throws AddressException, MessagingException {

       Transport transport = mailSession.getTransport("smtp");
       transport.connect(emailHost, fromEmail, fromPassword);
       //Log.i("GMail", "allrecipients: " + emailMessage.getAllRecipients());
       transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
       transport.close();
       //Log.i("GMail", "Email sent successfully.");
   }

  
}

اینم کد اکتیویتی
package com.tranetech.openspace.sendmail;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Arrays;
import java.util.List;

public class SendMailActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button send = (Button) this.findViewById(R.id.button1);

send.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
Log.i("SendMailActivity", "Send Button Clicked.");

String fromEmail = "";
String fromPassword = "";
String toEmails = "";
List<String> toEmailList = Arrays.asList(toEmails
.split("\s*,\s*"));
Log.i("SendMailActivity", "To List: " + toEmailList);
String emailSubject = "";
String emailBody = "";
new SendMailTask(SendMailActivity.this).execute(fromEmail,
fromPassword, toEmailList, emailSubject, emailBody);
}
});
}
}

اینم کد کلاس AsyncTask

package com.tranetech.openspace.sendmail;
import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SendMailTask extends AsyncTask {
private Activity sendMailActivity;
public SendMailTask(Activity activity) {
sendMailActivity = activity;
}
@Override
protected Object doInBackground(Object... args) {
try {

String [] emails = new String [] {"mail@gmail.com", "mail@gmail.com"};
List <String> emailsList = new ArrayList <String>( Arrays.asList(emails));

GMail androidEmail = new GMail("uormail@gmail.com","yourpass", emailsList, "emailSubject","emailBody");
androidEmail.createEmailMessage();
androidEmail.sendEmail();
Log.i("SendMailTask", "Mail Sent.");
} catch (Exception e) {
publishProgress(e.getMessage());
Log.e("SendMailTask", e.getMessage(), e);
}
return null;
}
}

مثلا  میخوام این تابع اضافه کنم درکجا وچیارو تغیر بدم ممنون

public void addAttachment(String filename,String subject) throws Exception { 
    BodyPart messageBodyPart = new MimeBodyPart(); 
   DataSource source = new FileDataSource(filename); 
   messageBodyPart.setDataHandler(new DataHandler(source)); 
   messageBodyPart.setFileName(filename); 
   _multipart.addBodyPart(messageBodyPart);

   BodyPart messageBodyPart2 = new MimeBodyPart(); 
   messageBodyPart2.setText(subject); 

   _multipart.addBodyPart(messageBodyPart2); 
} 



message.setContent(_multipart);
با سلام
استاد خواهشن راهنمای کنیدممنون
هیچی نمیدونم که چیطوری یه فایل هم پیوست کنم به کد ایمیل سندیر
up......