简体   繁体   English

通过Java代码发送电子邮件中的SMTP异常

[英]SMTP exception in sending the email through java code

I am trying to send the email using java but my code is throwing the exception Exception herejavax.mail.MessagingException: Can't send command to SMTP host; 我正在尝试使用Java发送电子邮件,但是我的代码在此处引发了异常Exceptionjavax.mail.MessagingException:无法将命令发送到SMTP主机;

I am using the following code 我正在使用以下代码

 import java.util.Properties;
 import javax.mail.*;
 import javax.mail.internet.*;

 public class SendEmail {

     public static void main(String args[]) {

       final String username ="mymail@gmail.com"; 
       final String password = "password here";

       final String xtomail="email here";
       final String cc="email here";
       final String bcc="email here";

       final String xsub="heavy discount for static websites";
       final String xbody="we are great leader in software industry";

       Properties props = new Properties();

       try{    
           props.put("mail.smtp.auth", "true");
           props.put("mail.smtp.starttls.enable", "true");
           props.put("mail.smtp.host", "smtp.gmail.com");
           props.put("mail.smtp.port", "587");

       } catch(Exception ex) {
           System.out.println("exception here"+ex); 
       }

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

       try {

           Message message = new MimeMessage(session);
           message.setFrom(new InternetAddress(username));
           message.setRecipients(Message.RecipientType.TO,
                                 InternetAddress.parse(xtomail));
           message.addRecipients(Message.RecipientType.CC,InternetAddress.parse(cc));
           message.addRecipients(Message.RecipientType.BCC,InternetAddress.parse(bcc));
           message.setSubject(xsub);
           message.setText(xbody);

           Transport.send(message);
           System.out.println("email send");             
       }
       catch (Exception e) {
           System.out.println("Exception here"+e);
       }                    
    }
}        

What does the debug output show? 调试输出显示什么?

Is your Gmail account enabled for less secure apps ? 您的Gmail帐户是否启用了安全性较低的应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM