美好的一天。我在一个应用程序中使用GMail来发送带有附加文件的电子邮件。我这样做的方式是我的应用程序会自动撰写邮件,附加附件,并将其发送给收件人,而无需打开邮件应用程序。该应用程序目前已经投入使用,就在今天早上,我发现我无法再发送电子邮件了。起初我认为设备没有互联网连接,或密码被更改,但这两个都不是真的,我检查了logcat,我收到了这个错误:
com.sun.mail.smtp.SMTPSendFailedException: 553 5.1.2 The address specified is not a valid RFC-5321 address. si10sm4645040pab.15 - gsmtp
在Google上搜索问题后,我发现谷歌在10月31日到11月1日之间发布了更新,它以某种方式影响了这个(以及Office Outlook,Thunderbird和其他邮件应用程序)。但是,我无法弄清楚他们改变了什么(如果是端口)或其他什么。这是我的代码:
这是我正在使用的Mail.java
课程:
public class Mail extends javax.mail.Authenticator {
private String _user;
private String _pass;
private String[] _to;
private String _from;
private String _port;
private String _sport;
private String _host;
private String _subject;
private String _body;
private boolean _auth;
private boolean _debuggable;
private Multipart _multipart;
public Mail() {
_host = "smtp.gmail.com"; // default smtp server
_port = "465"; // 465 -- default smtp port
_sport = "465"; // 465 -- default socketfactory port
_user = ""; // username
_pass = ""; // password
_from = ""; // email sent from
_subject = ""; // email subject
_body = ""; // email body
_debuggable = false; // debug mode on or off - default off
_auth = true; // smtp authentication - default on
_multipart = new MimeMultipart();
// There is something wrong with MailCap, javamail can not find a handler for the
// multipart/mixed part, so this bit needs to be added.
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
}
public Mail(String user, String pass) {
this();
_user = user;
_pass = pass;
}
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);
// send email
Transport.send(msg);
return true;
} else {
return false;
}
}
public void addAttachment(String filename) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
// Truncating the full file path to just filename
Pattern p = Pattern.compile("[^/]*$");
Matcher m = p.matcher(filename);
if (m.find()){
messageBodyPart.setFileName(m.group());
}
else{
messageBodyPart.setFileName(filename);
}
_multipart.addBodyPart(messageBodyPart);
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(_user, _pass);
}
private Properties _setProperties() {
Properties props = new Properties();
props.put("mail.smtp.host", _host);
if(_debuggable) {
props.put("mail.debug", "true");
}
if(_auth) {
props.put("mail.smtp.auth", "true");
}
props.put("mail.smtp.port", _port);
props.put("mail.smtp.socketFactory.port", _sport);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
return props;
}
// the getters and setters here
}
我在活动中使用此类:
private boolean sendEmail(String attachmentFilePath, String subject){
Mail m = new Mail("sender.email@gmail.com", "passwordHere");
String[] toArr = {"destination@yahoo.com"};
m.set_to(toArr);
//include time here
m.set_from("sender");
m.set_subject(subject);
m.setBody("Please check the attached file.");
try {
m.addAttachment(attachmentFilePath);
if(m.send()) {
File exportDir = new File(Environment.getExternalStorageDirectory().getPath() + "/csv", "");
File original = new File(attachmentFilePath);
String baseFilename = original.getName().toString().replace(".csv", "");
File to = new File(exportDir, baseFilename + " (sent).csv");
original.renameTo(to);
}
else {
Toast.makeText(getBaseContext(), "Email was not sent.", Toast.LENGTH_LONG).show();
return false;
}
}
catch(Exception e) {
//Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show();
Log.e("MailApp", "Could not send email", e);
return false;
}
return true;
}
但是,由于收到错误,我似乎无法发送电子邮件:
11-05 10:58:56.244 7672-7692/com.agict.marswin E/MailApp﹕ Could not send email
com.sun.mail.smtp.SMTPSendFailedException: 553 5.1.2 The address specified is not a valid RFC-5321 address. qc16sm4604799pab.47 - gsmtp
我不太清楚这里发生了什么。它看起来似乎没有更改端口,我在用于发送的电子邮件地址和我发送的地址中没有任何错误或印刷错误。
非常感谢任何帮助。谢谢。
答案 0 :(得分:1)
我也面临着类似的问题。尝试将@ gmail.com添加到您的发件人电子邮件地址。为我工作。