我正在尝试在Android中附加带有GmailSender的SD文件。
我有这个:Sending email with attachment through GMailSender?
我检查过该文件是否存在,但邮件无法正确发送。没有附加文件的相同方法运行正常。
可能是什么问题?
THX。
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:没有MIME类型multipart / mixed的对象DCH; 边界= “---- = _ Part_1_1079440400.1348225974758” at 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) 在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) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:507) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:849) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) at dalvik.system.NativeStart.main(Native Method) 引起:javax.activation.UnsupportedDataTypeException:没有MIME类型multipart / mixed的对象DCH; 边界= “---- = _ Part_1_1079440400.1348225974758” 在javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:905) 在javax.activation.DataHandler.writeTo(DataHandler.java:330) 在javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1485) 在javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1773) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1121) ......还有17个
答案 0 :(得分:0)
也许这可以帮到你:
String image = "prueba.jpg";
File file = new File(Environment.getExternalStorageDirectory(), image);
GMailSender sender = new GMailSender("user@gmail.com", "password");
sender.sendMail (head , body ,"user@gmail.com", "sendto@mail.com", file);
GmailSender已修改:
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);
}
答案 1 :(得分:0)
代码很好,问题是GMailSender库,它已经过时了。
谢谢你。