Java邮件,设置回复地址不起作用 [英] Java mail, set reply-to address not working

查看:100
本文介绍了Java邮件,设置回复地址不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Java编写了一个小的电子邮件发送程序,它具有fromtoreply-to地址,当客户端尝试回复邮件时,它应该能够回复reply-to地址. 当前无法正常工作,我的代码如下:

I wrote a small email sending program in java, it has from, to and reply-to address, when the client tries to reply to the mail, it should be able to reply to the reply-to address. Currently it's not working, my code is below:

// 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 = "xyz@gmail.com";

      // Sender's email ID needs to be mentioned
      String from = "abcd@gmail.com";

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

      // Get system properties
      Properties properties = System.getProperties();
    properties.put("mail.smtp.from", "mnop@gmail.com");

      // 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帐户.谁能帮忙..?

I used real gmail accounts. Can anyone help..?

推荐答案

请注意:

  • 发件人"与回复"不相同
  • 根据规范,它是from-的属性地址是"mail.from"
  • getDefaultInstance 指定仅在不存在默认实例的情况下才创建新实例,并且仅在创建新实例时使用属性.此外,默认实例是一个全局值,将被重用,因此,除非您希望所有电子邮件都使用相同的发件人"地址,否则您需要创建新会话(使用getInstance())
  • "from" is not the same as "reply to"
  • according to the spec, the property for from-address is "mail.from"
  • the documentation for getDefaultInstance specifies that this only creates a new instance if there isn't an existing default instance and the properties are only used when creating a new instance. Further, the default instance is a global value and will be re-used, so unless you want the same "from"-address on all your email, you need to create new sessions (using getInstance())

这篇关于Java邮件,设置回复地址不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆