簡體   English   中英

發送未經身份驗證的電子郵件

[英]Send email without authentication

我正在嘗試開發一個代碼,該代碼允許通過Javamail發送郵件而無需身份驗證。

        Properties properties = System.getProperties();
        properties.put("mail.smtp.auth", "false");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.port", 587);
        Session session = Session.getInstance(properties);
        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress("xxxxxx@hotmail.com"));
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress("yyyyyyy@gmail.com"));
            message.setSubject("This is the Subject Line!");
            message.setText("This is actual message");
            Transport.send(message);
            System.out.println("Sent message successfully....");
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }

但是當我執行它時,我得到這個異常

com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required.

是否可以發送未經身份驗證的郵件? 我想念什么嗎?

嘗試這個

    Properties props = new Properties();
    props.put("mail.smtp.host", mailHost);
    props.put("mail.smtp.port", port);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");

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

    session.setDebug(true);

    MimeMessage message = new MimeMessage(session);

    message.setFrom(new InternetAddress(fromAddress));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
    message.setSubject(subject);
    message.setContent(body, "text/html; charset=utf-8");
    message.setText(body);
    Transport.send(message);

Gmail特別不支持未經授權的郵件發送。 實際上,大多數公共SMTP服務器都不會(不再),因為這會導致過多的濫用。

可能可以找到不需要身份驗證的公共smtp,但是這種情況越來越少。 您總是可以運行自己的smtp或簡單地...添加身份驗證。

特別是如果您使用的是gmail,則可以簡單地傳遞您的憑據?

暫無
暫無

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

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