AndroidのGmailSenderでSDのファイルを添付しようとしています。
私はこれを持っています: GMailSender を介して添付ファイル付きのメールを送信しますか?
ファイルが存在することを確認しましたが、メールが正しく送信されません。ファイルが添付されていない同じ方法は問題なく実行されます。
問題は何でしょうか?
どうも。
File imageFile = new File("/sdcard/prueba.jpg");
if (imageFile.exists()){
Toast.makeText(getApplicationContext(),"Existe",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"NO Existe", Toast.LENGTH_LONG).show();
}
gmail.sendMail("Datos", c.getMailBody(),Constants.getAddress(), udb.getMail(), imageFile);
私の GmailSender クラス:
public class GMailSender extends javax.mail.Authenticator{
private final static String defaultUserName = "particle";
private final static String defaultPassword = "particle123";
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
static {
Security.addProvider(new JSSEProvider());
}
public GMailSender() {
new GMailSender(defaultUserName, defaultPassword);
}
public GMailSender(String user, String password) {
this.user = user;
this.password = password;
Properties 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);
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
try{
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}catch(Exception e){
}
}
public synchronized void sendMail(String subject, String body, String sender, String recipients, File attachment) throws Exception {
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if(attachment!=null){
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(body);
MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(attachment);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
message.setContent(mp);
}
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}
public class ByteArrayDataSource implements DataSource {
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type) {
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data) {
super();
this.data = data;
}
public void setType(String type) {
this.type = type;
}
public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
public String getName() {
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not Supported");
}
}
}
私の例外は:
javax.mail.MessagingException: IOException while sending message;
ネストされた例外: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; 境界="----=_Part_1_1079440400.1348225974758" com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1177) で javax.mail.Transport.send0(Transport.java:195) で javax.mail .Transport.send(Transport.java:124) at teru.SimDetect.TFC.GMailSender.sendMail(GMailSender.java:105) at teru.SimDetect.TFC.SimDetectActivity.onCreate(SimDetectActivity.java:56) at android.app. Instrumentation.callActivityOnCreate(Instrumentation.java:1072) で android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836) で android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893) で android.app.ActivityThread.access$1500( ActivityThread.java:135) android.app で。ActivityThread$H.handleMessage(ActivityThread.java:1054) で android.os.Handler.dispatchMessage(Handler.java:99) で android.os.Looper.loop(Looper.java:150) で android.app.ActivityThread.main (ActivityThread.java:4385) で java.lang.reflect.Method.invokeNative(ネイティブ メソッド) で java.lang.reflect.Method.invoke(Method.java:507) で com.android.internal.os.ZygoteInit$MethodAndArgsCaller .run(ZygoteInit.java:849) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) at dalvik.system.NativeStart.main(Native Method) 原因: javax.activation.UnsupportedDataTypeException: no MIME タイプ multipart/mixed のオブジェクト DCH。bound="----=_Part_1_1079440400.1348225974758" at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:905) at javax.activation.DataHandler.writeTo(DataHandler.java: