我得到 530 5.7.0 必须先发出 STARTTLS 命令。a5sm29349940pbw.4 - 从java程序发送邮件时gsmtp错误,如何解决?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18481810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:01:28  来源:igfitidea点击:

i am getting 530 5.7.0 Must issue a STARTTLS command first. a5sm29349940pbw.4 - gsmtp error while send a mail from java program, how to solve it?

javaemailjavamail

提问by wadjikars

Error:

错误:

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. a5sm29349940pbw.4 - gsmtp

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1829)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1368)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:886)
at javax.mail.Transport.send0(Transport.java:191)
at javax.mail.Transport.send(Transport.java:120)
at com.conceptbuild.EmailGenerator.main(EmailGenerator.java:120)

Code:

代码:

package com.conceptbuild;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
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;
import javax.mail.internet.MimeMessage.RecipientType;

public class EmailGenerator {

    public EmailGenerator() {
        // TODO Auto-generated constructor stub
    }

    private class MailAuthenticator extends Authenticator {

        String username = "username";

        String password = "password";

        public MailAuthenticator() {
            // TODO Auto-generated constructor stub
        }

        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }

    }

    public static void main(String[] args)   {

        String host="smtp.gmail.com";
        String from="fromaddress";
        String to="toaddress";
        String cc="ccaddress";
        Address fromaddress=null,toaddress=null,ccaddress=null;

        Properties mailproperties= new Properties();

        mailproperties.put("mail.smtp.host", host);
        mailproperties.put("mail.smtp.port", 25);
        mailproperties.put("mail.debug", "true");
        mailproperties.put("mail.transport.protocal", "smtps");
        mailproperties.put("mail.smtp.STARTTLS.enable", "true");
        mailproperties.put("mail.smtp.auth", "true");
        mailproperties.put("mail.smtps.**ssl.enable", "false");
        mailproperties.setProperty("mail.smtps.**ssl.required", "false");

        EmailGenerator emailgenerator= new EmailGenerator();

        MailAuthenticator auth= emailgenerator.new MailAuthenticator();

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

        try {
             fromaddress= new InternetAddress(from,"Sushant Wadjikar");
             toaddress= new InternetAddress(to);
             ccaddress=new InternetAddress(cc);

        }catch (AddressException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        } catch (UnsupportedEncodingException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        }

        Message message= new MimeMessage(session);

        try {
            message.setFrom(fromaddress);
            message.setRecipient(RecipientType.TO, toaddress);
            message.setRecipient(RecipientType.CC, ccaddress);
            message.setSubject("Hello !!! My First Mail from my java program");
            message.setSentDate(new Date());
            message.setText("Good Morning"+"\n"+"This is my first mail, sending it from my java program"+"\n"+"Thanks and Regards"+"\n"+"Sushant");

            Transport transport;
            transport = session.getTransport("smtp");

            transport.connect();

            message.saveChanges();

            transport.send(message);

            System.out.println("Mail send successfully.........");

        } catch (MessagingException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        }
    }
}

回答by Bill Shannon

Thiswill likely fix your problem.

可能会解决您的问题。

Also, make up your mind on whether you're using the "smtp" protocol or the "smtps" protocol and name the properties accordingly. And there are no properties with "**" in their names.

此外,请决定您使用的是“smtp”协议还是“smtps”协议,并相应地命名属性。并且名称中没有带有“**”的属性。