试图使用JavaMail

时间:2014-07-25 00:27:05

标签: java

所以我试图创建一个服务器,我可以将发件人电子邮件更改为我想要的任何内容。我已经尝试过注册一个免费的SMTP服务器,但我不确定我是否可以做我想做的事情。如果这不起作用,有人可以引导我到SMTP服务器,这将允许我这样做吗?

package Mailer2;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class mailer2 {

    public static void main(String[] args) throws AddressException, MessagingException{
        Properties prop = new Properties();
        prop.put("mail.smtp.host", "ssrs.reachmail.net");
        prop.put("mail.smtp.port", "587");

        Session session = Session.getDefaultInstance(prop, new Authenticator() {

            // Override method to Authenticate to mail server
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("MCA28\\admin", "NotShowingPassword");
            }
            });

        session.setDebug(true);

        MimeMessage Msg = new MimeMessage(session);

        Msg.setFrom(new InternetAddress("yo@yo.com"));

        Msg.setRecipients(RecipientType.TO, "emailtosend@gmail.com");

        Msg.setSubject("afa");

        // Initiate MimeBodyPart for filling email content
        MimeBodyPart messagePart = new MimeBodyPart();
        messagePart.setText("Message Content");
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messagePart);
        Msg.setContent(multipart);

        // Email Sending process
        Transport.send(Msg);

    }



}

这是日志:

DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "ssrs.reachmail.net", port 587, isSSL false
220 ssrs.reachmail.net rmsmtp 3.1.0a 4a1db2f953
DEBUG SMTP: connected to host "ssrs.reachmail.net", port: 587

EHLO 192.168.1.4
250-EASYSMTP
250-8BITMIME
250-SIZE 102400000
250-AUTH PLAIN LOGIN
250 STARTTLS
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "SIZE", arg "102400000"
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<yo@yo.com>
503 error: not authorized, use AUTH
DEBUG SMTP: got response code 503, with response: 503 error: not authorized, use AUTH

RSET
250 ok
DEBUG SMTP: MessagingException while sending, THROW: 
com.sun.mail.smtp.SMTPSendFailedException: 503 error: not authorized, use AUTH
;
  nested exception is:
    com.sun.mail.smtp.SMTPSenderFailedException: 503 error: not authorized, use AUTH

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
    at javax.mail.Transport.send0(Transport.java:195)
    at javax.mail.Transport.send(Transport.java:124)
    at Mailer2.mailer2.main(mailer2.java:51)
Caused by: com.sun.mail.smtp.SMTPSenderFailedException: 503 error: not authorized, use AUTH

    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1616)
    ... 4 more
QUIT
221 bye
Exception in thread "main" com.sun.mail.smtp.SMTPSendFailedException: 503 error: not authorized, use AUTH
;
  nested exception is:
    com.sun.mail.smtp.SMTPSenderFailedException: 503 error: not authorized, use AUTH

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
    at javax.mail.Transport.send0(Transport.java:195)
    at javax.mail.Transport.send(Transport.java:124)
    at Mailer2.mailer2.main(mailer2.java:51)
Caused by: com.sun.mail.smtp.SMTPSenderFailedException: 503 error: not authorized, use AUTH

    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1616)
    ... 4 more

1 个答案:

答案 0 :(得分:1)

代码遗失prop.put("mail.smtp.auth", "true");

根据提供商的不同,您可能还需要在邮件提交端口上请求SSL或STARTLS。