アプリケーションでアセットから電子メールにPDFファイルを添付するにはどうすればよいですか?次のコードを使用して画像を添付していますが、PDFを添付する方法がわかりません。
EMail.javaファイル
パッケージcom.drc.email; import android.app.Activity; import android.content.Intent; import android.database.Cursor; android.net.Uriをインポートします。 android.os.Bundleをインポートします。 android.provider.MediaStoreをインポートします。 import android.util.Log; android.view.Viewをインポートします。 import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; android.widget.Toastをインポートします。 パブリッククラスEメールはアクティビティを拡張します{ ボタン送信、接続; EditText userid、password、from、to、subject、body; private static final int SELECT_PICTURE = 1; プライベート文字列selectedImagePath=null; /**アクティビティが最初に作成されたときに呼び出されます。* / @オーバーライド public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); send =(ボタン)this.findViewById(R.id.btnsend); attach =(ボタン)this.findViewById(R.id.btnattach); userid =(EditText)this.findViewById(R.id.userid); password =(EditText)this.findViewById(R.id.password); from =(EditText)this.findViewById(R.id.from); to =(EditText)this.findViewById(R.id.to); 件名=(EditText)this.findViewById(R.id.subject); body =(EditText)this.findViewById(R.id.body); attach.setOnClickListener(new OnClickListener(){ @オーバーライド public void onClick(View v){ //TODO自動生成されたメソッドスタブ //ファイルを選択します selectedImagePath = null; インテントインテント=newIntent(); intent.setType( "image / *"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent、 "画像の選択")、SELECT_PICTURE); } }); send.setOnClickListener(new View.OnClickListener(){ public void onClick(View view){ MailSender sender = new MailSender(userid.getText()。toString()、password.getText()。toString()); 試す { if(selectedImagePath == null) {{ sender.sendMail(subject.getText()。toString()、body.getText()。toString()、from.getText()。toString()、to.getText()。toString()); Toast.makeText(getBaseContext()、 "Send Mail Sucess"、Toast.LENGTH_LONG).show(); } そうしないと {{ sender.sendMailAttach(subject.getText()。toString()、body.getText()。toString()、from.getText()。toString()、to.getText()。toString()、selectedImagePath.toString()、 String.format( "image%d.jpeg"、System.currentTimeMillis())); Toast.makeText(getBaseContext()、 "Send Attach Mail Sucess"、Toast.LENGTH_LONG).show(); } } catch(例外e){ Log.e( "SendMail"、e.getMessage()、e); } sender = null; } }); } public void onActivityResult(int requestCode、int resultCode、Intent data){ if(resultCode == RESULT_OK){ if(requestCode == SELECT_PICTURE){ Uri selectedImageUri = data.getData(); selectedImagePath = getPath(selectedImageUri); //disimage.setImageURI(Uri.parse(selectedImagePath)); } } } public String getPath(Uri uri){ String[]プロジェクション={MediaStore.Images.Media.DATA}; カーソルcursor=managedQuery(uri、projection、null、null、null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); // Toast.makeText(this、cursor.getString(column_index).toString()、Toast.LENGTH_LONG); cursor.getString(column_index);を返します。 } }
MailSender.javaファイル
パッケージcom.drc.email; インポートjavax.activation.DataHandler; インポートjavax.activation.DataSource; インポートjavax.activation.FileDataSource; インポートjavax.mail.Message; インポートjavax.mail.Multipart; インポートjavax.mail.PasswordAuthentication; インポートjavax.mail.Session; インポートjavax.mail.Transport; インポートjavax.mail.internet.InternetAddress; インポートjavax.mail.internet.MimeBodyPart; インポートjavax.mail.internet.MimeMessage; インポートjavax.mail.internet.MimeMultipart; import java.io.ByteArrayInputStream; インポートjava.io.IOException; インポートjava.io.InputStream; インポートjava.io.OutputStream; インポートjava.util.Properties; パブリッククラスMailSenderはjavax.mail.Authenticatorを拡張します{ private String mailhost = "smtp.gmail.com"; プライベート文字列ユーザー。 プライベート文字列パスワード。 プライベートセッションセッション。 静的{ // Security.addProvider(new // org.apache.harmony.xnet.provider.jsse.JSSEProvider()); } public MailSender(String user、String password){ this.user=ユーザー; this.password=パスワード; System.out.println( "Hello"); プロパティprops=new Properties(); props.setProperty( "mail.transport.protocol"、 "smtp"); props.setProperty( "mail.host"、mailhost); props.put( "mail.smtp.auth"、 "true"); props.put( "mail.smtp.port"、 "465"); props.put( "mail.smtp.socketFactory.port"、 "465"); props.put( "mail.smtp.socketFactory.class"、 "javax.net.ssl.SSLSocketFactory"); props.put( "mail.smtp.socketFactory.fallback"、 "false"); props.setProperty( "mail.smtp.quitwait"、 "false"); session = Session.getDefaultInstance(props、this); } 保護されたPasswordAuthenticationgetPasswordAuthentication(){ 新しいPasswordAuthentication(user、password);を返します。 } パブリック同期voidsendMail(文字列の件名、文字列の本文、文字列の送信者、文字列の受信者)は例外をスローします{ MimeMessageメッセージ=新しいMimeMessage(セッション); DataHandlerハンドラー=newDataHandler(new ByteArrayDataSource(body.getBytes()、 "text / plain")); message.setSender(new InternetAddress(sender)); message.setSubject(subject); message.setDataHandler(handler); if(recipients.indexOf('、')> 0) message.setRecipients(Message.RecipientType.TO、InternetAddress.parse(recipients)); そうしないと message.setRecipient(Message.RecipientType.TO、new InternetAddress(recipients)); Transport.send(メッセージ); } パブリック同期voidsendMailAttach(文字列の件名、文字列の本文、文字列の送信者、文字列の受信者、文字列のselectedImagePath、文字列のファイル名)は例外をスローします{ MimeMessageメッセージ=新しいMimeMessage(セッション); message.setSender(new InternetAddress(sender)); message.setSubject(subject); //メールメッセージのテキストを設定します。 // MimeBodyPart messagePart = new MimeBodyPart(); messagePart.setText(body); // //メールの添付ファイルを設定します // MimeBodyPart attachmentPart = new MimeBodyPart(); FileDataSource fileDataSource = new FileDataSource(selectedImagePath){ @オーバーライド public String getContentType(){ 「application/octet-stream」を返します。 } }; attachmentPart.setDataHandler(new DataHandler(fileDataSource)); attachmentPart.setFileName(filename); マルチパートmultipart=new MimeMultipart(); multipart.addBodyPart(messagePart); multipart.addBodyPart(attachmentPart); message.setContent(multipart); if(recipients.indexOf('、')> 0) {message.setRecipients(Message.RecipientType.TO、InternetAddress.parse(recipients));} そうしないと {message.setRecipient(Message.RecipientType.TO、new InternetAddress(recipients));} Transport.send(メッセージ); } パブリッククラスByteArrayDataSourceはDataSource{を実装します プライベートbyte[]データ; プライベート文字列型。 public ByteArrayDataSource(byte [] data、String type){ 素晴らしい(); this.data = data; this.type = type; } public ByteArrayDataSource(byte [] data){ 素晴らしい(); this.data = data; } public void setType(String type){ this.type = type; } public String getContentType(){ if(type == null) 「application/octet-stream」を返します。 そうしないと リターンタイプ; } public InputStream getInputStream()はIOException{をスローします 新しいByteArrayInputStream(data);を返します。 } public String getName(){ 「ByteArrayDataSource」を返します。 } public OutputStream getOutputStream()はIOException{をスローします 新しいIOException( "サポートされていません");をスローします。 } } }
3つの外部jarファイルを使用しています。
- Activation.jar
- additional.jar
- mail.jar