发送邮件时这里有什么问题?

时间:2018-04-18 10:24:19

标签: java spring javamail

当我尝试发送电子邮件时,该程序会抛出以下异常。

this:com.sun.mail.smtp.SMTPSendFailedException: 550 5.7.60 SMTP; Client does not have permissions to send as this sender

代码

public static void main(String[] args) { 
    final String userName = "some@.com"; //requires valid gmail id
    final String password = "password"; // correct password for gmail id
    final String toEmail = "someemail@.com"; // can be any email id 

    System.out.println("TLSEmail Start");
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.office365.com"); //SMTP Host
    props.put("mail.smtp.port", "587"); //TLS Port
    props.put("mail.smtp.auth", "true"); //enable authentication
    props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS

    //create Authenticator object to pass in Session.getInstance argument
    Authenticator auth = new Authenticator() {
        //override the getPasswordAuthentication method
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password);
        }
    };

    Session session = Session.getInstance(props, auth);

    EmailUtil.sendEmail(session, toEmail,"TLSEmail Testing Subject", "TLSEmail Testing Body");
}

1 个答案:

答案 0 :(得分:1)

是的,我已经找到了您使用的代码副本:

https://github.com/praveenaki/Java-Helpers/blob/master/Emailer/src/main/java/com/pvn/TLSAuthEmailer/EmailUtil.java

请返回并阅读完整代码。 "来自"和"到"地址正在sendEmailNow()中设置。您需要将msg.setFrom()设置为正确的地址,否则Office 365服务器将拒绝该电子邮件。

当你说错误信息时,这就是你的错误信息:

  

客户端无权作为此发件人发送

为了完整起见,我复制了您在下面复制的完整代码:

package com.pvn.TLSAuthEmailer;
/*
 * Refer : http://www.journaldev.com/2532/java-program-to-send-email-using-smtp-gmail-tls-ssl-attachment-image-example
 * */



import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class EmailUtil {

    /**
     * Utility method to send simple HTML email
     * @param session
     * @param toEmail
     * @param subject
     * @param body
     */
    public static void sendEmail(Session session, String toEmail, String subject, String body){
        try
        {
          MimeMessage msg = new MimeMessage(session);
          //set message headers
          msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
          msg.addHeader("format", "flowed");
          msg.addHeader("Content-Transfer-Encoding", "8bit");

          msg.setFrom(new InternetAddress("no_reply@journaldev.com", "NoReply-JD"));

          msg.setReplyTo(InternetAddress.parse("no_reply@journaldev.com", false));

          msg.setSubject(subject, "UTF-8");

          msg.setText(body, "UTF-8");

          msg.setSentDate(new Date());

          msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
          System.out.println("Message is ready");
          Transport.send(msg);  

          System.out.println("EMail Sent Successfully!!");
        }
        catch (Exception e) {
          e.printStackTrace();
        }
    }

    public static void sendEmailNow() {
        final String fromEmail = "test@gmail.com"; //requires valid gmail id
        final String password = "********"; // correct password for gmail id
        final String toEmail = "praveen.akinapally@gmail.com"; // can be any email id 

        System.out.println("TLSEmail Start");
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com"); //SMTP Host
        props.put("mail.smtp.port", "587"); //TLS Port
        props.put("mail.smtp.auth", "true"); //enable authentication
        props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS

                //create Authenticator object to pass in Session.getInstance argument
        Authenticator auth = new Authenticator() {
            //override the getPasswordAuthentication method
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(fromEmail, password);
            }
        };
        Session session = Session.getInstance(props, auth);

        EmailUtil.sendEmail(session, toEmail,"TLSEmail Testing Subject", "Balance > 100");

    }
}