第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

javax.mail:無法向 SMTP 主機(jī)發(fā)送命令

javax.mail:無法向 SMTP 主機(jī)發(fā)送命令

湖上湖 2022-05-25 16:11:32
我是 Java Mail 的新手。當(dāng)我執(zhí)行下面的代碼時(shí),我遇到了一個(gè)異常javax.mail.MessagingException:無法向 SMTP 主機(jī)發(fā)送命令public void sendMessage(EmailMessage emailMessage) throws MessagingException {    Properties props = new Properties();    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");    System.out.println(emailMessage.getFromAddress());    Optional<EmailId> emailIdOptional = emailIdRepository.findByEmailId(emailMessage.getFromAddress());    System.out.println(emailIdOptional.get().getEmailId());    Session session = Session.getInstance(props, new javax.mail.Authenticator() {        protected PasswordAuthentication getPasswordAuthentication() {            return new PasswordAuthentication(emailIdOptional.get().getEmailId(), emailIdOptional.get().getPassword());        }    });    javax.mail.Message msg = new MimeMessage(session);    msg.setFrom(new InternetAddress(emailMessage.getFromAddress(), false));    msg.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(emailMessage.getToAddress()));    msg.setSubject(emailMessage.getSubject());    msg.setContent(emailMessage.getMessage(), "text/html");    msg.setSentDate(new Date());    MimeBodyPart messageBodyPart = new MimeBodyPart();    messageBodyPart.setContent(emailMessage.getMessage(), "text/html");    Transport.send(msg);}任何人都請幫我解決這個(gè)問題。
查看完整描述

2 回答

?
楊魅力

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個(gè)贊

String host = "smtp.xyz.com";

final String username = "xyz@business.com";

final String password = "your password";


Properties props = new Properties();


props.put("mail.smtp.auth", "true");

props.put("mail.smtp.starttls.enable", "true");

// go to account setting -> smtp setting and get server name and port

props.put("mail.smtp.host", "your server name");

props.put("mail.smtp.port", "your server port");

props.put("mail.debug", "true");


try {

    Session session = Session.getInstance(prop,

            new javax.mail.Authenticator() {

                protected javax.mail.PasswordAuthentication getPasswordAuthentication() {

                    return new javax.mail.PasswordAuthentication(username, password);

                }

            });


    Message msg = new MimeMessage(session);


    msg.setFrom(new InternetAddress("xyz@business.com"));


    msg.setRecipients(Message.RecipientType.TO,

            InternetAddress.parse("abc@business.com"));

    msg.setSubject("Test E-Mail through Java");


    Transport.send(msg,username,password);

    System.out.println("Sent message successfully...");

}catch(Exception e){


}

你可以試試這個(gè)可能會(huì)解決你的問題!?。?!


查看完整回答
反對 回復(fù) 2022-05-25
?
牛魔王的故事

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超3個(gè)贊

此代碼對我有用:-


public static void SendMessage(final String femail, final String fpass, final String email, final String subject, final String message)

{

//Creating properties

Properties props = new Properties();

//Configuring properties for gmail

//If you are not using gmail you may need to change the values

 props.put("mail.smtp.host", "smtp.gmail.com");

 props.put("mail.smtp.port", "587");

 props.put("mail.smtp.auth", "true");

 props.put("mail.smtp.starttls.enable", "true");

 props.put("mail.smtp.ssl.trust", "smtp.gmail.com");     

//Creating a new session

Session session = Session.getDefaultInstance(props,

        new javax.mail.Authenticator() {

            //Authenticating the password

            protected PasswordAuthentication getPasswordAuthentication() {

                return new PasswordAuthentication(femail, fpass);

            }

        });

try {

    //Creating MimeMessage object

    MimeMessage mm = new MimeMessage(session);

    //Setting sender address

    mm.setFrom(new InternetAddress(femail));

    //Adding receiver

    mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));

    //Adding subject

    mm.setSubject(subject);

    //Adding message

    mm.setText(message);           

    //Sending email

    Transport.send(mm);


} catch (MessagingException e) {

    e.printStackTrace();

}

}

我已經(jīng)編輯了代碼并使用 starttls 方法進(jìn)行了設(shè)置。


希望會(huì)有所幫助?。。?!


查看完整回答
反對 回復(fù) 2022-05-25
  • 2 回答
  • 0 關(guān)注
  • 291 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)