gpt4 book ai didi

android - 如何在不使用外部应用程序的情况下直接发送带附件的电子邮件

转载 作者:行者123 更新时间:2023-11-29 15:13:53 25 4
gpt4 key购买 nike

我有这个简单的代码和另一个 jar 库,使我能够发送电子邮件而无需转到其他邮件应用程序。

 public class claimrewardemail extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.successful);

final String username = "myname@gmail.com";
final String password = "mypassword";



new Thread(new Runnable() {

@Override
public void run() {
try {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(
username, password);
}
});
// TODO Auto-generated method stub
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from-email@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("youremail@gmail.com"));
message.setSubject("email");
message.setText("HI,"
+ "\n\n great");

Transport.send(message);
System.out.println("Done");


} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}).start();

}
}

上面的代码帮助我直接发送带有硬编码电子邮件、标题和消息的邮件,现在我想添加附件部分,我可以在手机中访问我的文件,并添加一个按钮,我可以将附件和附件一起发送这封邮件。谁能帮帮我?

最佳答案

试试这个:

    String filename = "example_filename";
Multipart _multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);

messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);

_multipart.addBodyPart(messageBodyPart);
message.setContent(_multipart);

编辑:

当您包含 aFileChooser 库时,您应该在您的 Activity 布局中创建两个按钮(“发送电子邮件”和“选择文件”),然后在您的 Activity 中:

private Button sendEmail;
private Button chooseFileButton;
private String filename;
private static final int REQUEST_CHOOSER = 1234;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

sendEmail = (Button) findViewById(R.id.send_email_button_id);
sendEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {

@Override
public void run() {
try {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(
username, password);
}
});
// TODO Auto-generated method stub
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from-email@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("youremail@gmail.com"));
message.setSubject("email");
message.setText("HI,"
+ "\n\n great");
if (!"".equals(filename)) {
Multipart _multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);

messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);

_multipart.addBodyPart(messageBodyPart);
message.setContent(_multipart);
}
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}).start();
}
});

chooseFileButton = (Button) findViewById(R.id.choose_file_button_id);
chooseFileButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Create the ACTION_GET_CONTENT Intent
Intent getContentIntent = FileUtils.createGetContentIntent();

Intent intent = Intent.createChooser(getContentIntent, "Select a file");
startActivityForResult(intent, REQUEST_CHOOSER);
}
});
}

然后添加onActivityResult 方法,在这里你可以获取选择的文件名:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CHOOSER:
if (resultCode == RESULT_OK) {

final Uri uri = data.getData();

// Get the File path from the Uri
filename = FileUtils.getPath(this, uri);
}
break;
}
}

不要忘记将 FileChooserActivity 添加到您的 AndroidManifest.xml:

<activity
android:name="com.ipaulpro.afilechooser.FileChooserActivity"
android:icon="@drawable/ic_launcher"
android:enabled="true"
android:exported="true"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.OPENABLE" />

<data android:mimeType="*/*" />
</intent-filter>
</activity>

之后您可以发送电子邮件,点击“发送”按钮;

关于android - 如何在不使用外部应用程序的情况下直接发送带附件的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26668620/

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