gpt4 book ai didi

android - 如何附加电子邮件中 Assets 的pdf文件?

转载 作者:行者123 更新时间:2023-12-03 13:24:58 25 4
gpt4 key购买 nike

如何在应用程序中将 Assets 的pdf文件附加到电子邮件中?我正在使用以下代码附加图像,但我不知道如何附加pdf。

EMail.java文件

包com.drc.email;

导入android.app.Activity;
导入android.content.Intent;
导入android.database.Cursor;
导入android.net.Uri;
导入android.os.Bundle;
导入android.provider.MediaStore;
导入android.util.Log;
导入android.view.View;
导入android.view.View.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;

公共(public)类电子邮件扩展了 Activity {
按钮发送,附加;
EditText用户ID,密码,从,到,主题,正文;

私有(private)静态最终int SELECT_PICTURE = 1;
private String selectedImagePath = null;

/** 在第一次创建 Activity 时调用。 */
@Override
公共(public)无效onCreate( bundle 保存的InstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

发送=(按钮)this.findViewById(R.id.btnsend);
附加=(按钮)this.findViewById(R.id.btnattach);
userid =(EditText)this.findViewById(R.id.userid);
密码=(EditText)this.findViewById(R.id.password);
来自=(EditText)this.findViewById(R.id.from);
到=(EditText)this.findViewById(R.id.to);
subject =(EditText)this.findViewById(R.id.subject);
正文=(EditText)this.findViewById(R.id.body);
attach.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v){
//TODO自动生成的方法 stub
//选择一个文件
selectedImagePath = null;
Intent intent = new Intent();
intent.setType(“image/*”);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,“Select Picture”),SELECT_PICTURE);
}
});
send.setOnClickListener(new View.OnClickListener(){

public void onClick(View view){
MailSender发件人=新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(),“发送邮件成功”,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(),“发送附加邮件成功”,Toast.LENGTH_LONG).show();
}
} catch(Exception e){
Log.e(“SendMail”,e.getMessage(),e);

}
sender = null;

}

});

}
public void onActivityResult(int requestCode,int resultCode,Intent data){
如果(resultCode == RESULT_OK){
如果(requestCode == SELECT_PICTURE){
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
//disimage.setImageURI(Uri.parse(selectedImagePath));
}
}
}
公共(public)字符串getPath(Uri uri){
String [] projection = {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;

导入java.io.ByteArrayInputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入java.util.Properties;

公共(public)类MailSender扩展了javax.mail.Authenticator {

private String mailhost =“smtp.gmail.com”;
私有(private)String用户;
私有(private)字符串密码;
私有(private) session session ;

静态的 {
//Security.addProvider(new
//org.apache.harmony.xnet.provider.jsse.JSSEProvider());
}

公共(public)MailSender(字符串用户,字符串密码){
this.user =用户;
this.password =密码;
System.out.println(“H​​ello”);
属性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);
}

protected PasswordAuthentication getPasswordAuthentication(){
返回新的PasswordAuthentication(用户名,密码);
}

公共(public)同步无效sendMail(String subject,String body,String sender,String收件人)引发异常{
MimeMessage消息=新的MimeMessage( session );
DataHandler处理程序= new DataHandler(new ByteArrayDataSource(body.getBytes(),“text/plain”));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
如果(recipients.indexOf(',')> 0)
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(recipients));
别的
message.setRecipient(Message.RecipientType.TO,新的InternetAddress(收件人));

Transport.send(message);
}
公共(public)同步无效sendMailAttach(字符串主题,字符串正文,字符串发件人,字符串收件人,字符串selectedImagePath,字符串文件名)抛出异常{
MimeMessage消息=新的MimeMessage( session );
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
//设置电子邮件文本。
//
MimeBodyPart messagePart =新的MimeBodyPart();
messagePart.setText(body);
//
//设置电子邮件附件文件
//
MimeBodyPart attachmentPart =新的MimeBodyPart();
FileDataSource fileDataSource =新的FileDataSource(selectedImagePath){
@Override
公共(public)字符串getContentType(){
返回“应用程序/八位字节流”;
}
};
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(filename);

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messagePart);
multipart.addBodyPart(attachmentPart);

message.setContent(multipart);

如果(recipients.indexOf(',')> 0)
{message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(recipients));}
别的
{message.setRecipient(Message.RecipientType.TO,新的InternetAddress(收件人));}

Transport.send(message);
}
公共(public)类ByteArrayDataSource实现DataSource {
私有(private)字节[]数据;
私有(private)字符串类型;

public ByteArrayDataSource(byte [] data,String type){
极好的();
this.data =数据;
this.type =类型;
}

公开的ByteArrayDataSource(byte [] data){
极好的();
this.data =数据;
}

public void setType(String type){
this.type =类型;
}

公共(public)字符串getContentType(){
如果(类型==空)
返回“应用程序/八位字节流”;
别的
返回类型;
}

公共(public)InputStream getInputStream()引发IOException {
返回新的ByteArrayInputStream(data);
}

公共(public)字符串getName(){
返回“ByteArrayDataSource”;
}

公共(public)OutputStream getOutputStream()引发IOException {
抛出新的IOException(“不支持”);
}
}
}

我正在使用3个外部jar文件。

  • activation.jar
  • Additional.jar
  • mail.jar
  • 最佳答案

    您应该使用URI引用 Assets 目录中的PDF文件myfile.pdf,例如:

    Uri uri=Uri.parse("file:///android_asset/myfile.pdf"); 

    关于android - 如何附加电子邮件中 Assets 的pdf文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6017385/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com