我有这个简单的代码和另一个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();
}
}
上面这段代码帮我直接用硬编码的电子邮件,标题和消息发送邮件,现在我想添加附件部分,我可以在手机中访问我的文件,并添加一个按钮,我可以发送附件和这封邮件。有人可以帮帮我吗?
答案 0 :(得分:5)
试试这个:
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
库时,您应该在活动布局中创建两个按钮(“发送电子邮件”和“选择文件”),然后在您的活动中:
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>
之后,您可以通过点击“发送”按钮发送电子邮件;
答案 1 :(得分:0)
是的,有可能你在代码中添加mail.jar和activation.jar文件请搜索一个按钮我想你可能我知道当我们点击你的本地磁盘中选择文件的那个打开的新窗口时不是正常的按钮选择之后,您只需将其分配给代码中的路径,以便电子邮件可以附带发送。