2016-06-09 41 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

相关问题