2016-06-09 70 views
0

我正在創建一個簡單的java郵件程序,該程序工作正常,最後一個系統打印也工作。但問題是我dint收到outlook.here郵件我使用公司outlook.please有人幫助我。 我在這裏如何使用Javamail發送簡單的電子郵件?

enter code here 

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




public class SimpleSendEmail 
{ 
    public static void main(String[] args) 

    { 

     String host = "compny host"; 
     String from = "mail id"; 
     String to = "[email protected]"; 
     String subject = "birthday mail"; 
     String messageText = "I am sending a message using the" 
       + " simple.\n" + "happy birthday."; 
     boolean sessionDebug = false; 
     Properties props = System.getProperties(); 
     props.put("compny host", host); 
     props.put("mail.smtp.port", "25"); 
     props.put("mail.transport.protocol", "smtp"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     Session session = Session.getDefaultInstance(props, null); 
     // Set debug on the Session so we can see what is going on 
     // Passing false will not echo debug info, and passing true 
     // will. 
     session.setDebug(sessionDebug); 
     try 
     { 
      Message msg = new MimeMessage(session); 
      msg.setFrom(new InternetAddress(from)); 
      InternetAddress[] address = { new InternetAddress(to) }; 
      msg.setRecipients(Message.RecipientType.TO, address); 
      msg.setSubject(subject); 
      msg.setSentDate(new Date()); 
      msg.setText(messageText); 

      Transport.send(msg); 
      System.out.println("Sent message successfully...."); 
     } 

     catch (MessagingException mex) 
     { 
      mex.printStackTrace(); 
     } 
    } 
} 



output 
Sent message successfully.... 

回答

0

我確實希望你在你身邊使用正確的主機。

但是您缺少用戶名和密碼。

transport = session.getTransport("smtp"); 
transport.connect(hostName, port, user, password); 
transport.sendMessage(message, message.getAllRecipients()); 

,或者您可以使用身份驗證:

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

我使用的代碼無需驗證是這個是否可能> ??? – fAZ

+0

電子郵件服務器應始終要求身份驗證,否則它將面向垃圾郵件發送者。 – tak3shi

相關問題