gpt4 book ai didi

JavaMail 不通过后缀发送 SMTP 电子邮件

转载 作者:搜寻专家 更新时间:2023-10-31 20:33:57 24 4
gpt4 key购买 nike

我正在尝试设置 JavaMail 以使用 Postfix 发送和接收电子邮件,它安装在我的 CentOS7 开发箱上。我已经确认 postfix 能够通过在终端中键入 MAIL=/home/root/Maildir 来显示收到的电子邮件,然后返回,然后是 mail,其中列出了用户帐户收到的所有电子邮件。但是,当我以 root 身份登录并在 CentOS 7 终端 中查看收到的电子邮件时,在我运行 Javamail 代码后没有新电子邮件,如下所述。 如何让 Javamail 发送 smtp 邮件?

这是我的类(class):

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

//Send a simple, single part, text/plain e-mail
public class TestEmail {

public void send(){
// SUBSTITUTE YOUR EMAIL ADDRESSES HERE!
String to = "root@localhost";
String from = "username@localhost";
// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!
String host = "localhost";

// Create properties, get Session
Properties props = new Properties();

//http://docs.oracle.com/javaee/6/api/javax/mail/Session.html
// If using static Transport.send(),
// need to specify which host to send it to
props.put("mail.smtp.host", host);
// To see what is going on behind the scene
props.put("mail.debug", "true");
Session session = Session.getInstance(props);

try {
// Instantiate a message
Message msg = new MimeMessage(session);

//Set message attributes
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("A new record was just added.");
msg.setSentDate(new Date());

// Set message content
msg.setText("This is a test of sending a " +
"plain text e-mail through Java.\n" +
"Here is line 2.");

//Send the message
Transport.send(msg);
}
catch (MessagingException mex) {
// Prints all nested (chained) exceptions as well
mex.printStackTrace();
}
}
}//End of class

我这样调用这个类:

TestEmail em = new TestEmail();
em.send();

运行上述代码时,eclipse 控制台会产生以下日志:

DEBUG: JavaMail version 1.5.0-b01
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
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 "localhost", port 25, isSSL false
220 localhost.localdomain ESMTP Postfix
DEBUG SMTP: connected to host "localhost", port: 25

EHLO localhost.localdomain
250-localhost.localdomain
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "10240000"
DEBUG SMTP: Found extension "VRFY", arg ""
DEBUG SMTP: Found extension "ETRN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<username@localhost>
250 2.1.0 Ok
RCPT TO:<root@localhost>
250 2.1.5 Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP: root@localhost
DATA
354 End data with <CR><LF>.<CR><LF>
Date: Mon, 5 Jan 2015 13:12:02 -0800 (PST)
From: username@localhost
To: root@localhost
Message-ID: <1738078707.0.1420492322780.JavaMail.username@localhost.localdomain>
Subject: A new record was just added.
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

This is a test of sending a plain text e-mail through Java.
Here is line 2.
.
250 2.0.0 Ok: queued as DB1249A618
QUIT
221 2.0.0 Bye
sessionID is: 0816C244BDBAAD890D82138DC3801962

最佳答案

从日志 Ok: queued as DB1249A618 邮件被 SMTP 服务器正确接收。所以问题可能是服务器配置错误(可能是反垃圾邮件过滤器)或错误的邮件地址。

看这里https://serverfault.com/questions/485505/get-postfix-to-forward-roots-mail检查您的服务器配置是否正确。

关于JavaMail 不通过后缀发送 SMTP 电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27788349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com