Java 发送邮件:身份验证失败
我正在尝试从应用程序发送电子邮件,我使用以下代码:Java 发送邮件:身份验证失败,java,android,email,Java,Android,Email,我正在尝试从应用程序发送电子邮件,我使用以下代码: private static final String username = "sth@gmail.com"; private static final String password = "pass"; private void sendMail(String email, String subject, String messageBody) { Session session = createSessionObject
private static final String username = "sth@gmail.com";
private static final String password = "pass";
private void sendMail(String email, String subject, String messageBody) {
Session session = createSessionObject();
try {
Message message = createMessage(email, subject, messageBody, session);
new SendMailTask().execute(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
private Message createMessage(String email, String subject, String messageBody, Session session) throws MessagingException, UnsupportedEncodingException {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("sth@gmail.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email));
message.setSubject(subject);
message.setText(messageBody);
return message;
}
private Session createSessionObject() {
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
return Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}
private class SendMailTask extends AsyncTask<Message, Void, Void> {
private ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(SendQuestion.this, "Please wait", "Sending mail", true, false);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
}
@Override
protected Void doInBackground(Message... messages) {
try {
Transport.send(messages[0]);
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}
private静态最终字符串用户名=”sth@gmail.com";
私有静态最终字符串password=“pass”;
私有void sendMail(字符串电子邮件、字符串主题、字符串消息体){
会话=createSessionObject();
试一试{
消息消息=创建消息(电子邮件、主题、消息正文、会话);
新建SendMailTask().execute(消息);
}捕获(地址e){
e、 printStackTrace();
}捕获(消息异常e){
e、 printStackTrace();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
}
私有消息createMessage(字符串电子邮件、字符串主题、字符串消息正文、会话会话)引发MessaginException、UnsupportedEncodingException{
Message Message=新的mimessage(会话);
message.setFrom(新的InternetAddress(“sth@gmail.com"));
message.addRecipient(message.RecipientType.TO,新的Internet地址(电子邮件,电子邮件));
message.setSubject(主题);
message.setText(messageBody);
返回消息;
}
专用会话createSessionObject(){
属性=新属性();
properties.put(“mail.smtp.auth”、“true”);
properties.put(“mail.smtp.starttls.enable”、“true”);
properties.put(“mail.smtp.host”、“smtp.gmail.com”);
properties.put(“mail.smtp.port”,“587”);
return Session.getInstance(属性,new javax.mail.Authenticator(){
受保护的密码身份验证getPasswordAuthentication(){
返回新密码身份验证(用户名、密码);
}
});
}
私有类SendMailTask扩展了AsyncTask{
私有进程对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
progressDialog=progressDialog.show(SendQuestion.this,“请稍候”,“正在发送邮件”,true,false);
}
@凌驾
受保护的void onPostExecute(void避免){
super.onPostExecute(避免);
progressDialog.disclose();
}
@凌驾
受保护的Void doInBackground(消息…消息){
试一试{
Transport.send(消息[0]);
}捕获(消息异常e){
e、 printStackTrace();
}
返回null;
}
}
Eclipse抛出javax.mail.AuthenticationFailedException。当我第一次尝试发送邮件时,我的gmail帐户收到一封来自谷歌的关于安全的邮件,他们阻止了邮件。然后,我激活at/LESSescureapps,以允许不太安全的应用。但这并没有解决问题。是我的代码有问题还是gmail有问题
PS:我已经添加了所有必需的.jar,并包括了对清单的Internet访问
public static int Email(String fromemail, String toemail, String cc,
String bcc, String Subject, String Body, String Attachment) {
final String fromEmail = fromemail;// "user@gmail.com"; //requires
// valid gmail id
// final String password = Password; // correct password for gmail id
final String toEmail = toemail;// "user2@ymail.com"; // can be any
// email id
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
sf.setTrustAllHosts(true);
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com"); // SMTP Host
props.put("mail.smtp.socketFactory.port", "465"); // SSL Port
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory"); // SSL Factory Class
props.put("mail.smtp.auth", "true"); // Enabling SMTP Authentication
props.put("mail.smtp.port", "465"); // SMTP Port
Authenticator auth = new Authenticator() {
// override the getPasswordAuthentication method
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail, Password);
}
};
Session session = Session.getDefaultInstance(props, auth);
int i;
i = sendEmail(session, toEmail, Subject, Body);
return i;
}
这是发送功能:
public static void sendEmail(Session session, String toEmail,
String subject, String body) {
try {
MimeMessage msg = new MimeMessage(session);
// set message headers
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
msg.addHeader("format", "flowed");
msg.addHeader("Content-Transfer-Encoding", "8bit");
msg.setFrom(new InternetAddress("no_reply@journaldev.com",
"Operation Department Email"));
msg.setReplyTo(InternetAddress.parse("no_reply@journaldev.com",
false));
msg.setSubject(subject, "UTF-8");
msg.setText(body, "UTF-8");
msg.setSentDate(new Date());
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toEmail, false));
Transport.send(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
事实证明,这是谷歌服务器的问题。我联系了支持人员,他们把它修好了。上述代码工作正常。谢谢您的回答。2件事:1)我=发送电子邮件(会话、电子邮件、主题、正文);sendEmail无效,因此这是MailSSLSocketFactory的错误2)我应该创建新类吗?