代码不会发送带附件的邮件
问题描述:
我想在Android中使用附件发送电子邮件。如果我只发送邮件给我的邮件,但如果我想发送附件,它不会发送到我的邮件。代码不会发送带附件的邮件
是什么原因?
如果你想我可以发送你我所有的代码。
public class SendMail extends AsyncTask<Void,Void,Void> {
//Declaring Variables
private Context context;
private Session session;
//Information to send email
private String email;
private String subject;
private String message;
private String filePath;
//Progressdialog to show while sending email
private ProgressDialog progressDialog;
//Class Constructor
public SendMail(Context context, String email, String subject, String message, String filePath){
//Initializing variables
this.context = context;
this.email = email;
this.subject = subject;
this.message = message;
this.filePath = filePath;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//Showing progress dialog while sending email
progressDialog = ProgressDialog.show(context,"Saadan KT","See võib aega võtta",false,false);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//Dismissing the progress dialog
progressDialog.dismiss();
//Showing a success message
Toast.makeText(context,"Korras;)",Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... params) {
//Creating properties
Properties props = new Properties();
//Configuring properties for gmail
//If you are not using gmail you may need to change the values
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
//Creating a new session
session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
//Authenticating the password
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(Config.EMAIL, Config.PASSWORD);
}
});
try {
//Creating MimeMessage object
MimeMessage mm = new MimeMessage(session);
//Setting sender address
mm.setFrom(new InternetAddress(Config.EMAIL));
//Adding receiver
mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
//Adding subject
mm.setSubject(subject);
//Adding message
//mm.setText(message);
MimeMultipart multipart = new MimeMultipart("related");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(message);
multipart.addBodyPart(messageBodyPart);
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
DataSource fds = new FileDataSource(filePath);
messageBodyPart.setDataHandler(new DataHandler(fds));
// add image to the multipart
multipart.addBodyPart(messageBodyPart2);
// put everything together
mm.setContent(multipart);
//Sending email
Transport.send(mm);
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}
答
看不到附件代码。 可能这可能会帮助你。看一看。
首先为multipart/mixed part添加处理程序。
// There is something wrong with MailCap, javamail can not find a
// handler for multipart/mixed part, so this bit needs to be added.
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
现在添加附件部分,
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(message);
multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
//setup attachments
ArrayList<String> attachmentsPath = new ArrayList<String>();
//setup attachments
File file = new File(filepath);
MimeBodyPart attachBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(file.getAbsolutePath());
attachBodyPart.setDataHandler(new DataHandler(source));
String name = file.getName();
Log.d(LOG_TAG, "attaching file " + name);
attachBodyPart.setFileName(name);
multipart.addBodyPart(attachBodyPart);
attachmentsPath.add(file.getAbsolutePath());
// Put parts in message
mm.setContent(multipart);
//Sending email
Transport.send(mm);
+0
嗯...它仍然不起作用( –
+0
然后请检查您添加的文件,是文件路径是正确的。因为它是有效的代码,我已经在几个项目中使用它,并且运行良好。您可以请任何日志活动? –
我没有看到附件代码。 – greenapps
'messageBodyPart.setDataHandler(new DataHandler(fds));'你忘了2? – greenapps