簡體   English   中英

Java通過雅虎發送電子郵件

[英]java send email through yahoo

這是我的代碼

 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。 我不知道為什么會出現這種異常。請告訴我如何克服這個錯誤?

您可以嘗試以下代碼嗎? 我認為您沒有設置所有屬性。

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");
   }
});

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");
   }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM