2015-05-05 87 views
3

我想這個代碼:如何使用我的Outlook帳戶發送電子郵件?

import java.io.IOException; 
import java.util.Properties; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class NewClass { 

public static void main(String[]args)throws IOException { 



final String username = "prashantsood_90"; 
final String password = "password"; 

Properties props = new Properties(); 
props.put("mail.smtp.auth", "true"); 
props.put("mail.smtp.starttls.enable", "true"); 
props.put("mail.smtp.host", "outlook.office365.com"); 
props.put("mail.smtp.port", "587"); 

Session session = Session.getInstance(props, 
    new javax.mail.Authenticator() { 
    protected PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication(username, password); 
    } 
    }); 

try { 

    Message message = new MimeMessage(session); 
    message.setFrom(new InternetAddress("[email protected]")); 
    message.setRecipients(Message.RecipientType.TO, 
     InternetAddress.parse("[email protected]")); 
    message.setSubject("Test"); 
    message.setText("HI"); 

    Transport.send(message); 

    System.out.println("Done"); 

} catch (MessagingException e) { 
    throw new RuntimeException(e); 
} 
} 
} 

這是給此異常:

Exception in thread "main" java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed; 
nested exception is: 
class javax.mail.MessagingException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM 

    at NewClass.main(NewClass.java:47) 
    Caused by: javax.mail.SendFailedException: Sending failed; 
    nested exception is: 
class javax.mail.MessagingException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM 

at javax.mail.Transport.send0(Transport.java:218) 
at javax.mail.Transport.send(Transport.java:80) 
at NewClass.main(NewClass.java:42) 
Java Result: 1 

請幫助我如何從我的Outlook帳戶發送電子郵件到Gmail或任何其他account.I一時無法發送使用Outlook帳戶,但我可以發送Gmail到Gmail郵件。

+0

您可以使Outlook自動運行,請參閱如何在Outlook中創建電子郵件並使其可見信息在http://stackoverflow.com/questions/18057759/how-to-create-an-e-mail-in-outlook-and-make-it-visible-for-the-user。 –

回答

1

嘗試寫自己的Authenticator

import javax.mail.Authenticator; 
import javax.mail.PasswordAuthentication; 
public class SMTPAuthenticator extends Authenticator { 
    private String userName; 
    private String password; 

    public void setUserName(String userName) { 
     this.userName = userName; 
    } 

    public String getUserName() { 
     return userName; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public SMTPAuthenticator(String userName,String password){ 
     this.userName=userName; 
     this.password=password; 
    } 

    public PasswordAuthentication getPasswordAuthentication() 
    { 
     return new PasswordAuthentication(userName, password); 
    } 
} 

然後用它來創建會話:

SMTPAuthenticator authenticator = new SMTPAuthenticator(username, password); 
Properties properties = new Properties(); 
properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName()); 
properties.setProperty("mail.smtp.auth", "true"); 
properties.setProperty("mail.smtp.host", "outlook.office365.com"); 
properties.setProperty("mail.smtp.port", "587"); 

Session session = Session.getInstance(properties, authenticator); 

而且刪除此行,如果你沒有TSL保護的連接

props.put("mail.smtp.starttls.enable", "true"); 
+0

您可以在編輯後回覆整個代碼嗎?我無法理解它。 – AK2

+1

@ AK2只需爲Authenticator創建一個新類,然後用這段代碼替換您的代碼(包含屬性和會話的部分) – MihaiC

+1

當我編寫您的SMTPAuthenticator時,它會在此行發出錯誤'return new PasswordAuthentication(userName,密碼);'它說PasswordAuthentication不能應用於給定的類型。 – AK2

相關問題