我尝试使用javax邮件发送电子邮件,但我在网上找到的所有资源都带有附件,因此我的代码会抛出错误:
public boolean send() throws Exception {
Properties props = _setProperties();
if(!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") && !_subject.equals("") && !_body.equals("")) {
Session session = Session.getInstance(props, this);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(_from));
InternetAddress[] addressTo = new InternetAddress[_to.length];
for (int i = 0; i < _to.length; i++) {
addressTo[i] = new InternetAddress(_to[i]);
}
msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
msg.setSubject(_subject);
msg.setSentDate(new Date());
// setup message body
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(_body);
_multipart.addBodyPart(messageBodyPart);
// Put parts in message
msg.setContent(_multipart);
final MimeMessage msg2 = msg;
// send email
new Thread(new Runnable() {
@Override
public void run() {
try {
Transport.send(msg2);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}).start();
return true;
} else {
return false;
}
}
最终投掷:
W/System.err: javax.mail.MessagingException: IOException while sending message;
W/System.err: nested exception is:
W/System.err: java.io.FileNotFoundException: /sdcard/filelocation (No such file or directory)
W/System.err: at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:676)
W/System.err: at javax.mail.Transport.send0(Transport.java:189)
W/System.err: at javax.mail.Transport.send(Transport.java:118)
W/System.err: at com.example..logintest.Mail$1$override.run(Mail.java:143)
W/System.err: at com.example..logintest.Mail$1$override.access$dispatch(Mail.java)
W/System.err: at com.example..logintest.Mail$1.run(Mail.java:0)
W/System.err: at java.lang.Thread.run(Thread.java:761)
W/System.err: Caused by: java.io.FileNotFoundException: /sdcard/filelocation (No such file or directory)
我无法在网上找到任何帮助,我绝对不会尝试发送文件。我的_body只是一个简单的字符串。有任何想法吗?我不明白如何设置字段,以便他们不会尝试发送附件。