如何在我的应用程序中将 pdf 文件从资产附加到电子邮件?我正在使用以下代码附加图像,但我不知道如何附加 pdf。
电子邮件.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;
公共类电子邮件扩展活动{
按钮发送,附加;
EditText 用户名、密码、从、到、主题、正文;
私有静态最终 int SELECT_PICTURE = 1;
私有字符串 selectedImagePath=null;
/** 在第一次创建活动时调用。*/
@覆盖
公共无效 onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
设置内容视图(R.layout.main);
发送 = (按钮) this.findViewById(R.id.btnsend);
attach = (Button) this.findViewById(R.id.btnattach);
userid = (EditText) this.findViewById(R.id.userid);
密码 = (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() {
@覆盖
公共无效 onClick(查看 v){
// TODO 自动生成的方法存根
// 选择一个文件
selectedImagePath=null;
意图意图 = new Intent();
intent.setType("图片/*");
意图.setAction(意图.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"选择图片"), SELECT_PICTURE);
}
});
send.setOnClickListener(new View.OnClickListener() {
public void onClick(查看视图){
MailSender 发件人 = 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(), "发送邮件成功", 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();
}
} 捕捉(异常 e){
Log.e("SendMail", e.getMessage(), e);
}
发件人=空;
}
});
}
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));
}
}
}
公共字符串getPath(Uri uri){
字符串 [] 投影 = { 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.Internet 地址;
导入 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;
公共类 MailSender 扩展 javax.mail.Authenticator {
私人字符串 mailhost = "smtp.gmail.com";
私有字符串用户;
私人字符串密码;
私人会话会话;
静止的 {
// Security.addProvider(new
// org.apache.harmony.xnet.provider.jsse.JSSEProvider());
}
public MailSender(字符串用户,字符串密码){
this.user = 用户;
this.password = 密码;
System.out.println("你好");
属性 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);
}
受保护的 PasswordAuthentication getPasswordAuthentication() {
返回新的PasswordAuthentication(用户,密码);
}
公共同步无效发送邮件(字符串主题,字符串正文,字符串发件人,字符串收件人)抛出异常{
MimeMessage 消息 = 新的 MimeMessage(会话);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(新 InternetAddress(sender));
message.setSubject(主题);
message.setDataHandler(处理程序);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
别的
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(消息);
}
公共同步无效 sendMailAttach(字符串主题,字符串正文,字符串发件人,字符串收件人,字符串 selectedImagePath,字符串文件名)抛出异常 {
MimeMessage 消息 = 新的 MimeMessage(会话);
message.setSender(新 InternetAddress(sender));
message.setSubject(主题);
// 设置电子邮件消息文本。
//
MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setText(body);
//
// 设置邮件附件文件
//
MimeBodyPart attachmentPart = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource(selectedImagePath) {
@覆盖
公共字符串 getContentType() {
返回“应用程序/八位字节流”;
}
};
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(文件名);
Multipart 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 {
私有字节[] 数据;
私有字符串类型;
公共 ByteArrayDataSource(byte[] 数据,字符串类型) {
极好的();
this.data = 数据;
this.type = 类型;
}
公共 ByteArrayDataSource(byte[] 数据) {
极好的();
this.data = 数据;
}
公共无效 setType(字符串类型){
this.type = 类型;
}
公共字符串 getContentType() {
如果(类型 == 空)
返回“应用程序/八位字节流”;
别的
返回类型;
}
公共输入流 getInputStream() 抛出 IOException {
返回新的字节数组输入流(数据);
}
公共字符串 getName() {
返回“字节数组数据源”;
}
公共输出流 getOutputStream() 抛出 IOException {
throw new IOException("不支持");
}
}
}
我正在使用 3 个外部 jar 文件。
- 激活.jar
- 附加.jar
- 邮件.jar