【问题标题】:Sending Email through Javamail通过 Javamail 发送电子邮件
【发布时间】:2013-02-20 12:28:32
【问题描述】:

我一直在尝试使用 javamail api 发送电子邮件。来自 smtp 服务器 (smtp.live.com) 的调试显示 550 5.3.4 未采取请求的操作;要继续发送消息,请登录您的帐户。

它似乎可以很好地创建消息,但不允许它发送。任何想法为什么?

  try
     {
     // Setup properties for e-mail server
     Properties props = System.getProperties();
     props.put("mail.smtp.host", mConfig.getEmailHost());
     props.put("mail.smtp.starttls.enable", "true");
     props.put("mail.smtp.auth", "true");
     props.put("mail.smtp.port", "587");

     // Get a Session object
     Session session = Session.getInstance(props, new MyAuthenticator());
     session.setDebug(true);
     Transport transport = session.getTransport("smtp");

     // Create message
     MimeMessage message = new MimeMessage(session);

     // Add the to/from fields
     message.setFrom(new InternetAddress(mFromAddr, mFromName));
     message.addRecipient(Message.RecipientType.TO, new InternetAddress(mToAddr));
     if (mCCAddrs != null)
        {
        for (int i=0; i<mCCAddrs.length; i++)
           message.addRecipient(Message.RecipientType.CC, new InternetAddress(mCCAddrs[i]));
        }
     // Add Subject
     message.setSubject(mEmailSubject);

     // Setup multipart message for including the attachment
     Multipart multipart = new MimeMultipart();

     // Create message body
     BodyPart messageBodyPart = new MimeBodyPart();
     messageBodyPart.setText(mEmailBody);
     multipart.addBodyPart(messageBodyPart);

     if (mAttachmentName != null)
        {
        // Create message attachment
        BodyPart messageAttachmentPart = new MimeBodyPart();
        messageAttachmentPart.setDataHandler(new DataHandler(new ByteArrayDatasource(data)));
        messageAttachmentPart.setFileName(mAttachmentName);
        multipart.addBodyPart(messageAttachmentPart);
        }

     // Send message
     message.setContent(multipart);
     transport.connect(mConfig.getEmailHost(), mConfig.getEmailUser(), mConfig.getEmailPassword());
     transport.sendMessage(message, message.getAllRecipients());
     transport.close();
     }
  catch (Exception ex)
     {
     ex.printStackTrace();
     throw new Exception("Failed to send e-mail: " + ex.getMessage());
     }
  `

【问题讨论】:

    标签: java jakarta-mail


    【解决方案1】:

    您实际上需要在身份验证器中包含一些凭据信息。服务器向您指示它不允许匿名发送电子邮件。

    new MyAuthenticator()
    
           ^-------- fill this with some credentials
    

    请注意,除非您的邮件服务器有一些特殊要求,否则通常使用 standard password authenticator 就足够了:


    编辑/更新

    根据您的反馈,我仔细查看了您的错误消息。在我看来,Hotmail 要求您在使用它发送电子邮件之前登录到您设置的帐户并进行验证。在使用帐户之前,您可能需要使用网络浏览器登录并检查他们的激活链接。

    【讨论】:

    • 我已经有了,私有类 MyAuthenticator extends Authenticator { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(mConfig.getEmailUser(), mConfig.getEmailPassword()); } }
    • 您是否为emailUseremailPassword 调试了mConfig 值。创建身份验证器时它们是否存在?
    • 我已将会话更改为 Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(mConfig.getEmailUser(), mConfig .getEmailPassword()); } });我仍然遇到同样的错误。
    • @AC3112 - 在创建身份验证器之前,您是否验证了 mConfig.getEmailUser()mConfig.getEmailPassword() 具有有效值?
    • 有趣,您的帐户可能需要激活。请参阅我的答案的编辑。
    【解决方案2】:

    试试下面的代码。

        import java.util.Date;
        import java.util.Properties;
        import javax.mail.Address;
        import javax.mail.Message;
        import javax.mail.PasswordAuthentication;
        import javax.mail.Session;
        import javax.mail.Transport;
        import javax.mail.internet.AddressException;
        import javax.mail.internet.InternetAddress;
        import javax.mail.internet.MimeMessage;
    
        public class TestJavaMail {
        private String SMTP_PORT = "465";
        private String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        private String SMTP_HOST_NAME = "smtp.gmail.com";
        private String contentType = "text/html";
        private Properties smtpProperties;
        public TestJavaMail(){
        initProperties();
        }
    
        private void initProperties(){
        smtpProperties = new Properties();
        smtpProperties.put("mail.smtp.host", SMTP_HOST_NAME);
        smtpProperties.put("mail.smtp.auth", "true");
        smtpProperties.put("mail.debug", "true");
        smtpProperties.put("mail.smtp.port", SMTP_PORT);
        smtpProperties.put("mail.smtp.socketFactory.port",
        SMTP_PORT);
        smtpProperties.put ("mail.smtp.socketFactory.class",
        SSL_FACTORY);
        smtpProperties.put("mail.smtp.socketFactory.fallback",
        "false");
        }
        public static void main(String args[]) {
        String to= "sendToMailAddress";
        String from ="sender email_id";
        String pwd = "Sender password";
        String subject= "Java Mail";
        String body= "Testing to write Doc on java mail.";
        send(to, from, pwd , subject, body);
        }
        TestJavaMail.java Continued…
        public static void send(String to, final String from
        , final String pwd, String subject,String body){
        TestJavaMail tjm = new TestJavaMail();
        try
        {
        Properties props = tjm.getSmtpProperties() ;
        // -- Attaching to default Session, or we could start
        a new one --
        Session session = Session.getDefaultInstance(props,
        new javax.mail.Authenticator() {
        protected PasswordAuthentication
        getPasswordAuthentication() {
        return new PasswordAuthentication(from, pwd);
        }
        });
        // -- Create a new message --
        Message msg = new MimeMessage(session);
        // -- Set the FROM and TO fields --
        msg.setFrom(new InternetAddress(from));
        msg.setRecipients(Message.RecipientType.TO,
        InternetAddress.parse(to, false));
        // -- Set the subject and body text --
        msg.setSubject(subject);
        msg.setText(body);
        msg.setSentDate(new Date());
        // -- Send the message –
        Transport.send(msg);
        System.out.println("Message sent OK.");
        }
        catch (Exception ex)
        {
        ex.printStackTrace();
        }
        }
        public Properties getSmtpProperties() {
        return smtpProperties;
        }
        public void setSmtpProperties(Properties smtpProperties) {
        this.smtpProperties = smtpProperties;
        }
        }
        }
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-04-19
      • 2020-10-02
      • 1970-01-01
      • 2015-03-03
      • 2017-12-18
      • 2022-08-14
      • 2012-04-27
      • 2017-10-17
      • 2012-03-22
      相关资源
      最近更新 更多