Java邮件,设置回复地址不起作用

2024-04-28

我用java写了一个小的电子邮件发送程序,它有from, to and reply-to地址,当客户端尝试回复邮件时,应该能够回复reply-to地址。 目前它不起作用,我的代码如下:

// File Name SendEmail.java

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

public class SendEmail
{
   public static void main(String [] args)
   {

      // Recipient's email ID needs to be mentioned.
      String to = "[email protected] /cdn-cgi/l/email-protection";

      // Sender's email ID needs to be mentioned
      String from = "[email protected] /cdn-cgi/l/email-protection";

      // Assuming you are sending email from localhost
      String host = "localhost";

      // Get system properties
      Properties properties = System.getProperties();
    properties.put("mail.smtp.from", "[email protected] /cdn-cgi/l/email-protection");

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

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

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

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress(to));

         // Set Subject: header field
         message.setSubject("This is the Subject Line!");

         // Now set the actual message
         message.setText("New Message goes here");

         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }
}

我使用的是真实的 Gmail 帐户。任何人都可以帮忙吗..?


Try:

MimeMessage message = new MimeMessage(session);
message.setReplyTo(new javax.mail.Address[]
{
    new javax.mail.internet.InternetAddress("[email protected] /cdn-cgi/l/email-protection")
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java邮件,设置回复地址不起作用 的相关文章

随机推荐