2015-10-17 173 views
1

我有一個IMAP主機和用戶名和密碼。使用此憑據,我想將電子郵件發送到IMAP服務器,該服務器將路由請求。身份驗證失敗使用IMAP將電子郵件發送到服務器

我的代碼是

import java.util.Properties; 
import javax.mail.*; 
import javax.mail.internet.*; 

import com.sun.mail.imap.IMAPStore; 
import com.sun.mail.util.MailSSLSocketFactory; 

import java.util.*; 

public class Mail { 

private String to = "[email protected]"; 
private String from ="[email protected]"; 
private String message ="test"; 
private String subject="Test"; 
private String imapServ="hist.abc.net"; 
private String userName="[email protected]"; 
private String password="xxxxxxx"; 

public String getPassword() { 
return password; 
} 

public void setPassword(String password) { 
this.password = password; 
System.out.println("password:"+password); 
} 

public String getUserName() { 
return userName; 
} 

public void setUserName(String userName) { 
this.userName = userName; 
System.out.println("userName:"+userName); 
} 


/** 
* @return the to 
*/ 
public String getTo() { 
return to; 
} 

/** 
* @param to the to to set 
*/ 

public void setTo(String to) { 
this.to = to; 
} 

/** 
* @return the from 
*/ 

public String getFrom() { 
return from; 
} 

/** 
* @param from the from to set 
*/ 

public void setFrom(String from) { 
this.from = from; 
} 

/** 
* @return the message 
*/ 

public String getMessage() { 
return message; 
} 

/** 
* @param message the message to set 
*/ 

public void setMessage(String message) { 
this.message = message; 
} 

/** 
* @return the subject 
*/ 

public String getSubject() { 
return subject; 
} 

/** 
* @param subject the subject to set 
*/ 

public void setSubject(String subject) { 
this.subject = subject; 
} 

/** 
* @return the imapServ 
*/ 

public String getImapServ() { 
return imapServ; 
} 

public void setImapServ(String imapServ) { 
this.imapServ = imapServ; 
} 


public int sendMail(){ 

try 
{ 
Properties props = System.getProperties(); 
props.setProperty("mail.imap.sasl.enable", "true"); 
props.setProperty("mail.imap.starttls.enable", "true"); 

props.setProperty("mail.imap.auth.ntlm.domain", "false"); 
props.setProperty("mail.imap.auth.plain.disable", "false"); 
props.setProperty("mail.imap.auth.gssapi.disable", "true"); 
props.setProperty("mail.imap.ssl.enable", "true"); 
props.setProperty("mail.imap.port", "993"); 
Session imapSession = Session.getInstance(props); 
imapSession.setDebug(true); 
IMAPStore store = new IMAPStore(imapSession, null); 
Authenticator auth = new SMTPAuthenticator(); 

Session session = Session.getInstance(props, auth); 
session.setDebug(true); 
// -- Create a new message -- 
Store store1=imapSession.getStore("imap"); 
store1.connect(imapServ,userName,password); 

Folder folder=store1.getFolder("INBOX"); 
folder.open(Folder.READ_ONLY); 

Message mess[]=folder.getMessages(); 

for(int i=mess.length-1;i>=0;i--) 
{ 
System.out.println(""+i+":"+mess[i].getFrom()[0]+"t"+mess[i].getSubject()); 
} 

Message msg = new MimeMessage(session); 

// -- Set the FROM and TO fields -- 
msg.setFrom(new InternetAddress(from)); 

String rec[]=to.split(","); 
for(int i=0;i<rec.length;i++) 
{ 
System.out.println("rec:"+rec[i]); 

msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(rec[i], false)); 
msg.setSubject(subject); 
msg.setText(message); 
// -- Set some other header information -- 
msg.setHeader("Mail", "MailApi"); 
msg.setSentDate(new Date()); 
// -- Send the message -- 
Transport.send(msg); 
System.out.println("Message sent to"+ rec[i]+" OK."); 
} 
return 0; 
} 
catch (Exception ex) 
{ 
ex.printStackTrace(); 
System.out.println("Exception "+ex); 
return -1; 
} 
} 


private class SMTPAuthenticator extends javax.mail.Authenticator { 
@Override 
public PasswordAuthentication getPasswordAuthentication() { 
String username =userName;   
String pass =password;          
return new PasswordAuthentication(username, pass); 
} 
} 

public static void main(String[] args) { 
Mail m = new Mail(); 
m.sendMail(); 
} 

}

我收到錯誤

異常javax.mail.AuthenticationFailedException:AUTHENTICATE 失敗。

任何想法如何解決這個錯誤?

回答

1

你登錄到您的IMAP服務器,然後使用SMTP發送郵件,但你還沒有配置SMTP服務器。您還註釋了一些將「imap」設置爲transport的屬性設置。那永遠不會工作; 「imap」是Store協議,「smtp」是一種傳輸協議。

你對一堆電子郵件基礎知識感到困惑。您可能想花一些時間與JavaMail FAQJavaMail sample programs

AuthenticationFailedException通常意味着服務器沒想到你提供了正確的用戶名和密碼。打開JavaMail Session debugging以獲取有關失敗的更多信息。

相關問題