java通过雅虎发送电子邮件

时间:2014-03-17 05:34:32

标签: java email

这是我的代码

 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 SendMail 
 {
   String host, port, emailid,username, password;
   Properties props = System.getProperties();
   Session l_session = null;
   public SendMail() 
   {
     host = "smtp.mail.yahoo.com";
     port = "587";
     emailid = "xxx@yahoo.com";
     username = "xxx@yahoo.com";
     password = "xxxxx";
     emailSettings();
     createSession();
     sendMessage("xxx@yahoo.com", "yyyy@yahoo.com","Test","test Mail");
   }
   public void emailSettings() 
   {
     props.put("mail.smtp.host", host);
     props.put("mail.smtp.auth", "true");
     props.put("mail.debug", "false");
     props.put("mail.smtp.port", port);
   }
   public void createSession() 
   {
     l_session = Session.getInstance(props,new javax.mail.Authenticator() 
                      {
                        protected PasswordAuthentication getPasswordAuthentication()
                        {
                          return new PasswordAuthentication(username, password);
                        }
                      });
    l_session.setDebug(true); // Enable the debug mode
   }

   public boolean sendMessage(String emailFromUser, String toEmail, String subject, String msg) 
   {       
    try 
    {
        MimeMessage message = new MimeMessage(l_session);
        emailid = emailFromUser;
        message.setFrom(new InternetAddress(this.emailid));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
        message.setSubject(subject);
        message.setContent(msg, "text/html");
        //message.setText(msg);
        Transport.send(message);
        System.out.println("Message Sent");
    } 
    catch (MessagingException mex) 
    {
        mex.printStackTrace();
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }//end catch block
    return true;
  }
  public static void main(String args[])
  {
     new SendMail();
  }
 }

当我运行此代码时,我得到 com.sun.mail.smtp.SMTPSendFailedException:530 5.7.1需要验证这个错误。我使用PasswordAuthentication。我不知道为什么会出现这种异常。请问有谁能告诉我如何克服这个错误?

2 个答案:

答案 0 :(得分:1)

请试试下面的代码。我认为你没有设置所有财产。

Properties props = new Properties();
   props.put("mail.smtp.host", "smtp.mail.yahoo.com");
   props.put("mail.stmp.user", "username");
   props.put("mail.smtp.auth", "true");
   props.put("mail.smtp.starttls.enable", "true");
   props.put("mail.smtp.password", "password");
   Session session = Session.getDefaultInstance(props, new Authenticator() {
@Override
   protected PasswordAuthentication getPasswordAuthentication() {
        String username = "username";
        String password = "password";
       return new PasswordAuthentication("username", "password");
   }
});

答案 1 :(得分:0)

25雅虎门不再响应。使用587:

      props.put("mail.smtp.port", "587");

完整代码:

Properties props = new Properties();
   props.put("mail.smtp.host", "smtp.mail.yahoo.com");
   props.put("mail.stmp.user", "username");

   props.put("mail.smtp.port", "587");

   props.put("mail.smtp.auth", "true");
   props.put("mail.smtp.starttls.enable", "true");
   props.put("mail.smtp.password", "password");
   Session session = Session.getDefaultInstance(props, new Authenticator() {
@Override
   protected PasswordAuthentication getPasswordAuthentication() {
        String username = "username";
        String password = "password";
       return new PasswordAuthentication("username", "password");
   }
});