FROM地址不正确

时间:2014-12-16 05:38:39

标签: java email smtp javamail

从我发送的电子邮件中的FROM:地址无法正确显示。这不是在应用程序服务器中运行,而是从main()调用:

// Get system properties
final Properties properties = System.getProperties();

// Setup mail server
properties.setProperty("mail.smtp.host", "localhost");

// Get the default Session object.
final Session session = Session.getDefaultInstance(properties);

// Create a default MimeMessage object.
final MimeMessage message = new MimeMessage(session);

// Set From: header field of the header.
message.setFrom(new InternetAddress("support@mydomain.com"));

// Set To: header field of the header.
message.setRecipients(javax.mail.Message.RecipientType.TO, "you@you.com");

// Set Subject: header field
message.setSubject("Your pants are on fire!");

message.setSentDate(new Date());

// Now set the actual message
message.setText("Take me to the bridge. ow!");

// Send message
Transport.send(message);

这是原始收到的电子邮件。需要注意两点:运行java进程的linux用户名是 mungo 。 linux servername / hostname是 servername 。域名 santamaria.com 。这些已经从原来改变为保护有罪。

我的问题是:为什么回归路径 support@mydomain.com ,我怎么能这样做?其次,我如何添加友好名称?例如 Roger Earl< support@mydomain.com>

Delivered-To: you@you.com
Received: by 10.70.125.201 with SMTP id ms9csp51721pdb;
        Sat, 22 Nov 2014 07:38:22 -0800 (PST)
X-Received: by 10.140.84.71 with SMTP id k65mr15089869qgd.76.1416670702208;
        Sat, 22 Nov 2014 07:38:22 -0800 (PST)
Return-Path: <mungo@servername.santamaria.com>
Received: from servername.santamaria.com (servername.santamaria.com. [201.74.27.72])
        by mx.google.com with ESMTPS id z90si10522625qgd.46.2014.11.22.07.38.21
        for <you@you.com>
        (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Sat, 22 Nov 2014 07:38:22 -0800 (PST)
Received-SPF: none (google.com: mungo@servername.santamaria.com does not designate permitted sender hosts) client-ip=201.74.27.72;
Authentication-Results: mx.google.com;
       spf=none (google.com: mungo@servername.santamaria.com does not designate permitted sender hosts) smtp.mail=mungo@servername.santamaria.com
Received: from servername.santamaria.com (localhost [127.0.0.1])
    by servername.santamaria.com (8.14.4/8.14.4) with ESMTP id sAMFcLkq012340
    for <you@you.com>; Sat, 22 Nov 2014 15:38:21 GMT
From: RogerEarl <mungo@servername.santamaria.com>
Date: Sat, 22 Nov 2014 15:38:21 +0000 (UTC)
To: you@you.com
Message-ID: <1542856295.1.1416670701712.JavaMail.mungo@servername.santamaria.com>
Subject: Your pants are on fire!
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Take me to the bridge. ow!

更新 工作代码如下所示:

properties.setProperty("mail.smtp.from", "support@mydomain.com");
message.setFrom(new InternetAddress("support@mydomain.com", "My Friendly Support Name");

1 个答案:

答案 0 :(得分:1)

InternetAddress类包含一个构造函数,允许您设置一个&#34;个人名称&#34;以及电子邮件地址;阅读javadocs

Return-Path标头由接收邮件服务器设置,可能基于来自&#34;的&#34;信封。地址。将mail.smtp.from属性设置为您要使用的地址。