使用 javaMail Api 从 android 应用程序发送电子邮件时附件的格式类型未知
Unknown format type for attachment while sending email from android app using javaMail Api
我正在尝试开发一个应用程序,其中包含一个用于上传求职者的屏幕resume.As我可以看到带有附件的电子邮件已发送到'To' 邮件 ID,但文档类型指定为 'unknown file type format'。另外,因为我正在使用 Google 邮件提供的功能,电子邮件以注册电子邮件 ID 的名义发送,而不是访客邮件 Id.Following 是我编写的用于发送 email.Please 提前帮助我 out.Thanks 的代码。
System.out.println("TLSEmail Start");
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.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.port", "465");
//create Authenticator object to pass in Session.getInstance argument
Authenticator auth = new Authenticator() {
//override the getPasswordAuthentication method
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail, "wpndththyxgofpmt");
}
};
Session session = Session.getInstance(props, auth);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("ayyapparushi@gmail.com"));
message.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(toEmail));
System.out.println("Mail Check 2");
message.setSubject(al_JobsList.get(Position).getJobName() + "-" + al_JobsList.get(Position).getJobExp());
System.out.println("Mail Check 3");
BodyPart messageBodyPart;
messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(messagepart, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart=new MimeBodyPart();
message.setText(messagepart2);
// Transport.send(message);
String file = emailmsg;
String fileName = name + " resume";
FileDataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
// messageBodyPart.attachFile(file);
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
// multipart.addBodyPart(message);
message.setContent(multipart);
Transport.send(message);
}
首先清理这些 common mistakes。
确保您使用的是官方 JavaMail for Android。
JavaMail(实际上是 JAF)只知道几种文件类型。您可能需要为其他类型的文件指定 MIME 类型。使用 MimeBodyPart.attachFile method that allows you to specify the type.
使用 Gmail(或几乎任何其他电子邮件提供商),您无法以任意用户身份发送邮件。您只能以帐户所有者的身份发送消息。
当电子邮件发送到 user.While 从移动设备的文件资源管理器中选择文件时,我找到了关于附件文件格式不受支持的问题的答案,获取了路径,但文件扩展名是不是 obtained.Hence,在提供文件名时我们需要附加文件的扩展名 attached.Thus,交付时 Google 会自动检查文件格式。
String fileName = name + " resume"+".docx";
这一行解决了我的问题。
我正在尝试开发一个应用程序,其中包含一个用于上传求职者的屏幕resume.As我可以看到带有附件的电子邮件已发送到'To' 邮件 ID,但文档类型指定为 'unknown file type format'。另外,因为我正在使用 Google 邮件提供的功能,电子邮件以注册电子邮件 ID 的名义发送,而不是访客邮件 Id.Following 是我编写的用于发送 email.Please 提前帮助我 out.Thanks 的代码。
System.out.println("TLSEmail Start");
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.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.port", "465");
//create Authenticator object to pass in Session.getInstance argument
Authenticator auth = new Authenticator() {
//override the getPasswordAuthentication method
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail, "wpndththyxgofpmt");
}
};
Session session = Session.getInstance(props, auth);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("ayyapparushi@gmail.com"));
message.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(toEmail));
System.out.println("Mail Check 2");
message.setSubject(al_JobsList.get(Position).getJobName() + "-" + al_JobsList.get(Position).getJobExp());
System.out.println("Mail Check 3");
BodyPart messageBodyPart;
messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(messagepart, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart=new MimeBodyPart();
message.setText(messagepart2);
// Transport.send(message);
String file = emailmsg;
String fileName = name + " resume";
FileDataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
// messageBodyPart.attachFile(file);
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
// multipart.addBodyPart(message);
message.setContent(multipart);
Transport.send(message);
}
首先清理这些 common mistakes。
确保您使用的是官方 JavaMail for Android。
JavaMail(实际上是 JAF)只知道几种文件类型。您可能需要为其他类型的文件指定 MIME 类型。使用 MimeBodyPart.attachFile method that allows you to specify the type.
使用 Gmail(或几乎任何其他电子邮件提供商),您无法以任意用户身份发送邮件。您只能以帐户所有者的身份发送消息。
当电子邮件发送到 user.While 从移动设备的文件资源管理器中选择文件时,我找到了关于附件文件格式不受支持的问题的答案,获取了路径,但文件扩展名是不是 obtained.Hence,在提供文件名时我们需要附加文件的扩展名 attached.Thus,交付时 Google 会自动检查文件格式。
String fileName = name + " resume"+".docx";
这一行解决了我的问题。