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;
}
}
私は添付ファイルのコードを見ません。 – greenapps
'messageBodyPart.setDataHandler(新しいDataHandler(fds));'あなたは2を忘れましたか? – greenapps