我是新來的Java郵件,我想發送帶附件的郵件,所以我想創建一個從網上樣本代碼的測試郵件,但我得到javax.mail.sendfailedexception:發送失敗;嵌套的例外情況是:類javax.mail.authenticationfailedexception在發送
javax.mail.sendfailedexception: sending failed;
nested exception is: class javax.mail.authenticationfailedexception
at javax.mail.transport.send0(Transport.java.218)
at javax.mail.transport.send(Transport.java.80)
我試着用不同的身份驗證,但我失敗了
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail
{
public static void main(String [] args)
{
// Recipient's email ID needs to be mentioned.
String to = "[email protected]";
// Sender's email My Office mail server
String from = "[email protected]";
String pass = "Password";
String host = "172.23.5.10";
String port = "25";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.smtp.port", port);
properties.setProperty("mail.smtp.auth", "true");
// Get the default Session object.
Session session = Session.getInstance(properties,new MailAuthentication(from,pass));
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("Test Mail");
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText("Test Mail Success Hari");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart);
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
import javax.mail.*
public class MailAuthentication extends Authentication
{
String _user;
String _pass;
public GMailAuthenticator (String username, String password)
{
super();
this._user = username;
this._pass = password;
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(_user, _pass);
}
}
我也試圖與
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
的d
Session session = Session.getDefaultInstance(properties);
和
Session session = Session.getDefaultInstance(properties);
,但我得到了同樣的錯誤。
請幫我解決了這一點
我使用Windows PC像java環境變量是否有SMTP任何措施?
在此先感謝。
檢查您的用戶名和密碼值。他們是否正確? –
尊敬的@SamwiseGamgee用戶名和密碼是正確的我直接登錄到172.23.5.10端口(公司郵件服務器)在瀏覽器與此和outlook也配置了這個IP和用戶名,密碼。 – Hari
檢查[協議跟蹤](http://www.oracle.com/technetwork/java/javamail/faq/index.html#debug)以獲取有關服務器拒絕您的用戶名和密碼的原因的更多線索。您可能必須將「mail.debug.auth」屬性設置爲「true」才能獲得完整的協議跟蹤。注意不要在這裏發佈它,因爲它可能會暴露您的密碼。 –