【发布时间】:2016-05-13 08:40:43
【问题描述】:
我已经看到了有关此类问题的所有答案。但是我的邮件没有发送。实际上,当我使谷歌的安全性降低时它会发送。但对我来说,这不是一个解决方案!因为它使 gmail 帐户不受保护,所以使用该应用程序的人也不想让他们的邮件安全。所以我尝试了所有建议的变体并阅读了 Java Mail FAQ。这是代码。 请,如果有真正的解决方案,请帮助我。但是请不要建议更改谷歌安全! 我需要不需要应用用户进行任何操作的解决方案。
public class Mail {
public Mail() {
_host = "smtp.gmail.com"; // default smtp server
_port = "587"; // default smtp port587
_user = "myname@gmail.com"; // username
_pass = "password"; // password
_from = "myname@gmail.com"; // email sent from
_to = {"yourname@gmail.com"};
_subject = "go"; // email subject
_body = "mail"; // email body
_debuggable = true; // 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, new GMailAuthenticator(_user,_pass));
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 t = session.getTransport("smtp");
try {
t.connect(_host, _user, _pass);
t.sendMessage(msg, msg.getAllRecipients());
} finally {
t.close();
}
return true;
} else {
return false;
}
}
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.starttls.enable", "true");
props.put("mail.debug", "true");
return props;
}
// class for secure access gmail
class GMailAuthenticator extends Authenticator {
String user;
String pw;
public GMailAuthenticator (String username, String password)
{
super();
this.user = username;
this.pw = password;
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user, pw);
}
}
以及我在活动中调用的方法 send()
final Button send = (Button) this.findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
new SendClass().execute();
}
});
private class SendClass extends AsyncTask{
@Override
protected Object doInBackground(Object[] objects) {
Mail m = new Mail();
m.send();
return null;
}
}
【问题讨论】:
-
您似乎做得对,但更安全意味着您在电子邮件中设置了 MFA - 对吧?您可能想要更改您的身份验证代码,因为它只使用 uid/pwd 。查看此页面crunchify.com/java-mailapi-example-send-an-email-via-gmail-smtp
标签: java android email gmail jakarta-mail